On this pageThree formulas
A moving average replaces each price with the average of recent prices. It is one of the oldest indicators and sits inside many others, from MACD to Bollinger Bands. The common question is which average is "best" for swing trading. The useful question is how much delay and how much noise you accept, because every moving average trades one for the other. This guide shows the formulas, puts numbers on the delay, and explains crossovers and the settings that make two platforms disagree.
Three formulas
Simple moving average (SMA)
The SMA of period n is the plain mean of the last n prices. Every price in the window counts equally. One side effect: when an unusually large bar leaves the window n bars later, the average jumps even if the current price did not move.
Exponential moving average (EMA)
Each new EMA value blends the latest price with the previous EMA:
EMA(today) = α × price(today) + (1 − α) × EMA(yesterday), α = 2 / (n + 1)
For n = 20, α is about 0.095, so the newest price carries roughly 9.5% of the weight. Every older price still counts, with weights shrinking geometrically, so nothing drops out of the window all at once. The first value needs a seed, which comes back later as a source of disagreement.
Weighted moving average (WMA)
The linear weighted average gives the newest price weight n, the one before n − 1, and so on down to 1, then divides by the sum of the weights. For n = 5 the weights are 5, 4, 3, 2 and 1, divided by 15.
Wilder's smoothing and the rest
Wilder's smoothing, used inside RSI and ATR, is an EMA with α = 1/n, which equals a standard EMA of period 2n − 1. Hull, double and triple exponential averages combine several averages to cut delay, at the cost of overshooting when prices turn.
Platforms name these differently:
| Average | MetaTrader method | Pine Script | pandas |
|---|---|---|---|
| Simple | Simple (MODE_SMA) |
ta.sma |
rolling(n).mean() |
| Exponential | Exponential (MODE_EMA) |
ta.ema |
ewm(span=n, adjust=False).mean() |
| Linear weighted | Linear Weighted (MODE_LWMA) |
ta.wma |
weighted rolling apply |
| Wilder's | Smoothed (MODE_SMMA) |
ta.rma |
ewm(alpha=1/n, adjust=False).mean() |
Every average lags, by a known amount
An average of past prices is centered in the past. For an SMA of n bars, the average age of the prices it contains is (n − 1)/2 bars. For an EMA with α = 2/(n + 1), the weighted average age works out to the same (n − 1)/2. The WMA, with its heavier recent weights, comes to (n − 1)/3.
| Period | SMA average age | EMA average age | WMA average age |
|---|---|---|---|
| 20 | 9.5 bars | 9.5 bars | 6.3 bars |
| 50 | 24.5 bars | 24.5 bars | 16.3 bars |
| 200 | 99.5 bars | 99.5 bars | 66.3 bars |
If the SMA and the EMA have the same average delay, why do they look so different? Because the weight is distributed differently. A step test shows it: a price sits at 100 for a long time, then jumps to 110 and stays there.
| Bars after the jump | SMA(20) | EMA(20) | WMA(20) |
|---|---|---|---|
| 1 | 100.50 | 100.95 | 100.95 |
| 5 | 102.50 | 103.94 | 104.29 |
| 10 | 105.00 | 106.32 | 107.38 |
| 20 | 110.00 | 108.65 | 110.00 |
The EMA reacts sooner, but its long tail of old prices keeps it short of 110 after 20 bars, when the SMA and the WMA have fully caught up. A shorter period shrinks every number in these tables and lets more of the price noise through. No setting removes delay without adding noise; you choose where to sit between the two.
Crossovers, defined precisely
Two kinds of crossover are common: price crossing an average, and a faster average crossing a slower one. A precise rule for "fast crosses above slow on bar t" is: fast is above slow on bar t, and was at or below it on bar t − 1. Evaluate it on closed bars. On the forming bar, both averages move with every tick, and a cross can appear and vanish several times before the close.
In sideways markets, two averages twist around each other and cross often. In long one-directional moves they cross rarely and late, by roughly the delays in the table above. Named pairs such as the "golden cross" (a 50-day average rising above a 200-day one) are labels for an event in past prices, not forecasts.
Here is a Pine Script v6 sketch that plots two averages and marks crosses only on confirmed bars:
//@version=6
indicator("Two moving averages", overlay = true)
fastLen = input.int(20, "Fast length")
slowLen = input.int(50, "Slow length")
fast = ta.ema(close, fastLen)
slow = ta.sma(close, slowLen)
plot(fast, "Fast EMA", color = color.orange)
plot(slow, "Slow SMA", color = color.gray)
plotshape(ta.crossover(fast, slow) and barstate.isconfirmed, "Cross up", shape.triangleup, location.belowbar, color.green)
plotshape(ta.crossunder(fast, slow) and barstate.isconfirmed, "Cross down", shape.triangledown, location.abovebar, color.red)
Settings that make two platforms disagree
When your average does not match someone else's, check these in order:
- Price source. Close, typical price (high + low + close, divided by 3) and median price give different lines. MetaTrader calls this Apply to.
- Seed and history. An EMA's starting value fades slowly. After k bars the seed still carries a weight of (1 − α) to the power k, which for a 20-period EMA falls under 1% only after about 46 bars, and for a 200-period EMA after about 460. Load several times the period before trusting the values.
- Candle boundaries. A daily average depends on when the day closes. Forex brokers set their own server time, and crypto venues usually close the day at midnight UTC, so "the 20-day average" is not one number.
- Shift. MetaTrader's Shift input moves the line forward by a number of bars. A shifted line looks earlier than it is.
The way bar counts translate into hours and days on each chart is covered in indicator periods by trading timeframe.
Averages inside other indicators
Knowing which average sits inside an indicator explains its delay:
- MACD is the 12-period EMA minus the 26-period EMA, with a 9-period EMA of that difference as its signal line.
- Bollinger Bands are a 20-period SMA with bands two standard deviations above and below.
- Keltner channels surround an EMA with bands set by a multiple of the average true range.
- RSI and ATR use Wilder's smoothing, an EMA in disguise.
FAQ
Which moving average is best for swing trading?
None is best in general. The choice is a trade-off between delay and noise for your timeframe and market, and any choice should be judged on data with costs included, not on how good it looks on a finished chart.
SMA or EMA: which lags less?
With the same period, both have the same average delay of (n − 1)/2 bars. The EMA responds sooner to a fresh move and lets go of old prices more slowly. The WMA has less average delay than both.
Why does my EMA differ from the one on another platform?
Usually the seed and the amount of history loaded, then the price source and the candle times. Give both platforms the same data, the same settings and enough warm-up bars, and the values converge.
What does a moving average cross tell me?
That the recent average of prices moved past a longer average. It describes what prices already did, with a delay you can calculate. It says nothing certain about what they do next.
Trading involves risk of loss. Nothing in this article is financial advice.
Run it on CloudSat. Cloud VPS on AMD EPYC and NVMe from $1.98/mo in 13 regions, delivered in under 5 minutes and paid in crypto. See plans or deploy a server.




