A moving average smooths prices. The is the simple average of the last n closes, the weights newer prices more. Both lag behind the price: they show what was, not what is coming. The longer the period, the calmer the line and the later it reacts.
How the SMA is calculated
Add up the last n closes and divide by n. The next day the oldest price drops out and the new one comes in – the average moves along. Example SMA 3 of 10, 12 and 11: 33 ÷ 3 = 11. If 15 comes in, 10 drops out: (12 + 11 + 15) ÷ 3 = 12.67.
How the EMA is calculated
EMA today = close × k + EMA yesterday × (1 − k), with k = 2 ÷ (n + 1). With n = 3, k = 0.5: EMA 11 and close 15 give 13 – more than the SMA with 12.67, because the new price counts more. In our chart the EMA starts with the SMA of the first n prices; other programs start differently, so early values differ.
Lag is built in
An SMA only turns down when the new close is below the price that drops out. In the model, the SMA 20 only turns 11 candles after the highest close, the EMA 20 after 7. But reacting earlier also means: being fooled by short spikes more often.