Hello im trying to change this strategy to indicator with alert condition and still run strategy concept.the part im stuck on is strategy.opentrades, i use this to stop looking for trades if current trade not closed. also strategy.exit i use to close trade
//strategy("2Ma cross ATR Bands strategy", overlay = true)
// Inputs
atrPeriod = input(3, "ATR Period") // Period for calculating Average True Range (ATR)
srcUpper = input(defval = close, title = "Source Upper") // Source for Upper ATR Band
srcLower = input(defval = close, title = "Source Lower") // Source for Lower ATR Band
atrMultiplierUpper = input(title = "ATR Multiplier Upper", defval = 4.0, tooltip ="3.8" ) // Multiplier for Upper ATR Band
atrMultiplierLower = input(title = "ATR Multiplier Lower", defval = 3.0, tooltip = "2.5") // Multiplier for Lower ATR Band
// Calculate ATR
atr = ta.atr(atrPeriod)
// Plotting
plot(srcUpper + atr * atrMultiplierUpper, color = color.green) // Plot the Upper ATR Band
plot(srcLower - atr * atrMultiplierLower, color = color.red) // Plot the Lower ATR Band
plot(ta.sma(close, 10)) // Plot a simple moving average for reference
// Entry condition
buysign = ta.crossover(close, ta.sma(close, 10)) // Buy signal when close crosses above the 10-period SMA
//buysign2 = ta.crossover(close, xATRTrailingStop)
tp2 =srcUpper + atr * atrMultiplierUpper // Calculate Take Profit
sl2 = srcLower - atr * atrMultiplierLower // Calculate Stop Loss
//================================
plotshape(buysign,"buy", shape.triangledown, color = color.green)
// Plot TP and SL lines as dashed lines
if buysign and strategy.opentrades == false //alertcomdition
strategy.entry("long", strategy.long)
line.new(bar_index, low, bar_index, tp2, width=2, color=color.rgb(154, 103, 20), style=line.style_dashed)
tpLine = line.new(bar_index, tp2, bar_index + 3, tp2, width=2, style=line.style_dashed, color=color.rgb(154, 103, 20))
label.new(bar_index + 1, tp2, "TP", color=color.rgb(17, 255, 0, 83), style=label.style_label_left, size=size.tiny, textcolor=color.white)
// Plot SL as dashed line
line.new(bar_index, high, bar_index, sl2, width=2, color=color.rgb(154, 103, 20), style=line.style_dashed)
slLine = line.new(bar_index, sl2, bar_index + 3, sl2, width=2, style=line.style_dashed, color=color.rgb(154, 103, 20))
label.new(bar_index + 1, sl2, "SL", color=color.rgb(255, 0, 0, 90), style=label.style_label_left, size=size.tiny, textcolor=color.white)
strategy.exit("long", stop = sl2, limit = tp2) //alertcomdition
Just use a var
flag to see if you are in a long position. Set it to true
whenever your entry condition is true
and set it to false
whenever your exit condition is true
.
var is_long = false
buysign = not is_long and ta.crossover(close, ta.sma(close, 10)) // Buy signal when close crosses above the 10-period SMA
if (buysign)
is_long := true
if (your_exit_signal)
is_long := false