What is a moving average?
Raw time-series data is jumpy. Daily sales, a 7-day count of clinic visits, a sensor reading: each point carries some signal and some noise. A moving average trades a little timeliness for a calmer line. Each output value is the arithmetic mean of the last n inputs, where n is the window size you choose.
"Moving" is the part people get wrong. The window does not jump to the next block of n points. It moves one step, so the second window shares n−1 values with the first. Take 10, 12, 8, 14, 11, 9 with a 3-point window:
| Window | Values | Mean |
|---|---|---|
| 1 | 10, 12, 8 | 10.00 |
| 2 | 12, 8, 14 | 11.33 |
| 3 | 8, 14, 11 | 11.00 |
| 4 | 14, 11, 9 | 11.33 |
Six raw points became four smoothed ones. The first n−1 positions have no value because there is not yet a full window. For where this sits among other summaries, see measures of central tendency, and for the wider field, the statistics and probability hub.
Types of moving average
Four versions turn up in practice:
- Simple moving average (SMA): equal weights over the last n values.
- Exponential moving average (EMA): weights that shrink geometrically with age.
- Weighted moving average (WMA): weights that fall in a straight line from newest to oldest.
- Cumulative moving average (CMA): the mean of everything seen so far.
Simple moving average (SMA)
The brackets matter. Without them the division attaches to the last term only and you get a nonsense total. The update form is handy for long series: add the new value, subtract the one that just left, divide by n.
Five closing prices: 23.00, 23.40, 23.20, 24.00, 25.50.
SMA₅ = (23.00 + 23.40 + 23.20 + 24.00 + 25.50) / 5 = 119.10 / 5 = 23.82
Day 6 closes at 24.80, so day 1 (23.00) drops out: 23.82 + (24.80 − 23.00) / 5 = 23.82 + 0.36 = 24.18. Averaging days 2 to 6 directly gives 120.90 / 5 = 24.18, same answer.
✓ SMA₅ = 23.82, then 24.18
One more thing worth knowing: an average sits between the smallest and largest value in its window, not above all of them. The 5-point SMA of 150, 155, 142, 133, 162 is 148.40, which is higher than the 133 inside it.
Exponential moving average (EMA)
The EMA remembers through one number, the previous EMA, so it needs almost no memory. Older observations never drop out; their weight just shrinks by a factor of (1 − α) each step.
The α = 2/(n+1) rule is a convention. It is chosen so that the EMA's average data age matches an n-period SMA (more on that in the lag section). You can pick any α between 0 and 1, and some libraries use 1/n instead.
Data 10, 12, 14, 11, 13 with n = 3, so α = 2/4 = 0.5.
EMA₁ = 10
EMA₂ = 0.5(12) + 0.5(10) = 11.00
EMA₃ = 0.5(14) + 0.5(11) = 12.50
EMA₄ = 0.5(11) + 0.5(12.50) = 11.75
EMA₅ = 0.5(13) + 0.5(11.75) = 12.375
✓ Final EMA₅ = 12.375
Starting at EMA₁ = x₁ means the early values lean hard on the first observation, since nothing else has contributed yet. On a long series the effect washes out. Some tools (pandas with adjust=True) correct for it, and another common fix is to seed with the SMA of the first n points.
Weighted moving average (WMA)
The denominator n(n+1)/2 is just 1 + 2 + … + n, the sum of the weights, so the result stays a proper weighted mean.
Values 10, 12, 14 with weights 1, 2, 3 (oldest to newest):
WMA = (3×14 + 2×12 + 1×10) / 6 = (42 + 24 + 10) / 6 = 76 / 6 = 12.67
✓ WMA = 12.67 versus SMA = 12.00, because the rising newest value counts for more
Cumulative moving average (CMA)
The CMA never drops anything. At step t it is the mean of all t values so far, and it updates as CMA_t = CMA_(t−1) + (x_t − CMA_(t−1)) / t. It suits running quantities such as the defect rate since a line started, and for stable data it settles toward the true mean, as the law of large numbers suggests. It is a poor trend detector because old data keeps full influence forever.
SMA vs EMA vs WMA
| Property | SMA | EMA | WMA |
|---|---|---|---|
| Weights | Equal | Geometric decay | Linear decay |
| Average age of data | (n−1)/2 | (n−1)/2 when α = 2/(n+1) | (n−1)/3 |
| Reaction to newest point | Weakest (1/n) | Strongest (α) | Middle (2/(n+1)) |
| Memory needed | n values | 1 value | n values |
| Cost per step | O(1) with update | O(1) | O(n) |
| Window | Finite | Infinite tail | Finite |
| Good for | Stable long-run trend | Fast-moving data | Finite window, recency bias |
Pick the SMA when you want a plain, explainable number and every point in the window deserves equal say. Pick the EMA when the latest values should pull harder and you want constant-time updates. The WMA fits when you want recency weighting but a hard cutoff.
All three on the same data
Daily readings 15, 17, 14, 18, 20, 16, 22 with a 3-period window (EMA uses α = 0.5):
| Day | Value | SMA(3) | EMA(3) | WMA(3) |
|---|---|---|---|---|
| 1 | 15 | none | 15.00 | none |
| 2 | 17 | none | 16.00 | none |
| 3 | 14 | 15.33 | 15.00 | 15.17 |
| 4 | 18 | 16.33 | 16.50 | 16.50 |
| 5 | 20 | 17.33 | 18.25 | 18.33 |
| 6 | 16 | 18.00 | 17.13 | 17.67 |
| 7 | 22 | 19.33 | 19.56 | 19.67 |
Look at day 5, where the data jumps to 20. The EMA and WMA move up faster than the SMA, then fall back faster on day 6. That extra reactivity is also extra noise.
What causes lag, and why it matters
Every moving average describes the recent past. A useful measure of lag is the average age of the data it uses. For an n-period SMA the ages run from 0 to n−1 with equal weight, so the lag is (n−1)/2. A 10-period SMA sits about 4.5 periods behind.
The EMA with α = 2/(n+1) has an average age of (1 − α)/α, which also works out to (n−1)/2. So a 10-period EMA lags by 4.5 periods too. Many guides claim the EMA "has less lag" and that is only half true: it responds harder to the newest point, but it also drags a long tail of old data. A linearly weighted WMA has an average age of (n−1)/3, so it really does lag less than the SMA (3 periods for n = 10).
The trade-off never goes away. A wider window smooths more and reacts later. A narrow one reacts sooner and passes more noise through. A centered average, which places each mean in the middle of its window, removes the lag but needs future values, so it only works on historical data.
Moving average in Python
These use plain lists and NumPy. The SMA uses the running-sum update instead of re-adding n values each step.
import numpy as np
def sma(data, n):
out = [None] * (n - 1)
s = sum(data[:n])
out.append(s / n)
for i in range(n, len(data)):
s += data[i] - data[i - n]
out.append(s / n)
return out
def ema(data, n):
a = 2 / (n + 1)
out = [data[0]]
for x in data[1:]:
out.append(a * x + (1 - a) * out[-1])
return out
def wma(data, n):
w = np.arange(1, n + 1)
out = [None] * (n - 1)
for i in range(n - 1, len(data)):
out.append(float(w @ data[i - n + 1:i + 1]) / w.sum())
return out
def r(xs):
return [None if v is None else round(v, 4) for v in xs]
data = [15, 17, 14, 18, 20, 16, 22]
print("SMA:", r(sma(data, 3)))
print("EMA:", r(ema(data, 3)))
print("WMA:", r(wma(data, 3)))
# SMA: [None, None, 15.3333, 16.3333, 17.3333, 18.0, 19.3333]
# EMA: [15, 16.0, 15.0, 16.5, 18.25, 17.125, 19.5625]
# WMA: [None, None, 15.1667, 16.5, 18.3333, 17.6667, 19.6667]
To check a result by hand, the mean calculator handles any single SMA window, and the line chart maker will plot raw and smoothed series together.
Where moving averages are used
- Finance: 50-day and 200-day SMAs as trend gauges. A 50-day crossing above the 200-day is called a golden cross, the reverse a death cross.
- Economics: a 4-quarter or 12-month average strips seasonality out of GDP or jobs data.
- Quality control: a rolling defect rate shows process drift before it becomes a recall.
- Public health: a 7-day average of new cases cancels the weekday reporting pattern.
- Climate: 30-year normals are a long-window average.
- Machine learning: smoothing noisy training loss, or keeping running statistics of a feature. See the broader time series analysis page and exploratory data analysis.
One extreme value sits inside an SMA window for n steps and distorts every average that touches it. A moving median resists that, and the outliers guide explains how to spot such points. To see how much a window varies, use its standard deviation or variance.
Frequently asked questions
What is a moving average?
A moving average is the mean of the most recent n observations in a series, recalculated each time a new observation arrives. The window slides forward one step at a time, dropping the oldest value and adding the newest. It smooths short-term noise so the underlying trend is easier to see.
What is the formula for a simple moving average?
SMA_t = (x_t + x_(t−1) + … + x_(t−n+1)) / n, where x_t is the newest value and n is the window size. The whole sum goes in brackets before dividing. The five values 23.00, 23.40, 23.20, 24.00 and 25.50 give 119.10 / 5 = 23.82.
How is an exponential moving average calculated?
EMA_t = α·x_t + (1 − α)·EMA_(t−1), with α = 2/(n+1) for a period-n EMA, starting from EMA₁ = x₁. For α = 0.5 and the data 10, 12, 14: EMA₁ = 10, EMA₂ = 11.00, EMA₃ = 12.50.
What is the difference between SMA and EMA?
An SMA gives every value in its window the same weight and ignores everything older. An EMA gives the newest value the largest weight and lets older values fade geometrically without ever reaching zero. With α = 2/(n+1) both have the same average data age of (n−1)/2 periods, but the EMA reacts more strongly to the latest observations.
What is a weighted moving average?
A WMA gives the newest value weight n, the next n−1, down to 1 for the oldest, then divides by n(n+1)/2. For 10, 12, 14 with a 3-period window it equals (3×14 + 2×12 + 1×10) / 6 = 12.67, compared with an SMA of 12.00.
What causes lag in a moving average?
Lag comes from averaging past data. An n-period SMA is on average (n−1)/2 periods behind the newest point, an EMA with α = 2/(n+1) has the same average age, and a linearly weighted WMA sits about (n−1)/3 periods behind. A wider window smooths more and lags more.
Can a moving average be greater than some of its data points?
Yes. A moving average always lies between the smallest and largest values in its window, so it can sit above the low ones. The 5-period SMA of 150, 155, 142, 133 and 162 is 148.40, which is above 133.
What are the 50-day and 200-day moving averages?
They are simple moving averages of daily closing prices over 50 and 200 trading days. When the 50-day crosses above the 200-day it is called a golden cross, and a cross below is a death cross. Both are lagging signals built from past prices.
Statistics and probability | mean vs median vs mode | weighted average calculator | average calculator