51 lines
1.8 KiB
Plaintext
51 lines
1.8 KiB
Plaintext
//@version=5
|
|
strategy(title="3ATR EMA Upper Band TP", shorttitle="3ATR-UpperBand", 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")
|
|
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
|
|
bullish_entry = (close < ema) and
|
|
(close[1] <= lower_band[1]) and
|
|
(close > close[1])
|
|
|
|
// Variables for managing trade logic
|
|
var float entry_price = na
|
|
var float trail_price = na // Declare trail_price
|
|
var float trail_stop = na
|
|
var bool trail_active = false
|
|
|
|
if bullish_entry
|
|
entry_price := close
|
|
trail_active := false
|
|
trail_price := na // Reset trail_price on a new entry
|
|
|
|
// Exit Conditions
|
|
trail_activation = close >= upper_band
|
|
trail_active := trail_active or trail_activation
|
|
trail_price := trail_active ? math.max(high, nz(trail_price[1], high)) : na
|
|
trail_stop := trail_active ? trail_price * (1 - trail_percent / 100) : na
|
|
|
|
// Exit strategy
|
|
strategy.exit("Upper Band TP", "Long", limit=upper_band, stop=trail_stop)
|
|
|
|
// Entry Execution
|
|
if bullish_entry
|
|
strategy.entry("Long", strategy.long)
|
|
|
|
// Visuals
|
|
plot(ema, "EMA", color=color.new(#4A90E2, 0))
|
|
plot(upper_band, "Upper Band (TP)", color=color.orange)
|
|
plot(lower_band, "Lower Band", color=color.green)
|
|
|
|
// Entry/exit markers
|
|
plotshape(bullish_entry, style=shape.triangleup, location=location.belowbar, color=color.green, size=size.small)
|
|
plotshape(close >= upper_band, style=shape.triangledown, location=location.abovebar, color=color.red, size=size.small, title="Take Profit") |