Sidechair

ta.ema()

Browse the reference

Returns the exponential moving average of source over length bars, weighting recent values by a smoothing factor of 2 / (length + 1).

Try it in the editor

ta.ema computes ema = alpha * source + (1 - alpha) * ema[1] with alpha = 2 / (length + 1). Each new bar pulls the average toward source by a fixed fraction, and older values fade without ever dropping out completely.

The first ema value is the simple average of the first length values of source, found on bar number length. After that the recursion takes over. If source goes na, the output is na and the next length valid values seed it again.

Syntax

Pine Script
ta.ema(source: series int/float, length: simple int) -> series float

Arguments

NameTypeDefaultDescription
sourceseries int/floatrequiredSeries of values to smooth.
lengthsimple intrequiredSets the smoothing factor alpha = 2 / (length + 1). A whole number of 1 or more.

Returns

series float: the exponential moving average, or na until length values of source have arrived.

Remarks

  • Because the recursion remembers every earlier bar, the first few hundred values depend slightly on where the chart's history starts. Load more history and early values shift by tiny amounts.
  • ta.ema(x, 1) returns x. Call it on every bar; a call inside a conditional block skips bars and corrupts the recursion.

Example

Pine ScriptEMA with a crossover markerOpen in editor
//@version=6
indicator("EMA cross", overlay = true)
fast = ta.ema(close, 12)
slow = ta.ema(close, 26)
plot(fast, "Fast", color = color.teal)
plot(slow, "Slow", color = color.maroon)
plotshape(ta.crossover(fast, slow), "Up", shape.triangleup, location.belowbar, color.green, size = size.small)

See also

Runs on Sidechair