feat: Add bull market filter using 200-period SMA to strategy

This commit is contained in:
Bobby (aider) 2025-02-08 09:23:26 -08:00
parent e54a30c402
commit d325bcaa25

View File

@ -4,6 +4,12 @@ strategy(title="3ATR EMA Basic", shorttitle="3ATR-Basic", overlay=true, currency
// 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
// Calculations
ema = ta.ema(close, ema_length)
@ -11,8 +17,9 @@ atr = ta.atr(ema_length)
upper_band = ema + (atr * atr_multiplier)
lower_band = ema - (atr * atr_multiplier)
// Entry Condition
bullish_entry = (close < ema) and
// Modified Entry Condition (requires bull market)
bullish_entry = bull_market and
(close < ema) and
(close[1] <= lower_band[1]) and
(close > close[1])
@ -26,7 +33,10 @@ if bullish_entry
if bullish_exit
strategy.close("Long")
// Visualization
// Visualization - Add market state background
bgcolor(bull_market ? color.new(color.green, 90) : color.new(color.red, 90), title="Market State")
// Original visualizations
plot(ema, "EMA", color=color.new(#4A90E2, 0))
plot(upper_band, "Upper", color=color.orange)
plot(lower_band, "Lower", color=color.green)