68 lines
2.5 KiB
Plaintext
68 lines
2.5 KiB
Plaintext
//@version=5
|
|
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")
|
|
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)
|
|
atr = ta.atr(ema_length)
|
|
upper_band = ema + (atr * atr_multiplier)
|
|
lower_band = ema - (atr * atr_multiplier)
|
|
|
|
// Entry Condition (bull market requirement removed)
|
|
bullish_entry = (close < ema) and
|
|
(close[1] <= lower_band[1]) and
|
|
(close > close[1])
|
|
|
|
// Target Prices
|
|
var float entry_price = na
|
|
var float target_1 = na
|
|
var float target_2 = na
|
|
var float trail_stop = na
|
|
var float trail_price = na
|
|
var bool trail_active = false
|
|
|
|
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
|
|
if trail_active and na(trail_price)
|
|
trail_price := high
|
|
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)
|
|
|
|
// Visuals
|
|
plot(ema, "EMA", color=color.new(#4A90E2, 0))
|
|
plot(upper_band, "Upper", color=color.orange)
|
|
plot(lower_band, "Lower", color=color.green)
|
|
|
|
// 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)
|
|
|
|
// 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)
|