Introduction to Quant Finance · Lesson 21
What is a backtest?
One rule, five made-up markets. What a backtest does, and the three things to look at in one.
Know the basics? Read the lesson notesA market and a rule
A market is anything with a price you can buy and sell, like a share in a company. Each day the price ends a little higher or a little lower than the day before. A day it ends lower is a down day. A day it ends higher is an up day.
A trading rule is a fixed instruction for when to buy and when to sell. Here is the one we'll use:
Below are the first 24 days of a market we made up, called market A. Tap the day the rule first buys.
Made-up market A, days 1 to 24 (simulated, not a real market)
Each square is one day. Filled: a down day. Empty: an up day.
Days 18, 19 and 20 are all down, so the rule buys at the end of day 20. Earlier on, days 1 and 2, 12 and 13, and 15 and 16 were down too, but only two in a row each time.
One trade
The buy and the sell together make one trade. Each trade spends $10,000 buying at the buy and gets back whatever that has become at the sell. What came back minus what went in is the trade's result.
This trade bought at the end of day 20 and sold at the end of day 25. Of the 5 days it held, 4 were down. A square shows which way a day went, not how far.
Made-up market A, days 18 to 25 (simulated)
Filled: down day. Empty: up day. Ringed: the buy and the sell. Orange bar underneath: the 5 days the trade is held, 21 to 25.
Did this trade make money or lose money?
The backtest walks on, day by day
A backtest checks a rule on past days before you risk real money. Here we invent the history, so we know exactly what is in it: random days with a tiny steady rise built in, nothing the rule could know in advance. A backtest runs the rule over past days as if you had traded back then. It moves forward one day at a time, using only the days so far, and writes every trade into a list: the trade list.
Press Play to run the first 48 days. Watch days 21 to 23: that's the rule's last part, one trade at a time, at work.
The rule on made-up market A, days 1 to 48 (simulated)
Filled: down day. Empty: up day. Ringed: a buy or a sell. Orange bar underneath: the days a trade is held, from the day after the buy to the sell day.
| Trade | Bought (day) | Sold (day) | Result |
|---|---|---|---|
| No trades yet | |||
Three trades in 48 days, and all three lost. Days 21 to 23 were three more down days in a row, but a trade was already open, so the rule waited.
A sell day can also be a down day: day 25 ended the first trade and counts toward the next run of three (25, 26, 27).
The backtest doesn't stop at day 48. Our made-up market runs for 10 years: 2,520 trading days.
Will this account end up or down?
Now give the rule an account that keeps score. It starts at $10,000. Each time a trade closes, the trade's result is added. After the three trades you just watched, it's at $9,295: $10,000 − $292 − $296 − $117.
Every trade puts in the same $10,000, whatever the account shows, so the account is only a running score: it adds up the results. The backtest will now run all 10 years. Where do you think the account ends?
The account line
Press Play. As each trade closes, its result is added to the account, and the chart draws the account after every trade. That line is the account line.
Made-up market A, 10 years: the account after each trade (simulated)
Account in dollars. The dashed line is the $10,000 start. A made-up market, not a real one.
| Trade | Bought (day) | Sold (day) | Result | Account |
|---|---|---|---|---|
| Press Play | ||||
The end number alone would hide that early dip. The account line shows the whole way there.
One market, or five?
So far, one made-up market. We made four more, B to E, in exactly the same way, and ran the same rule on each: 3 down days in a row, sell 5 days later.
Market A ended at $14,662. How many of the five markets end above $10,000?
Five markets
Each small chart is one market's account line over the same 10 years, drawn to the same scale. Press Run.
The same rule on five made-up markets (simulated)
Account after each trade, 10 years. Dashed: the $10,000 start.
The catch: we made these markets random
We built each market by drawing every day's move at random, like a roll of dice, with a small rise built in: 0.03% a day on average. A typical day moves about 1.2% up or down, 40 times that rise. Today's move has nothing to do with yesterday's, so three down days in a row say nothing about the next five. There is no pattern here for any rule to find.
Then why did four end higher? Every day a trade is open, it collects that built-in rise: $10,000 × 0.03% × 5 days ≈ $15 a trade. Over about 140 trades a market on average, that is about 140 × $15 = $2,100. Press the button to compare it with what the five made.
Five made-up markets, one chart (simulated)
Grey: each market's account line. The dashed line is the $10,000 start.
The rule found nothing. It collected the rise, and each market's own random days pushed it up or down from there.
So why did B lose half?
Market B ended at $4,923, about half of its $10,000 gone. Market C ended at $16,302. Why so different?
The recipe of every backtest
Every backtest has the same parts:
- Historical data: the past prices it runs on.
- A trading rule with an entry (when to buy) and an exit (when to sell).
- A position size: how much each trade puts in.
- Costs: fees, and the small amount lost by trading at a slightly worse price than shown.
- The results: the trade list, the account line (traders call it the equity curve) and summary numbers such as the trade count.
Two formulas follow. The first is the result of one trade, r: a dollar amount, not a percentage. The second is the account after k trades, Ak: the start plus every result so far. It just adds results, with no compounding, because S stays fixed. The part Psell ÷ Pbuy − 1 is the trade's percentage return: how much the price moved, as a share of the buy price.
- r
- the result of one trade, in dollars (not a percentage)
- S
- the position size: dollars put into the trade
- Pbuy, Psell
- the price at the buy and at the sell
- c
- the costs of that trade (zero in this lesson)
- A0
- the account at the start
- r1 … rk
- the first trade's result, the second's, and so on up to the k-th
- Ak
- the account after k trades: one point of the account line
Market A's first trade: S = $10,000 and $9,708 came back, so Psell ÷ Pbuy = 9,708 ÷ 10,000 = 0.9708 (results are rounded to whole dollars). With c = 0: r = 10,000 × (0.9708 − 1) − 0 = −$292.
After three trades: A3 = 10,000 − 292 − 296 − 117 = $9,295.
With real prices and costs (made-up numbers): S = $2,000, buy at $50, sell at $52, c = $4. The percentage return is 52 ÷ 50 − 1 = 0.04 (4%), so r = 2,000 × 0.04 − 4 = 80 − 4 = $76.
- each trade could really fill at the price shown;
- each trade puts in the same S, however the account stands;
- the costs are what they would really be;
- the rule uses only what was known on that day, nothing from later.
The rule uses information from later days (look-ahead bias, lesson B2). Real trading costs are higher than the backtest set (Costs (lesson A16)). A good result came from luck, or from trying many versions and keeping the best (The best of many (lesson B7)).
Which part of the recipe is missing?
What to look at in any backtest
Every backtest you'll ever see comes down to three things. Turn them over one at a time.
- The trade list.
Every trade: when it bought, when it sold, what it made or lost. Everything else is added up from it. It belongs to one exact rule: change one setting, such as the 3, and you get a different list.
- The account line.
The account after each trade. It shows the way there, not only the end: market A fell to $8,817 in its first year, climbed to $15,971 a little after year 6, dropped to $12,589 about a year later, and ended at $14,662.
- The set of markets.
Every market the rule ran on, not only the best one. Real tests use different shares and different years.
Everything later in this course questions one of these three.
A friend's backtest
What's the most useful thing to ask?
One trade, with costs
What is that trade's result r? Type it in dollars.
Hint: first the percentage return, 42 ÷ 40 − 1. Multiply it by S, then take away c.
Check it by hand
On which days does the rule buy?
Lesson done
A backtest replays a rule on past days, trade by trade, as if you had traded back then. It leaves three things to look at: the trade list, the account line, and every market it ran on. Every later lesson questions one of those three.
Key ideas
- Market
- anything with a price you can buy and sell
- Down day / up day
- a day the price ends lower / higher than the day before
- Trading rule
- a fixed instruction: an entry (when to buy) and an exit (when to sell)
- Trade and result
- one buy and one sell; what came back minus what went in
- Position size
- how much each trade puts in ($10,000 here)
- Costs
- fees and worse-than-shown prices; zero in this lesson
- Percentage return
- Psell ÷ Pbuy − 1: the price move as a share of the buy price
- Backtest
- a rule run over past days as if you had traded then, using only the days so far
- Trade list
- every trade the backtest took
- Account line
- the account after each trade (traders call it the equity curve)
- Set of markets
- every market the rule ran on, read together
r = S × (Psell ÷ Pbuy − 1) − c
Ak = A0 + r1 + … + rk