feat: Add dual profit targets and trailing stop to 3ATR EMA strategy

This commit is contained in:
Bobby (aider) 2025-02-08 09:34:54 -08:00
parent d325bcaa25
commit 9cd98202c7

View File

@ -1,15 +1,12 @@
//@version=5 //@version=5
strategy(title="3ATR EMA Basic", shorttitle="3ATR-Basic", overlay=true, currency=currency.USD, initial_capital=10000, commission_type=strategy.commission.percent, commission_value=0.1) strategy(title="3ATR EMA with Targets", shorttitle="3ATR-Target", overlay=true, currency=currency.USD, initial_capital=10000, commission_type=strategy.commission.percent, commission_value=0.1)
// Inputs // Inputs
ema_length = input.int(21, "EMA Length") ema_length = input.int(21, "EMA Length")
atr_multiplier = input.float(3.0, "ATR Multiplier") atr_multiplier = input.float(3.0, "ATR Multiplier")
bull_market_length = input.int(200, "Bull Market SMA Period") profit_target_1 = input.float(10, "First Profit Target (%)")
profit_target_2 = input.float(20, "Second Profit Target (%)")
// Get daily close prices for market state calculation trail_percent = input.float(2, "Trailing Stop %")
daily_close = request.security(syminfo.tickerid, "D", close)
bull_market_sma = ta.sma(daily_close, bull_market_length)
bull_market = daily_close > bull_market_sma
// Calculations // Calculations
ema = ta.ema(close, ema_length) ema = ta.ema(close, ema_length)
@ -17,40 +14,51 @@ atr = ta.atr(ema_length)
upper_band = ema + (atr * atr_multiplier) upper_band = ema + (atr * atr_multiplier)
lower_band = ema - (atr * atr_multiplier) lower_band = ema - (atr * atr_multiplier)
// Modified Entry Condition (requires bull market) // Entry Condition (bull market requirement removed)
bullish_entry = bull_market and bullish_entry = (close < ema) and
(close < ema) and
(close[1] <= lower_band[1]) and (close[1] <= lower_band[1]) and
(close > close[1]) (close > close[1])
// Exit Condition (when price crosses upper band) // Target Prices
bullish_exit = close > upper_band var float entry_price = na
var float target_1 = na
var float target_2 = na
var float trail_stop = na
var bool trail_active = false
// Trade Execution if bullish_entry
entry_price := close
target_1 := entry_price * 1.10 // 10% profit
target_2 := entry_price * 1.20 // 20% profit
trail_active := false
// Exit Conditions
trail_activation = close >= upper_band
profit_target_reached = close >= target_2
// Trailing stop logic (activated by upper band touch)
trail_active := trail_active or trail_activation
trail_price = math.max(high, nz(trail_price[1], high))
trail_stop := trail_price * (1 - trail_percent/100)
// Exit strategy
strategy.exit("First Profit", "Long", qty_percent=50, limit=target_1)
strategy.exit("Second Exit", "Long", stop=trail_active ? trail_stop : na, limit=target_2)
// Entry Execution
if bullish_entry if bullish_entry
strategy.entry("Long", strategy.long) strategy.entry("Long", strategy.long)
if bullish_exit // Visuals
strategy.close("Long")
// Visualization - Add market state background
bgcolor(bull_market ? color.new(color.green, 90) : color.new(color.red, 90), title="Market State")
// Original visualizations
plot(ema, "EMA", color=color.new(#4A90E2, 0)) plot(ema, "EMA", color=color.new(#4A90E2, 0))
plot(upper_band, "Upper", color=color.orange) plot(upper_band, "Upper", color=color.orange)
plot(lower_band, "Lower", color=color.green) plot(lower_band, "Lower", color=color.green)
plotshape(bullish_entry ? low : na, // Plot targets and stops
style=shape.triangleup, plot(strategy.position_size > 0 ? target_1 : na, "Target 10%", color=color.gray, style=plot.style_circles)
location=location.belowbar, plot(strategy.position_size > 0 ? target_2 : na, "Target 20%", color=color.gray, style=plot.style_cross)
color=color.new(#00FF00, 0), plot(strategy.position_size > 0 and trail_active ? trail_stop : na, "Trail Stop", color=color.red, style=plot.style_linebr)
size=size.small,
title="Bull Entry")
plotshape(bullish_exit ? high : na, // Entry/exit markers
style=shape.triangledown, plotshape(bullish_entry, style=shape.triangleup, location=location.belowbar, color=color.green, size=size.small)
location=location.abovebar, plotshape(profit_target_reached, style=shape.xcross, location=location.abovebar, color=color.gray, size=size.small)
color=color.new(#FF0000, 0),
size=size.small,
title="Bull Exit")