refactor: Convert Three ATR EMA indicator to strategy with backtest params

This commit is contained in:
Bobby (aider) 2025-02-08 08:51:15 -08:00
parent 3794e0b21e
commit 7155686923

View File

@ -1,42 +1,33 @@
//@version=5 //@version=5
indicator(title="Three ATR EMA Bands", shorttitle="3ATR EMA", overlay=true) 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)
// 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_bullish_arrows = input.bool(true, "Show Bullish Signals") show_bulls = input.bool(true, "Show Bullish Markers")
// Calculate EMA // Calculate EMA and ATR-based bands
ema = ta.ema(close, ema_length) ema = ta.ema(close, ema_length)
upper_band = ema + (ta.atr(ema_length) * atr_multiplier)
lower_band = ema - (ta.atr(ema_length) * atr_multiplier)
// Calculate True Range // Strategy Conditions
tr = math.max(high - low, math.max(math.abs(high - nz(close[1])), math.abs(low - nz(close[1])))) bullish_entry = (close < ema) and
(close[1] <= lower_band[1]) and
(close > close[1])
// Calculate Bands // Execute strategy orders
upper_band = ema + (tr * atr_multiplier) if bullish_entry
lower_band = ema - (tr * atr_multiplier) strategy.entry("Long", strategy.long)
// Bullish Signal Condition // Visual elements (optional)
bullish_signal = (close < ema plot(ema, "EMA", color=color.new(#4A90E2, 0))
and close[1] <= lower_band[1] plot(upper_band, "Upper", color=color.new(#F5A623, 0))
and close > close[1]) plot(lower_band, "Lower", color=color.new(#7ED321, 0))
// Plotting plotshape(show_bulls and bullish_entry ? low : na,
plot(ema, "EMA", color=color.new(#00BFFF, 0)) style=shape.triangleup,
plot(upper_band, "Upper Band", color=color.new(#FFA500, 0), linewidth=2) location=location.belowbar,
plot(lower_band, "Lower Band", color=color.new(#32CD32, 0), linewidth=2) color=color.new(#00FF00, 0),
size=size.small,
// Signal Arrows title="Bull Signal")
plotshape(show_bullish_arrows and bullish_signal ? low : na,
"Bullish Signal",
shape.triangleup,
location.belowbar,
color=color.new(#00FF00, 0),
size=size.small)
// Alerts
alertcondition(bullish_signal, "Bullish Signal Triggered", "3ATR EMA Bullish Signal")
// Strategy Calculations for Visual Reference
if (bullish_signal)
strategy.entry("Bullish", strategy.long)