I am having trouble with my strategy script. I am using an ATR and a hull moving average to determine my entry and exit conditions. However, when I run the code, it runs with no error but I dont see any trades executed based off of the specific conditions. Am I doing something wrong with my code?
my conditions receive data from 3 different timeframes (the current one and 2 different ones)
//@version=5
strategy('Multi-Timeframe Trend Following Strategy', overlay=true, default_qty_type=strategy.percent_of_equity, default_qty_value=10, calc_on_every_tick=true)
// User Inputs
current_tf = input.timeframe('', title='Current Timeframe')
primary_tf = input.timeframe('360', title='Primary Timeframe')
secondary_tf = input.timeframe('60', title='Secondary Timeframe')
exit_tf_options = input.string('Current Timeframe', title='Exit Timeframe', options=['Current Timeframe', 'Primary Timeframe', 'Secondary Timeframe'])
atrLength = input.int(21, title='ATR Length')
atrMultiplier = input.float(3, title='ATR Multiplier')
hullLength = input.int(21, title='Hull MA Length')
fractalLength = input.int(2, title='Fractal Length')
sensitivity = input.float(0.8, title='Sensitivity [0,1]', minval=0, maxval=1)
showSignals = input.bool(true, title='Reveal Long/Short Signals?')
showBarColor = input.bool(true, title='Show Bar-Color according to signals?')
// Function to calculate the signal logic
f_signal_logic(_src) =>
sf = sensitivity
avg = 0.
src = sf * _src + (1 - sf) * nz(avg[1], _src)
atr = ta.atr(atrLength)
up = src - atrMultiplier * atr
up1 = nz(up[1], up)
up := close[1] > up1 ? math.max(up, up1) : up
dn = src + atrMultiplier * atr
dn1 = nz(dn[1], dn)
dn := close[1] < dn1 ? math.min(dn, dn) : dn
trend = 1
trend := nz(trend[1], trend)
trend := trend == -1 and close > dn1 ? 1 : trend == 1 and close < up1 ? -1 : trend
[trend, up, dn]
[trend_cur, up_cur, dn_cur] = request.security(syminfo.tickerid, current_tf, f_signal_logic(hl2))
[trend_primary, up_primary, dn_primary] = request.security(syminfo.tickerid, primary_tf, f_signal_logic(hl2))
[trend_secondary, up_secondary, dn_secondary] = request.security(syminfo.tickerid, secondary_tf, f_signal_logic(hl2))
long_signal_current = trend_cur == 1 and trend_cur[1] == -1
short_signal_current = trend_cur == -1 and trend_cur[1] == 1
long_signal_primary = trend_primary == 1 and trend_primary[1] == -1
short_signal_primary = trend_primary == -1 and trend_primary[1] == 1
long_signal_secondary = trend_secondary == 1 and trend_secondary[1] == -1
short_signal_secondary = trend_secondary == -1 and trend_secondary[1] == 1
// Debugging signals to make sure they are being generated
plotshape(series=long_signal_current, location=location.belowbar, color=color.green, style=shape.labelup, text='Buy Current TF')
plotshape(series=short_signal_current, location=location.abovebar, color=color.red, style=shape.labeldown, text='Sell Current TF')
plotshape(series=long_signal_primary, location=location.belowbar, color=color.blue, style=shape.labelup, text='Buy Primary TF')
plotshape(series=short_signal_primary, location=location.abovebar, color=color.orange, style=shape.labeldown, text='Sell Primary TF')
plotshape(series=long_signal_secondary, location=location.belowbar, color=color.purple, style=shape.labelup, text='Buy Secondary TF')
plotshape(series=short_signal_secondary, location=location.abovebar, color=color.yellow, style=shape.labeldown, text='Sell Secondary TF')
longCondition = long_signal_current and long_signal_primary and long_signal_secondary
shortCondition = short_signal_current and short_signal_primary and short_signal_secondary
// Exit signals based on selected timeframe
var bool exit_long_signal = na
var bool exit_short_signal = na
if exit_tf_options == 'Current Timeframe'
exit_long_signal := trend_cur == -1 and trend_cur[1] == 1
exit_short_signal := trend_cur == 1 and trend_cur[1] == -1
else if exit_tf_options == 'Primary Timeframe'
exit_long_signal := trend_primary == -1 and trend_primary[1] == 1
exit_short_signal := trend_primary == 1 and trend_primary[1] == -1
else if exit_tf_options == 'Secondary Timeframe'
exit_long_signal := trend_secondary == -1 and trend_secondary[1] == 1
exit_short_signal := trend_secondary == 1 and trend_secondary[1] == -1
// Debugging prints
var table debug_table = table.new(position.top_right, 1, 10, border_width=1)
if barstate.islast
table.cell(debug_table, 0, 0, text_color=color.white, bgcolor=color.red, text="Long Condition: " + str.tostring(longCondition))
table.cell(debug_table, 0, 1, text_color=color.white, bgcolor=color.red, text="Short Condition: " + str.tostring(shortCondition))
table.cell(debug_table, 0, 2, text_color=color.white, bgcolor=color.red, text="Exit Long Signal: " + str.tostring(exit_long_signal))
table.cell(debug_table, 0, 3, text_color=color.white, bgcolor=color.red, text="Exit Short Signal: " + str.tostring(exit_short_signal))
// Entry conditions
long_condition = longCondition
short_condition = shortCondition
// Exit conditions
exit_long_condition = exit_long_signal
exit_short_condition = exit_short_signal
// Execute trades
if (long_condition)
strategy.entry('Long', strategy.long)
if (exit_long_condition)
strategy.close('Long')
if (short_condition)
strategy.entry('Short', strategy.short)
if (exit_short_condition)
strategy.close('Short')
// Plotting for visual confirmation
plotshape(series=long_condition and showSignals ? low : na, location=location.belowbar, color=color.new(color.green, 0), style=shape.labelup, text='Buy')
plotshape(series=short_condition and showSignals ? high : na, location=location.abovebar, color=color.new(color.red, 0), style=shape.labeldown, text='Sell')
bgcolor(long_condition and showBarColor ? color.new(color.green, 90) : na)
bgcolor(short_condition and showBarColor ? color.new(color.red, 90) : na)
// Debugging plots to verify signal generation
plot(series=long_signal_current ? 1 : na, color=color.green, title='Long Signal Current TF')
plot(series=short_signal_current ? 1 : na, color=color.red, title='Short Signal Current TF')
plot(series=long_signal_primary ? 1 : na, color=color.blue, title='Long Signal Primary TF')
plot(series=short_signal_primary ? 1 : na, color=color.orange, title='Short Signal Primary TF')
plot(series=long_signal_secondary ? 1 : na, color=color.purple, title='Long Signal Secondary TF')
plot(series=short_signal_secondary ? 1 : na, color=color.yellow, title='Short Signal Secondary TF')