feat: Add trailing stop loss to Three ATR EMA strategy
This commit is contained in:
parent
7155686923
commit
e0ad1ac620
@ -1,10 +1,11 @@
|
|||||||
//@version=5
|
//@version=5
|
||||||
strategy(title="Three ATR EMA Strategy", shorttitle="3ATR-EMA Strategy", overlay=true, currency=currency.USD, initial_capital=10000, default_qty_type=strategy.percent_of_equity, commission_type=strategy.commission.percent, commission_value=0.1)
|
strategy(title="Three ATR EMA Strategy w/ Trailing", shorttitle="3ATR-EMA Trailing", overlay=true, currency=currency.USD, initial_capital=10000, default_qty_type=strategy.percent_of_equity, commission_type=strategy.commission.percent, commission_value=0.1)
|
||||||
|
|
||||||
// Inputs
|
// Inputs
|
||||||
ema_length = input.int(21, "EMA Length", minval=1)
|
ema_length = input.int(21, "EMA Length", minval=1)
|
||||||
atr_multiplier = input.float(3.0, "ATR Multiplier", step=0.1)
|
atr_multiplier = input.float(3.0, "ATR Multiplier", step=0.1)
|
||||||
show_bulls = input.bool(true, "Show Bullish Markers")
|
show_bulls = input.bool(true, "Show Bullish Markers")
|
||||||
|
trail_percent = input.float(2.0, "Trailing Stop %", step=0.1)
|
||||||
|
|
||||||
// Calculate EMA and ATR-based bands
|
// Calculate EMA and ATR-based bands
|
||||||
ema = ta.ema(close, ema_length)
|
ema = ta.ema(close, ema_length)
|
||||||
@ -16,15 +17,39 @@ bullish_entry = (close < ema) and
|
|||||||
(close[1] <= lower_band[1]) and
|
(close[1] <= lower_band[1]) and
|
||||||
(close > close[1])
|
(close > close[1])
|
||||||
|
|
||||||
// Execute strategy orders
|
// Trailing Stop Logic
|
||||||
|
var float trail_price = na
|
||||||
|
var bool trail_active = false
|
||||||
|
|
||||||
if bullish_entry
|
if bullish_entry
|
||||||
strategy.entry("Long", strategy.long)
|
strategy.entry("Long", strategy.long)
|
||||||
|
trail_price := na // Reset on new entry
|
||||||
|
trail_active := false
|
||||||
|
|
||||||
// Visual elements (optional)
|
if strategy.position_size > 0
|
||||||
|
// Activate trailing when price hits upper band
|
||||||
|
if close >= upper_band
|
||||||
|
trail_active := true
|
||||||
|
|
||||||
|
// Update trailing price when active
|
||||||
|
if trail_active
|
||||||
|
trail_price := math.max(high, nz(trail_price, high))
|
||||||
|
|
||||||
|
// Calculate stop price
|
||||||
|
stop_price = trail_active ? trail_price * (1 - trail_percent/100) : na
|
||||||
|
|
||||||
|
// Submit exit order
|
||||||
|
if trail_active
|
||||||
|
strategy.exit("Trail Exit", "Long", stop=stop_price)
|
||||||
|
|
||||||
|
// Visual elements
|
||||||
plot(ema, "EMA", color=color.new(#4A90E2, 0))
|
plot(ema, "EMA", color=color.new(#4A90E2, 0))
|
||||||
plot(upper_band, "Upper", color=color.new(#F5A623, 0))
|
plot(upper_band, "Upper", color=color.new(#F5A623, 0))
|
||||||
plot(lower_band, "Lower", color=color.new(#7ED321, 0))
|
plot(lower_band, "Lower", color=color.new(#7ED321, 0))
|
||||||
|
|
||||||
|
// Plot trailing stop line
|
||||||
|
plot(strategy.position_size > 0 and trail_active ? stop_price : na, "Trailing Stop", color=color.red, style=plot.style_linebr)
|
||||||
|
|
||||||
plotshape(show_bulls and bullish_entry ? low : na,
|
plotshape(show_bulls and bullish_entry ? low : na,
|
||||||
style=shape.triangleup,
|
style=shape.triangleup,
|
||||||
location=location.belowbar,
|
location=location.belowbar,
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user