Skip to content

Moving averages

A moving average smooths a price series. Compare price with an average, or compare a shorter average with a longer one. All functions on this page return a series in the input's units: a moving average of close is a price, not a percentage.

SMA and EMA

sma — simple moving average. Each observation in the window has equal weight. sma(20) averages the latest 20 closes, including the current close. For example, closes of 100, 102 and 104 give sma(3) a current value of 102.

ema — exponential moving average. More recent observations receive more weight. Its standard smoothing factor is 2 / (period + 1). Unlike an SMA, its value also depends on its initial history.

close > sma(20)
ema(20) > ema(50)
ema(20) crosses over ema(50)
sma(high, 20) > sma(low, 20)

The first test compares price with its average. The second checks an ongoing relationship. The third checks only the crossing bar. The fourth explicitly selects high and low prices.

Function Close-based form Explicit input and defaults
sma sma(20) sma(close, 20); period defaults to 20 in sma(close).
ema ema(20) ema(close, 20); period defaults to 20 in ema(close).

Write an input to use the default period; do not write sma() or ema(). Both results support history, such as ema(20)[1]. A 20-bar SMA needs 20 observations for a complete value; an EMA starts from its initial observation, so early readings can still be sensitive to the history supplied.

Other smoothing choices

These use the same function(period) or function(series, period) pattern. Keep the input and lookback fixed when comparing their behaviour.

Function Example Default period with explicit input What changes
wma wma(20) 20 Linearly weights the newest observation most heavily.
dema dema(20) 20 2 × EMA − EMA(EMA); reduces smoothing lag and can overshoot price.
tema tema(20) 20 Combines three successive EMAs; can also overshoot.
hull_ma hull_ma(14) 14 Combines weighted averages at half, full and square-root periods.
kama kama(14) 14 Adjusts smoothing to price's directional efficiency.
alma alma(14) 14 Uses Gaussian weights with default offset 0.85 and width parameter 6.
vidya vidya(14) 14 Adapts its EMA weight using rolling R², with a separate default 14-bar R² window.
sine_weighted_moving_average sine_weighted_moving_average(14) 14 Uses sine-shaped weights across the window.
zero_lag_ema zero_lag_ema(20) 20 Adds the difference between an EMA and its second EMA; equivalent to the DEMA construction here.
zlema zlema(14) 14 Adjusts the input by a lagged price difference before applying an EMA.

zero_lag_ema and zlema are distinct calculations despite their similar names. “Zero lag” is a name, not a guarantee that a signal predicts price.

close > hull_ma(14)
kama(14) > kama(14)[1]
close > alma(14)
vidya(14) > vidya(14)[1]
close > sine_weighted_moving_average(14)
close > zero_lag_ema(20)
close > zlema(14)
close > dema(20) and close > tema(20)
close > wma(20)

Tune an advanced average

Some extra settings follow a column-name argument. TradeScript uses positional calls, so keep that position when changing later settings:

Form Argument order and effect
kama(close, 14, "close", 2, 30) Input, efficiency period, column, fast EMA period, slow EMA period.
alma(close, 14, "close", 0.85, 6) Input, period, column, weight offset, Gaussian width parameter.
vidya(close, 14, "close", 14) Input, EMA period, column, R² period.
ema(close, 20, "close", 0.1) Input, period, column, explicit smoothing factor. This factor overrides the period-derived EMA weight.

When passing high, low or another single series as the input, keep the quoted column argument as "close": that position names the calculation's input column, not another instrument field. Normally the shorter forms are enough; only supply the extra positions when changing those settings.

For a volume-weighted average, see vwma. For a complete moving-average strategy, see moving-average trend.