From 9cd98202c7d0c895bd237b8c759c0de972cfe962 Mon Sep 17 00:00:00 2001 From: "Bobby (aider)" Date: Sat, 8 Feb 2025 09:34:54 -0800 Subject: [PATCH] feat: Add dual profit targets and trailing stop to 3ATR EMA strategy --- pinescripts/Three_ATR_EMA.pine | 72 +++++++++++++++++++--------------- 1 file changed, 40 insertions(+), 32 deletions(-) diff --git a/pinescripts/Three_ATR_EMA.pine b/pinescripts/Three_ATR_EMA.pine index 692b446..0ab2873 100644 --- a/pinescripts/Three_ATR_EMA.pine +++ b/pinescripts/Three_ATR_EMA.pine @@ -1,15 +1,12 @@ //@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 ema_length = input.int(21, "EMA Length") atr_multiplier = input.float(3.0, "ATR Multiplier") -bull_market_length = input.int(200, "Bull Market SMA Period") - -// Get daily close prices for market state calculation -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 +profit_target_1 = input.float(10, "First Profit Target (%)") +profit_target_2 = input.float(20, "Second Profit Target (%)") +trail_percent = input.float(2, "Trailing Stop %") // Calculations ema = ta.ema(close, ema_length) @@ -17,40 +14,51 @@ atr = ta.atr(ema_length) upper_band = ema + (atr * atr_multiplier) lower_band = ema - (atr * atr_multiplier) -// Modified Entry Condition (requires bull market) -bullish_entry = bull_market and - (close < ema) and +// Entry Condition (bull market requirement removed) +bullish_entry = (close < ema) and (close[1] <= lower_band[1]) and (close > close[1]) -// Exit Condition (when price crosses upper band) -bullish_exit = close > upper_band +// Target Prices +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 strategy.entry("Long", strategy.long) -if bullish_exit - 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 +// Visuals plot(ema, "EMA", color=color.new(#4A90E2, 0)) plot(upper_band, "Upper", color=color.orange) plot(lower_band, "Lower", color=color.green) -plotshape(bullish_entry ? low : na, - style=shape.triangleup, - location=location.belowbar, - color=color.new(#00FF00, 0), - size=size.small, - title="Bull Entry") +// Plot targets and stops +plot(strategy.position_size > 0 ? target_1 : na, "Target 10%", color=color.gray, style=plot.style_circles) +plot(strategy.position_size > 0 ? target_2 : na, "Target 20%", color=color.gray, style=plot.style_cross) +plot(strategy.position_size > 0 and trail_active ? trail_stop : na, "Trail Stop", color=color.red, style=plot.style_linebr) -plotshape(bullish_exit ? high : na, - style=shape.triangledown, - location=location.abovebar, - color=color.new(#FF0000, 0), - size=size.small, - title="Bull Exit") +// Entry/exit markers +plotshape(bullish_entry, style=shape.triangleup, location=location.belowbar, color=color.green, size=size.small) +plotshape(profit_target_reached, style=shape.xcross, location=location.abovebar, color=color.gray, size=size.small)