Learn the stock market › Test a strategy › Lesson 1 of 5
What is a backtest?
A rule you can compute
A backtest starts with a rule. "Buy strong stocks" can't be tested, because two people would buy different things. This rule can:
- Own the stock while it closes above its 200-day moving average.
- Sell when it closes below the average, and hold the money without interest until it closes above again.
- Buy and sell at the close on the day after the signal, because the signal only shows once the day is over.
Here it's called the 200-day rule. It's common and easy to follow, so the whole course uses it.
The rule on Walmart, trade by trade
The test walks through every trading day since Oct 17, 2000 and does what the rule says. These were the latest trades:
| Bought | Sold | Price change while held |
|---|---|---|
| Dec 22, 2023 | Apr 7, 2025 | +62.9% |
| Apr 8, 2025 | Apr 9, 2025 | +9.5% |
| Apr 10, 2025 | Jun 2, 2026 | +26.1% |
| Jun 4, 2026 | Jun 26, 2026 | −1.7% |
| Oct 16, 2026 | Oct 21, 2026 | −0.9% |
| Oct 23, 2026 | Still held | +4.7% |
Several trades lasted only a few days. When the price sits close to the average, it crosses back and forth, and every crossing is a buy or a sell.
What the test shows
A backtest always needs something to compare with. The simplest is buying the same stock on the same day and holding it.
| Measure | The 200-day rule | Buy and hold |
|---|---|---|
| Per year | −0.3% | 10.3% |
| Largest fall | −72% | −36% |
| Time in the stock | 67% | 100% |
| Buys and sells | 281 | 1 |
The rule made less than holding the stock, −0.3% against 10.3% a year. It owned the stock 67% of the time and missed part of the rises while it was out.
The largest fall was still bigger with the rule, −72% against −36%. It often sold after a drop and bought back after a rise, and each of those round trips cost a little.
What a backtest is not
A backtest shows what a rule would have made, not what it will make. It is also easy to make a test look better than it was, which is the subject of the next lesson. Whether the rule usually beats holding, on more companies than one, is in the lesson on buy and hold.
Test yourself
What does a backtest need before you can trust it?
More numbers are on the Walmart page.