Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save quantra-go-algo/7ee6e0baf05803b316024fc3735fc0db to your computer and use it in GitHub Desktop.
Save quantra-go-algo/7ee6e0baf05803b316024fc3735fc0db to your computer and use it in GitHub Desktop.
def triple_barrier_method(df, holding_period=10, upper_lower_multipliers=[2, 1]):
# Set the close price as a cupy array
close = cp.array(df['Close'].values, dtype=cp.float64)
# Set the high price as a cupy array
high = cp.array(df['High'].values, dtype=cp.float64)
# Set the low price as a cupy array
low = cp.array(df['Low'].values, dtype=cp.float64)
# Set the daily volatility as a cupy array
daily_volatility = cp.array(df['vol'].values, dtype=cp.float64)
# Set the barriers empty array as a cupy array
barriers = np.empty((close.shape[0], 6), dtype=cp.float64)
# Set the upper and lower multipliers as a cupy array
upper_lower_multipliers = np.array(upper_lower_multipliers, dtype=cp.float64)
# Set the block size
block_size = 256
# Set the number of blocks
num_blocks = (close.shape[0] - 1) // block_size + 1
# Compute the barriers output
triple_barrier_method_cuda[num_blocks, block_size](close, high, low, daily_volatility, upper_lower_multipliers, holding_period, barriers)
# Transform the barriers array into a cudf dataframe
barriers_df = cudf.DataFrame(barriers, columns=['days_passed', 'price', 'vert_barrier', 'top_barrier', 'bottom_barrier', 'out'])
# Set the barrieres index to
barriers_df.set_index(df.index[-len(barriers_df.index):], inplace=True)
# Transform the dataframe from cudf-based to pandas
barriers_df = barriers_df.to_pandas()
# Return the prediction feature
return barriers_df[['out']]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment