fix: Move stop plots to global scope with conditional visibility

This commit is contained in:
Bobby (aider) 2025-02-08 09:12:25 -08:00
parent 5260a15b6f
commit eea3013e0d

View File

@ -42,19 +42,20 @@ if bullish_entry
strategy.entry("Long", strategy.long, qty=position_size())
// Stop Management
if strategy.position_size > 0
// Update trailing stop
trail_price := math.max(high, nz(trail_price, high))
trail_stop = trail_price * 0.98 // 2% trailing
var float current_stop = na
var float display_max_stop = na
// Active stop is best of: initial 7% stop OR current trailing stop
current_stop = math.max(max_stop_price, trail_stop)
if strategy.position_size > 0
trail_price := math.max(high, nz(trail_price, high))
trail_stop = trail_price * 0.98
current_stop := math.max(max_stop_price, trail_stop)
display_max_stop := max_stop_price
strategy.exit("Stop Exit", "Long", stop=current_stop)
// Plot stops
plot(current_stop, "Active Stop", color=color.red, style=plot.style_linebr)
plot(max_stop_price, "Max Stop", color=color.gray, style=plot.style_circles)
// Move plots to global scope with conditional visibility
plot(strategy.position_size > 0 ? current_stop : na, "Active Stop", color=color.red, style=plot.style_linebr)
plot(strategy.position_size > 0 ? display_max_stop : na, "Max Stop", color=color.gray, style=plot.style_circles)
// Visualization
plot(ema, "EMA", color=color.new(#4A90E2, 0))