Created
May 22, 2024 16:35
-
-
Save quantra-go-algo/ce36d99aa6aa3b9b08df4b799d02b7d6 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@cuda.jit | |
def triple_barrier_method_cuda(close, high, low, daily_volatility, upper_lower_multipliers, holding_period, out): | |
# Set the number days passed to zero | |
days_passed = 0 | |
# Set the vertical barrier initial value to NaN | |
vert_barrier = math.nan | |
# Set the top barrier initial value to NaN | |
top_barrier = math.nan | |
# Set the bottom barrier initial value to NaN | |
bottom_barrier = math.nan | |
# Set the absolute position of the used 1-dimension thread index | |
i = cuda.grid(1) | |
# Whenever we don't get to the last observation of the Close price column | |
if i < close.shape[0]: | |
# Set the days passed as the thread index number plus 1 | |
days_passed = float(i+1) | |
# If the next hold period is located before the last column observation and the holding period is different from zero | |
if ((days_passed + holding_period) < close.shape[0]) and (holding_period != 0): | |
# Set the vertical barrier as the holding period | |
vert_barrier = days_passed + float(holding_period) | |
# If the upper multiplier is higher than 0 | |
if upper_lower_multipliers[0] > 0: | |
# Set the top barrier | |
top_barrier = round(close[i] + close[i] * upper_lower_multipliers[0] * daily_volatility[i],6) | |
# If the lower multiplier is higher than 0 | |
if upper_lower_multipliers[1] > 0: | |
# Set the bottom multiplier | |
bottom_barrier = round(close[i] - close[i] * upper_lower_multipliers[1] * daily_volatility[i],6) | |
# Set the days passed value for the function output at day i | |
out[i, 0] = days_passed | |
# Set the Close price value for the function output at day i | |
out[i, 1] = close[i] | |
# Set the vertical barrier value for the function output at day i | |
out[i, 2] = float(vert_barrier) | |
# Set the top barrier value for the function output at day i | |
out[i, 3] = top_barrier | |
# Set the bottom barrier value for the function output at day i | |
out[i, 4] = bottom_barrier | |
cuda.syncthreads() | |
# Set the start day from the holding period | |
start = int(out[i, 0]) | |
# Set the end day from the holding period | |
end = int(out[i, 2]) | |
# If the holding period is not zero and the row number is lower than the last holding-period days of the data sample | |
if (holding_period != 0) and ((days_passed + holding_period) < close.shape[0]): | |
# Set the top barrier | |
top_barrier = out[i, 3] | |
# Set the bottom barrier | |
bottom_barrier = out[i, 4] | |
# Set the condition that tells if the take profit target was breached | |
condition_close_pt = False | |
# Set the condition that tells if the stop loss target was breached | |
condition_close_sl = False | |
# Set the condition that tells if the top barrier hits the high price to False | |
condition_high = False | |
# Set the condition that tells if the bottom barrier hits the low price to False | |
condition_low = False | |
# Set a for loop to check the barrier breaching for the next days until the holding period | |
for j in range(start, end): | |
# If the top barrier is breached by the close price | |
if round(close[j],6) >= top_barrier: | |
# Set the take profit condition to True and break the loop | |
condition_close_pt = True | |
break | |
# Set a for loop to check the barrier breaching for the next days until the holding period | |
for j in range(start, end): | |
# If the bottom barrier is breached by the close price | |
if round(close[j],6) <= bottom_barrier: | |
condition_close_sl = True | |
break | |
# Set a for loop to check the barrier breaching for the next days until the holding period | |
for j in range(start, end): | |
# If the top barrier is breached by the high price | |
if high[j] >= top_barrier: | |
condition_high = True | |
break | |
# Set a for loop to check the barrier breaching for the next days until the holding period | |
for j in range(start, end): | |
# If the bottom barrier is breached by the low price | |
if low[j] <= bottom_barrier: | |
condition_low = True | |
break | |
# If the take profit target or high condition is True | |
if condition_close_pt or condition_high: | |
# Set the prediction feature to +1 | |
out[i, 5] = 1.0 | |
# If the take profit target or high condition is True | |
elif condition_close_sl or condition_low: | |
# Set the prediction feature to -1 | |
out[i, 5] = -1.0 | |
# If any condition is breached | |
else: | |
# If the holding-period-to-current close price return is higher than zero | |
if (close[end]/close[i] - 1) > 0.0: | |
# Set the prediction feature to +1 | |
out[i, 5] = 1.0 | |
# If the holding-period-to-current close price return is lower than zero | |
elif (close[end]/close[i] - 1) < 0.0: | |
# Set the prediction feature to -1 | |
out[i, 5] = -1.0 | |
# If the holding-period-to-current close price return is equal to zero | |
else: | |
# Set the prediction feature to zero | |
out[i, 5] = 0.0 | |
# If the current day is | |
else: | |
out[i, 5] = 0.0 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment