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
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
ema_length = input.int(21, "EMA Length", minval=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)
upper_band = ema + (ta.atr(ema_length) * atr_multiplier)
lower_band = ema - (ta.atr(ema_length) * atr_multiplier)
// Calculate True Range
tr = math.max(high - low, math.max(math.abs(high - nz(close[1])), math.abs(low - nz(close[1]))))
// Strategy Conditions
bullish_entry = (close < ema) and
(close[1] <= lower_band[1]) and
(close > close[1])
// Calculate Bands
upper_band = ema + (tr * atr_multiplier)
lower_band = ema - (tr * atr_multiplier)
// Execute strategy orders
if bullish_entry
strategy.entry("Long", strategy.long)
// Bullish Signal Condition
bullish_signal = (close < ema
and close[1] <= lower_band[1]
and close > close[1])
// Visual elements (optional)
plot(ema, "EMA", color=color.new(#4A90E2, 0))
plot(upper_band, "Upper", color=color.new(#F5A623, 0))
plot(lower_band, "Lower", color=color.new(#7ED321, 0))
// Plotting
plot(ema, "EMA", color=color.new(#00BFFF, 0))
plot(upper_band, "Upper Band", color=color.new(#FFA500, 0), linewidth=2)
plot(lower_band, "Lower Band", color=color.new(#32CD32, 0), linewidth=2)
// Signal Arrows
plotshape(show_bullish_arrows and bullish_signal ? low : na,
"Bullish Signal",
shape.triangleup,
location.belowbar,
plotshape(show_bulls and bullish_entry ? low : na,
style=shape.triangleup,
location=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)
size=size.small,
title="Bull Signal")