This example requires an EMA crossover, agreement with a trend EMA, sufficient volume and a confirmed chart bar. Both alert methods use the same conditions as the arrows. The ATR dots are reference prices on signal bars, not trailing stops or orders. The code requests no other timeframe. Its EMA lines still update during an open bar; data-feed revisions can also change history. The default inputs are examples, not recommended settings.
Confirmed EMA signals with matching alertspinescript
//@version=6
indicator("Confirmed EMA Buy/Sell Example", overlay=true)
// --- Input Parameters ---
fastLength = input.int(9, "Fast EMA Length", minval=1)
slowLength = input.int(21, "Slow EMA Length", minval=1)
trendLength = input.int(200, "Trend Filter EMA", minval=1)
atrPeriod = input.int(14, "ATR Period", minval=1)
atrMult = input.float(1.8, "ATR Stop Multiplier", minval=0.5, step=0.1)
volMult = input.float(1.15, "Volume Spike Multiplier", minval=0.5, step=0.05)
// --- Mathematical Calculations ---
emaFast = ta.ema(close, fastLength)
emaSlow = ta.ema(close, slowLength)
emaMacro = ta.ema(close, trendLength)
atrValue = ta.atr(atrPeriod)
volAvg = ta.sma(volume, 20)
volFilter = volume > volAvg * volMult
// --- Trend and Crossover Logic ---
bullishCross = ta.crossover(emaFast, emaSlow)
bearishCross = ta.crossunder(emaFast, emaSlow)
bullishRegime = close > emaMacro
bearishRegime = close < emaMacro
// --- Confirmed Signal Triggers ---
// Finalize current-timeframe signals at bar close.
buySignal = bullishCross and bullishRegime and volFilter and barstate.isconfirmed
sellSignal = bearishCross and bearishRegime and volFilter and barstate.isconfirmed
// --- Signal-bar ATR reference levels (not trailing stops) ---
var float longStop = na
var float shortStop = na
if buySignal
longStop := close - (atrValue * atrMult)
if sellSignal
shortStop := close + (atrValue * atrMult)
// --- Visual Plotting ---
plot(emaFast, "Fast EMA", color=color.new(color.teal, 0), linewidth=2)
plot(emaSlow, "Slow EMA", color=color.new(color.orange, 0), linewidth=2)
plot(emaMacro, "Trend EMA", color=color.new(color.purple, 20), linewidth=2)
plotshape(buySignal, title="Buy Arrow", style=shape.triangleup,
location=location.belowbar, color=color.green, size=size.small, text="BUY")
plotshape(sellSignal, title="Sell Arrow", style=shape.triangledown,
location=location.abovebar, color=color.red, size=size.small, text="SELL")
plot(buySignal ? longStop : na, "Long ATR Reference", color=color.red, style=plot.style_circles, linewidth=2)
plot(sellSignal ? shortStop : na, "Short ATR Reference", color=color.red, style=plot.style_circles, linewidth=2)
// --- Native Bar-Close Alerts ---
if buySignal
alert("BUY Signal Triggered: " + syminfo.ticker + " @ " + str.tostring(close), alert.freq_once_per_bar_close)
if sellSignal
alert("SELL Signal Triggered: " + syminfo.ticker + " @ " + str.tostring(close), alert.freq_once_per_bar_close)
alertcondition(buySignal, "Buy Signal", "Confirmed buy signal on {{ticker}}")
alertcondition(sellSignal, "Sell Signal", "Confirmed sell signal on {{ticker}}")