Skip to content
FactorQX
Pine Scriptbeginnerpine-scriptindicatormoving-average

EMA Crossover Indicator

A minimal, non-repainting EMA crossover indicator in Pine Script v5 that marks where a fast EMA crosses a slow EMA.

1 min

A classic teaching example: plot two exponential moving averages and mark where the fast one crosses the slow one. It demonstrates inputs, the ta.* namespace, and plotshape — without any claim that crossovers are profitable.

ema-crossover.pine
//@version=5
indicator("EMA Crossover (FactorQX)", overlay = true)
 
fastLen = input.int(9, "Fast EMA", minval = 1)
slowLen = input.int(21, "Slow EMA", minval = 1)
 
fast = ta.ema(close, fastLen)
slow = ta.ema(close, slowLen)
 
plot(fast, "Fast EMA", color = color.teal, linewidth = 2)
plot(slow, "Slow EMA", color = color.orange, linewidth = 2)
 
// Confirmed cross signals on closed bars only (no repainting).
bull = ta.crossover(fast, slow)
bear = ta.crossunder(fast, slow)
 
plotshape(bull, "Bull cross", shape.triangleup, location.belowbar, color.teal, size = size.tiny)
plotshape(bear, "Bear cross", shape.triangledown, location.abovebar, color.orange, size = size.tiny)

How it works

ta.ema computes each EMA over the bar's close. ta.crossover and ta.crossunder return true on the bar where the relationship flips. Because they read confirmed series values, the markers do not move after a bar closes.

This is an educational indicator only — it draws shapes, it does not tell you to buy or sell anything.

Provided “as is.” This code is an educational example. Test and validate before any real use; it may omit error handling, security, or edge cases. It is not investment advice or a trading signal. See our disclaimer.