Moving averages are the most used indicator in algo trading. Here is how simple and exponential averages differ, and how strategies actually use them.
A moving average smooths price into a single line by averaging the last N bars. It is simple, but almost every trend-following system uses one somewhere.
Simple Moving Average (SMA)
The SMA adds up the last N closes and divides by N. Every bar in the window has equal weight. A 50-period SMA on H1 is the average close of the last 50 hours.
Strengths: smooth and stable, less likely to flip on one spike. Weakness: slow to react, because a bar from 49 hours ago counts as much as the latest one.
Exponential Moving Average (EMA)
The EMA gives more weight to recent prices, with older bars fading exponentially. It reacts faster to new moves.
Strengths: turns earlier when a trend changes. Weakness: more whipsaws in sideways markets.
How algorithms use moving averages
1. Trend filter
Only buy when price is above, say, the 200 EMA, and only sell below it. This single filter often improves a strategy more than any entry trigger, because it stops you fighting the larger move.
2. Crossovers
A fast average crossing a slow one (for example 20 over 50) signals a possible trend change. Crossovers are easy to automate but lag a lot and produce many false signals in ranges. They work best combined with a volatility or trend-strength filter.
3. Pullback entries
In an uptrend, wait for price to pull back to the 20 or 50 EMA and then enter as it turns up. This gives better prices than buying a breakout.
4. Higher-timeframe confirmation
Many systems check the trend on H4 or D1 while trading on M15. Be careful in code here: use the higher-timeframe value from the last closed bar. Using the still-forming bar leaks future information into a backtest.
Choosing the period
There is no magic number. 20, 50 and 200 are popular mainly because many people watch them. More important than the exact period:
- Test a range of values. If 45 works and 50 fails, the result is fragile.
- Match the period to the holding time - short trades need faster averages.
- Remember that every average lags by roughly half its length.
Bottom line
Use SMAs for stable, slow filters and EMAs where reaction speed matters. Most of the value comes from using a moving average as a filter that keeps you on the right side of the trend, not as a stand-alone signal.
Trading forex and gold carries a high level of risk and may not be suitable for every investor. Past performance and backtests do not guarantee future results. This article is education, not financial advice.
