MathsStatistics & Probability

Moving Average
calculator.

A rolling average smooths the noise in a series so its direction shows.

How it's worked out

A moving average replaces each value with the average of a window of recent values, so short-term noise cancels out. NIST describes it as taking the mean of a small set of past data, then moving on one period: add the newest value and drop the oldest. This tool works out three kinds side by side. It suits any evenly spaced series, such as weekly sales, daily temperatures or closing prices.

SMAt\displaystyle \text{SMA}_t=xt+xt−1+⋯+xt−n+1n\displaystyle {}= \frac{x_t + x_{t-1} + \cdots + x_{t-n+1}}{n}

Here n is the window and xtx_t is the value at period t. The simple average weights each value in the window equally. A window of 1 is the data itself, and a longer window gives a smoother line.

top\displaystyle \text{top}=nxt+(n−1)xt−1+⋯+xt−n+1\displaystyle {}= n x_t + (n - 1) x_{t-1} + \cdots + x_{t-n+1}
WMAt\displaystyle \text{WMA}_t=topn(n+1)2\displaystyle {}= \frac{\text{top}}{\frac{n(n + 1)}{2}}

The weighted average gives the newest value a weight of n and the oldest a weight of 1. The bottom of the fraction is the triangle number of n, so the weights still come to 1 in total, and the line reacts faster.

EMAt\displaystyle \text{EMA}_t=αxt+(1−α)EMAt−1\displaystyle {}= \alpha x_t + (1 - \alpha) \text{EMA}_{t-1}
α\displaystyle \alpha=2n+1\displaystyle {}= \frac{2}{n + 1}

The exponential average uses every past value, with weights that shrink by the factor \(1 - \alpha\) at each step back. The usual choice of α comes from the window: 18.18% for 10 periods and 9.52% for 20. With that α, the average age of the data, 1−αα\frac{1 - \alpha}{\alpha}, equals n−12\frac{n - 1}{2}, the same as the simple window. Here the first EMA is the first full simple average, as StockCharts does it. NIST lists other starts, such as the first value, and a different start gives different early figures.

Worked examples

Six weeks of values and a window of 3

Take the values 20, 22, 21, 25, 27, 26, a window of 3 and α = 0.5. The first simple average uses the first three values.

20+22+213\displaystyle \frac{20 + 22 + 21}{3}=21\displaystyle {}= 21

The simple averages are 21, 22.67, 24.33, 26. The first weighted average gives the newest value a weight of 3, and the weighted averages are 21.17, 23.17, 25.33, 26.17.

3×21+2×22+1×206\displaystyle \frac{3 \times 21 + 2 \times 22 + 1 \times 20}{6}=21.17\displaystyle {}= 21.17

The exponential series is 21, 23, 25, 25.5. At the end, the simple average is 26, the weighted 26.17 and the exponential 25.5. The weighted one sits highest because it counts the 27 in the middle of the last window more than the 25 before it.

No average exists for the first two periods, because a 3-period window needs three values. Every simple series is shorter than the data by one less than the window.

Open this example in the tool

A price that rises and then falls

This price peaks at period 5 and then slides. With windows of 3 and 5, the short average crosses below the long one at period 8, marking the change of direction 3 periods after the peak.

That delay cannot be avoided, because any average of past values reacts late. The latest simple average is 47.33, and the one-step error is 12.37 for the simple average and 10.5 for the exponential one.

Open this example in the tool

Two weeks of daily temperatures

A 7-day window suits daily data with a weekly pattern. The latest 7-day average here is 20.7°, against 21° for the exponential average. The newest day counts for 14.3% of the simple average and 25% of the exponential one, which is why the exponential line moves more on each new reading.

The middle of the 7-day window is 3 days back. Comparing windows on these 14 days, window 6 has the smallest one-step error of the simple average, 9.5. That is a choice for prediction; a 7-day window can still be the right one for removing the weekly pattern.

Open this example in the tool

Reading your result

Crossovers. A crossover happens when a short average moves above or below a long one. StockCharts calls the signal relatively late and warns that it is prone to whipsaws, where the lines cross back and forth without a strong trend.

One-step error. Each average predicts the next value, and the tool reports the mean squared miss. A lower number means the method tracks the series more closely, which helps when you choose a window or α. On the sales preset, a window of 2 gives 5.65 and a window of 3 gives 8.85. On a steadily rising series, the shortest window usually wins, since it lags least.

Choosing a window. A larger window means a smoother curve, and it lags more. To remove a seasonal pattern, match the cycle: 7 for daily data with a weekly pattern. For an even cycle such as 12 months, Hyndman and Athanasopoulos use a 2 by 12 centered average, because a plain even window is not symmetric. This tool's windows trail the data and are not centered, so their line sits behind the series.

Common mistakes

  • Treating it as a forecast of turns. Moving averages describe the past. They do not predict turning points, and crossover rules in trading carry no guarantee of profit.
  • Using unevenly spaced data. The method assumes evenly spaced values, so fill or note any missing periods first.
  • Extending a flat forecast through a trend. The next-period forecast from simple exponential smoothing is flat, which suits a series with no trend or season. Series with both are better handled by methods such as Holt-Winters.
  • Comparing EMAs from different tools. They can start from different values. The effect of the start fades as the series grows, but it matters more with a small α or a short series.

Questions people ask

A simple average weights each value in the window equally, and it drops the oldest value completely when a new one arrives. An exponential average uses every past value with weights that shrink at each step, so there is no sudden drop-off. It also reacts sooner, since the newest value counts for more.

Sources