//@version=5 strategy("21 EMA Scalping Continuation Strategy", overlay=true, initial_capital=10000, default_qty_type=strategy.percent_of_equity, default_qty_value=10) // Strategy Inputs emaLength = input.int(21, title="EMA Period") rrRatio = input.float(1.5, title="Risk to Reward Ratio", step=0.1) maxBars = input.int(3, title="Max Bars for Pullback", minval=1, maxval=5) // Technical Indicators emaVal = ta.ema(close, emaLength) plot(emaVal, color=color.blue, linewidth=2, title="21 EMA") // Persistent State Variables var int longState = 0 // Counts bars after bullish EMA crossover var float longLevel = na // Entry high level var float longSL = na // Stop loss level var int shortState = 0 // Counts bars after bearish EMA crossunder var float shortLevel = na // Entry low level var float shortSL = na // Stop loss level // Cross Signals bullCross = ta.crossover(close, emaVal) bearCross = ta.crossunder(close, emaVal) // --- BULLISH SETUP LOGIC --- if bullCross longState := 1 longLevel := na longSL := na else if longState > 0 if close < emaVal or longState > maxBars longState := 0 longLevel := na longSL := na else longState := longState + 1 // Identify opposite color candle (red candle) during active bullish state if longState > 0 and close < open longLevel := high longSL := low // Execute Long Orders if longState > 0 and not na(longLevel) and strategy.position_size == 0 risk = longLevel - longSL if risk > 0 tpLevel = longLevel + (risk * rrRatio) strategy.entry("EMA_Long", strategy.long, stop=longLevel) strategy.exit("Exit_Long", "EMA_Long", stop=longSL, limit=tpLevel) // Reset long state upon position entry if strategy.position_size > 0 longState := 0 longLevel := na longSL := na // --- BEARISH SETUP LOGIC --- if bearCross shortState := 1 shortLevel := na shortSL := na else if shortState > 0 if close > emaVal or shortState > maxBars shortState := 0 shortLevel := na shortSL := na else shortState := shortState + 1 // Identify opposite color candle (green candle) during active bearish state if shortState > 0 and close > open shortLevel := low shortSL := high // Execute Short Orders if shortState > 0 and not na(shortLevel) and strategy.position_size == 0 risk = shortSL - shortLevel if risk > 0 tpLevel = shortLevel - (risk * rrRatio) strategy.entry("EMA_Short", strategy.short, stop=shortLevel) strategy.exit("Exit_Short", "EMA_Short", stop=shortSL, limit=tpLevel) // Reset short state upon position entry if strategy.position_size < 0 shortState := 0 shortLevel := na shortSL := na