Moving averages and trend
What EMA 20 and EMA 50 measure, how to use them to define a trend in writing, and where they fail.
A moving average smooths price by averaging the last several closes, so you can see direction without every daily wiggle. It is the most widely used indicator in systematic trading, and the foundation of the backtest in the Algos demo.
Simple versus exponential
A simple moving average (SMA) gives every close in the window the same weight. A 20-day SMA is the plain average of the last 20 closes. An exponential moving average (EMA) gives more weight to recent closes and less to older ones. For an EMA 20, the newest close carries a weight of about 9.5 percent, calculated as 2 divided by 21, and older closes fade gradually. The result reacts faster to new moves than an SMA of the same length.
Length sets the trade-off. A short average, like EMA 20, follows price closely and turns quickly. A longer one, like EMA 50, is smoother and slower. Neither is better; they answer different questions about the short-term and medium-term direction.
Defining a trend in writing
Traders say 'the trend is up' loosely. An algorithm needs a definition it can check. Moving averages give you several:
- Price above the EMA 50: the medium-term direction is up.
- EMA 20 above EMA 50: recent prices are running ahead of the longer average, a common uptrend definition.
- EMA 50 sloping upward over the last several days: the longer average itself is rising.
- All three together: a stricter definition that triggers less often.
The Algos demo's trend readout uses the second definition, comparing EMA 20 with EMA 50, and shades the periods when a simple crossover rule would have been in the market.
Crossovers
A crossover happens when the faster average crosses the slower one. EMA 20 crossing above EMA 50 is read as a possible start of an uptrend; crossing below, a possible end. A long-only crossover rule buys on the upward cross and exits on the downward cross. It is one of the oldest systematic rules there is, and its strengths and weaknesses are well understood.
Its strength is that it stays in during long trends and stays out during long declines, which can reduce the depth of losses compared with simply holding. Its weakness is lag. Because averages are built from past prices, the crossover always arrives after the move has started, and the exit always arrives after the move has turned. You give up the beginning and end of every trend.
Where moving averages fail
Moving averages work worst in sideways markets. When price chops back and forth in a range, the averages cross repeatedly, each cross triggers a trade, and each trade loses a little to the reversal and to fees. These repeated small losses are called whipsaws. A trend-following rule will typically have many small losing trades and a few large winners. That pattern is normal for the approach and is something you need to be able to sit through.
There is no correct pair of lengths. 20 and 50 are common and reasonable. Searching for the perfect pair on past data is one of the fastest routes to an overfitted strategy, which lesson 6 covers in detail.
Key takeaways
- EMAs weight recent closes more heavily than SMAs and react faster.
- EMA 20 above EMA 50 is a simple, checkable definition of an uptrend.
- Crossover rules stay in long trends but always enter and exit late.
- Sideways markets cause repeated whipsaw losses.
- Do not hunt for perfect lengths on past data.
In the Algos demo, check the trend readout for BTC, ETH and SOL. For each, note whether price is above both EMAs, then count how many shaded in-the-market periods the crossover rule produced in the visible window and how many of them look like whipsaws.
Check the trend readouts