Skip to main content
EN
Menu

Learn the stock market › Test a strategy › Lesson 1 of 5

What is a backtest?

Short answerA backtest is a test of a trading rule on historical prices. You write the rule so precisely that nothing is left to judgement, let it buy and sell day by day in the past, and work out what it would have made. The rule of owning a stock only while it closes above its 200-day moving average returned −0.3% a year on Walmart since 2000, with 281 buys and sells. Holding the stock the whole time returned 10.3%.

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:

BoughtSoldPrice change while held
Dec 22, 2023Apr 7, 2025+62.9%
Apr 8, 2025Apr 9, 2025+9.5%
Apr 10, 2025Jun 2, 2026+26.1%
Jun 4, 2026Jun 26, 2026−1.7%
Oct 16, 2026Oct 21, 2026−0.9%
Oct 23, 2026Still held+4.7%

The rule buys and sells at the close on the day after the price has crossed the average.

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.

MeasureThe 200-day ruleBuy and hold
Per year−0.3%10.3%
Largest fall−72%−36%
Time in the stock67%100%
Buys and sells2811

Walmart from Oct 17, 2000 to Nov 6, 2026, prices with dividends. Out of the stock, the money earns no interest. Commissions and tax are not included.

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.