Quant from Scratch by QuantWright

Backtesting Essentials · Lesson 16

From backtest to forward test

Days that did not exist when the rule was fixed: what a forward test can show, why a rule picked as the best of several backtests tends to do worse there, and how to decide in advance what would make you stop.

Know the basics? Read the lesson notes

Twenty rules, eight years

As in earlier lessons, a backtest runs a trading rule over past prices to see what it would have made. Here the prices are simulated: one made-up market, 2015 to 2024. You stand at the end of 2022, with eight years of prices.

Each day a rule is in the market or out. In: $100 goes in at the day's first price and comes out at its last. No trading costs are taken off, so every result here is before costs. A rule's lead is its result minus the result of just holding (being in every day) over the same days (lesson B9).

Each evening this market publishes a made-up number, the reading, usually between about −2 and +2. We built it so the next day's move tends to go a little its way: each unit of the reading adds 0.10% to that move, where a typical day moves about 1.03% up or down. You also have 19 more made-up readings that push nothing. Rule 1 is in the market the day after the market's reading was above 0; rules 2 to 20 do the same with the 19 others. Backtest all twenty, and keep the best: the pick.

20 rules on 2015–2022 (simulated, before costs)

Dots: each rule's lead a year. Colour: the pick. Dashed: just holding.

The pick is rule 1, the one that reads the market's reading. It made $58 where just holding lost $61: a lead of 58 + 61 = $119 over eight years, or 119 ÷ 8 = $14.88 a year, before costs.

It is the best of 20 backtests, and the best of many tries is partly luck (lesson B7).

Days that did not exist

At the end of 2022 the pick is fixed: no setting will change. A forward test runs the rule, unchanged, on new days as they arrive (lesson B10's words).

A held-out test (lesson B10) runs the rule once on past days set aside before the work: those days existed, so someone could have looked at them. Forward days did not exist when the rule was fixed, so nobody could have looked at them, tuned on them or picked with them.

Which of these is a forward test of the pick?

That is a held-out test. The days were set aside, but they existed while you worked, so they could have been looked at. Forward days, 2023 and after, did not exist when the rule was fixed.
2023 and after did not exist when the rule was fixed at the end of 2022, so nothing in the rule can have come from them.
Those are the days the pick was chosen on, so a result there repeats part of the backtest. A forward test uses days that did not exist when the rule was fixed: 2023 and after.

Will it match the backtest?

Now let 2023 and 2024 happen, with the pick unchanged. In its backtest it led by $14.88 a year. Guess first: over the two forward years, what will its lead a year be?

The pick's lead, added up day by day (simulated, before costs)

Dark: the backtest. Colour: the forward test. Dashed: the backtest's pace, carried on.

Your guess:

Over 2023–2024 the pick made $26 and just holding lost $2: a lead of 26 + 2 = $28, or 28 ÷ 2 = $14 a year, before costs.

That is one market. On 1,000 more markets built the same way, the pick's lead averaged $4.71 a year in the backtest and $2.73 in the forward test, before costs.

Why lower: the pick is partly luck

A pick's backtest is the best of several tries, so part of its lead is luck that new days don't repeat. To see how much, take the same 1,000 markets twice: with the reading's built-in nudge (call it the push), and with the push removed (the same prices otherwise). Without the push no rule has anything real, yet a rule out of the market about half the days still trails just holding by about $4 a year here, because these markets rise on average, so sitting out about half the days misses about half the rise. The figure opens on "With the push"; switch to see the other.

Average lead a year, 1,000 markets (simulated, before costs)

Dark: the backtest, 2015–2022. Colour: the forward test, 2023–2024.

With the push, the pick's lead fell from $4.71 a year in the backtest to $2.73 forward. Rule 1 on its own, never chosen from the twenty, averaged $4.37 and $4.33: about the same, because nothing chose it for a lucky backtest. The fall comes from the picking: in some markets the pick was a rule with nothing real in it and a lucky backtest.

Without the push no rule has anything real. The pick still averaged $1.53 a year in the backtest, and then trailed just holding by $3.89 a year forward: the sitting-out handicap, not luck.

So when a rule was picked as the best of several on the same backtest, expect its forward lead to be lower than its backtest's on average, though not every time: with the push it was higher in 423 of the 1,000 markets.

For the preciseWith the push the pick was rule 1 in 812 of the 1,000 markets (a blind draw from 20 would get it in about 50), and a rule with nothing real in it in the other 188.

Decide when to stop, before you look

A forward test needs a plan written before its first day: how long it runs, and what result would end it. That is a stop rule, for example: after 65 trading days (days the market is open: 65 is about three months, and a year has about 260), stop the rule if its lead is below a line. A line drawn after the result can be drawn to fit it (lesson B10).

To count what a stop rule does, take rule 1, whose truth we know: with the push it has a real lead, and on the same markets without the push it has nothing real. Choose your line, then run three months forward on all 1,000 markets.

Rule 1 after 65 forward days, 1,000 markets (simulated, before costs)

Stop if the lead is below:

Letting the empty rule run on means its lead reached the line by luck, despite trailing just holding by about $4 a year on average. A lower line stops a real lead less often and lets an empty rule run on more often (−$5: 137 stopped, 768 ran on; +$5: 793 and 115), and after three months no line does both well; the next screen shows why.

How wide luck is in a forward test

Across the 1,000 markets, rule 1 with the push led by 1.7 cents a day on average, and one day's lead had a standard deviation of $0.73: how far a day's lead typically strays from its average. It is smaller than a day's typical move because the rule is in the market only some days, and its lead is $0 on those. Call that σ (sigma), the usual size of a day's swing. The error bar is the span around the average lead where the result lands about 95 times in 100 (lesson B10); over n days it reaches 1.96 × σ × √n either side (it grows with √n because up and down days partly cancel). Slide the forward test longer.

Where rule 1's forward lead lands 95 times in 100, by the formula (simulated, before costs)

Colour: with the push, centred on 1.7 cents a day. Grey: without it, centred on −1.5 cents a day. Line: the average lead. Band: ± the error bar.

Expected lead after 20 days$0.34
Error bar either side$6.39
Line $0: stopped the real one451
Line $0: let the empty one run on460

Over four weeks, 20 trading days, the bar reaches 1.96 × 0.73 × √20 = 1.96 × 0.73 × 4.47 = 1.96 × 3.26 = $6.39 either side, around an average lead of 20 × 0.017 = $0.34. A few weeks cannot tell this lead from none.

After two years, 522 days, the bar is $32.69 either side of $8.87, and a line at $0 still stopped the real rule in 299 of 1,000 markets and let the empty one run on in 329. The lead grows with the days; the bar grows only with their square root, so the lead outgrows it, slowly.

For the preciseThe bands use the average day and the standard deviation measured over all 2.6 million days, unrounded. Rule 1's actual forward leads with the push fell inside the band in 941, 940, 924, 932 and 948 of 1,000 markets at 20, 65, 130, 260 and 522 days, where the formula says about 950: these markets have calm and wild spells, so days are not all alike. Without the push rule 1 led by −1.5 cents a day on average.
Theory

How many days a forward test needs

Over n days a rule whose true average lead is d a day leads by d × n on average, and the error bar reaches 1.96 × σ × √n. Set the two equal: d × n = 1.96 × σ × √n. Divide both sides by d × √n: √n = 1.96 × σ ÷ d. Square both sides, and you have a sample-size calculation: how many days a test needs before an expected lead reaches the edge of its error bar. At that length, the average lead equals the bar, so the bar's lower edge just touches $0.

n = (1.96 × σ ÷ d)²
n
the number of forward days at which a lead equal to its average, d × n, reaches the edge of its 95% error bar
d
the rule's true average lead a day. It is unknown: a picked backtest's figure is flattered, so n from it comes out too small
σ
the standard deviation of one day's lead
1.96
the multiplier for a 95% error bar (lessons B5 and B10)
Worked example: rule 1, with the push

σ = $0.73 and d = $0.017. 1.96 × 0.73 = 1.4308. 1.4308 ÷ 0.017 = 84.16. 84.16² = 7,082.9: about 7,083 days, and at about 260 trading days a year, 7,083 ÷ 260 = 27.2 years.

Even then, a forward test lands above its average about half the time and below it half the time, so it clears zero by its whole bar only about half the time. The lead enters squared: twice the lead needs a quarter of the days.

Holds when
  • one rule, fixed, run once as planned;
  • one day's lead does not change the next day's;
  • d and σ stay the same on the forward days.
Breaks when

d is taken from a picked backtest, which is flattered, so n comes out too small. It also breaks when the market changes under the rule (lesson C3, Regimes and decay).

New numbers. A rule's true average lead is $0.05 a day, and one day's lead has σ = $0.60. About how many forward days does it need?

1.96 × 0.60 = 1.176. 1.176 ÷ 0.05 = 23.52. 23.52² = 553.2: about 553 days, a little over two years of trading days.
Multiply first: 1.96 × 0.60 = 1.176. Divide by the lead a day: 1.176 ÷ 0.05 = 23.52. Then square it: 23.52 × 23.52 = 553.2, about 553 days.

What a forward test can't do

A forward test is one draw, with its error bar. It can't promise the market stays the same: here the push never changes, while in a real market an effect can fade (lesson C3). And it can become the best of many again. Try it: forward test all twenty rules on 2023 and keep the best.

20 rules forward tested on 2023 (simulated, before costs)

Dots: each rule's lead in 2023 on the market shown. Colour: the best of 20. Bars: the best of 20's average lead across the 1,000 markets with the push.

On this market the best of 20 in 2023 was rule 14, one with nothing real in it: a lead of $14 in 2023, then $5 in 2024.

On the 1,000 markets with the push, the best of 20 forward tests led by $12.19 on average in 2023, and trailed by $1.54 in 2024, in this simulation, before costs. Part of the fall below zero is the sitting-out handicap: a rule with nothing real trails holding by about $4 a year. Twenty forward tests with the best kept is lesson B7's best of many: the forward days picked the rule, so its result there is partly luck.

For the preciseThe best of 20 in 2023 led by less in 2024 in 827 of the 1,000 markets. It was rule 1 in 249; a blind draw: about 50.

What this means for your backtest

  1. Write the stop rule before the first forward day.

    How long the test runs, the line, and what happens below it. A line drawn after the result can be drawn to fit it.

Practice 1 of 3

"Forward test it for a month"

The situationAn imagined sales page says: "Our rule leads by 10 cents a day. Forward test it yourself for a month." One day's lead has a standard deviation of $1.00.

Taking the claim at its word, about how many forward days would the rule need for a lead at its average to reach the edge of its error bar?

Hint: n = (1.96 × σ ÷ d)², with σ = 1.00 and d = 0.10.

1.96 × 1.00 = 1.96. 1.96 ÷ 0.10 = 19.6. 19.6² = 384.16: about 384 days, a year and a half of trading days. A month, about 21 days, is far too short. If the 10 cents comes from a picked backtest, the true lead is likely smaller and the days needed more.
1.96 × 1.00 = 1.96. Divide by the lead a day: 1.96 ÷ 0.10 = 19.6. Square it: 19.6 × 19.6 = 384.16, about 384 days. A month, about 21 days, is far too short.
Practice 2 of 3

Three weeks in

The situationYour stop rule, written before the forward test began: "after 65 trading days, stop the rule if its lead is below $0." Three weeks in, the rule is $9 behind just holding.

Which next step keeps the forward test honest?

The stop rule was written to decide on day 65 only. Stopping at three weeks is a new rule, written after seeing the result, and a rule written after the result can be fitted to it. Run on to day 65 and apply the line as written.
A line moved after seeing the result can be moved to fit it, and then the stop rule decides nothing. Run on to day 65 and apply the line as written.
The plan was written before the result, so it can't have been fitted to it. Day 65 is when the stop rule decides, with the line as written.
Practice 3 of 3

The best live result

The situationAn imagined sales page says: "We forward tested 30 systems for six months, on days none of them had seen. Our best one led by $15 over the six months."

What is the first problem to raise with that $15?

Keeping the best of 30 lets the forward days pick the system: lesson B7's best of many. In the lesson's simulation, before costs, the best of 20 forward tests led by $12.19 on average in 2023 and trailed by $1.54 in 2024.
That is a real problem too: six months is about 130 trading days, where the lesson's error bar is about $16 either side, so $15 sits inside it. But the first problem is the picking: the $15 is the best of 30 forward tests, and the best of many is partly luck (lesson B7).
The days were new to every system, but they then picked one of 30. The best of many forward tests is partly luck, just like the best of many backtests (lesson B7).
✓

Lesson done

A forward test runs a rule, unchanged, on days that did not exist when it was fixed. When the rule was picked as the best of several on the same backtest, expect its forward result to be lower on average, though not every time: in the lesson's simulation the pick's lead averaged $4.71 a year in the backtest and $2.73 forward, before costs. Write the stop rule before the first forward day, and read the result with its error bar: for that push, four weeks could not tell the lead from none. Many forward tests with the best kept are the best of many again.

Key ideas

Forward test
a rule, unchanged, run on new days as they arrive: days that did not exist when the rule was fixed, so nothing in it can have come from them
Stop rule
a plan written before a forward test's first day: how long the test runs, and the result below which the rule is stopped
Sample-size calculation
working out how many days a test needs before an expected lead reaches the edge of its error bar

Formula

days a forward test needs: n = (1.96 × σ ÷ d)²

d is the rule's true average lead a day and σ the standard deviation of one day's lead. At n days the average lead equals the 95% error bar, so the bar's lower edge just touches $0. It holds for one fixed rule run once, with days that don't change each other and a market that stays the same; d from a picked backtest gives too few days.

Lesson notes

A forward test runs a trading rule, unchanged, on new days as they arrive: days that did not exist when the rule was fixed, so nothing in the rule can have come from them. When the rule was picked as the best of several on the same backtest, its backtest result is partly luck, so its forward result is lower on average, though not every time. A stop rule, written before the first forward day, says what result would end the test; and a forward test needs its error bar, because a small lead takes far longer than a few weeks to tell from none. All numbers are from the lesson's simulated markets and are before trading costs.

The setup

A backtest runs a rule over past prices to see what it would have made. The lesson's prices are simulated, 2015 to 2024, and the researcher stands at the end of 2022. Each day a rule is in the market ($100 in at the day's first price, out at its last) or out of it. A rule's lead is its result minus the result of just holding (being in every day) over the same days. Each evening the market publishes a made-up number, the reading, usually between about −2 and +2, with a small push built in: each unit of it adds 0.10% to the next day's move in its direction, where a typical day moves about 1.03% up or down. There are also 19 made-up readings that push nothing. Rule 1 is in the market the day after the market's reading was above 0; rules 2 to 20 do the same with the 19 others. All twenty are backtested on 2015–2022 and the best is kept: the pick. On the lesson's main market, the pick is rule 1: $58 where just holding lost $61, a lead of 58 + 61 = $119 over eight years, or 119 ÷ 8 = $14.88 a year, before costs. It is the best of 20, and the best of many tries is partly luck (lesson B7).

Forward days, and how they differ from held-out days

From the end of 2022 the pick is fixed. A forward test runs it, unchanged, on 2023 and after, as the days arrive. A held-out test (lesson B10) runs a rule once on past days set aside before the work; those days existed, so someone could have looked at them. Forward days did not exist when the rule was fixed, so nobody could have looked at them, tuned on them or picked with them.

Will it match the backtest?

On the lesson's main market, the pick made $26 over 2023–2024 while just holding lost $2: a lead of 26 + 2 = $28, or $14 a year, about the same as its backtest's $14.88. That is one market. On 1,000 more built the same way, the pick's lead averaged $4.71 a year in the backtest and $2.73 forward, before costs; the forward figure was the higher in 423 of the 1,000: lower on average, not every time.

Why lower: the picking

The same 1,000 markets were run again with the push removed and the prices otherwise the same. Without the push no rule has anything real, yet a rule that sits out about half the days still trails just holding, by about $4 a year, because these markets rise on average, so sitting out about half the days misses about half the rise. With the push, rule 1 on its own, never chosen from the twenty, averaged $4.37 a year in the backtest and $4.33 forward: about the same, because nothing chose it for a lucky backtest. The pick's fall from $4.71 to $2.73 comes from the picking: in 188 of the 1,000 markets the pick was a rule with nothing real in it and a lucky backtest. Without the push no rule has anything real. The pick still averaged $1.53 a year in the backtest, then trailed just holding by $3.89 a year forward. That trailing is the sitting-out handicap, not luck (rule 1 alone trailed by $3.95 a year in the backtest and $4 forward).

The stop rule

A stop rule is a plan written before a forward test's first day: how long it runs, and what result ends it. A line drawn after the result can be drawn to fit it. The lesson's stop rule: after 65 trading days (days the market is open: about three months, where a year has about 260), stop rule 1 if its lead is below a line. With the push rule 1 has a real lead; without it, nothing real. Across the 1,000 markets, a line at −$5 stopped the real one in 137 and let the empty one run on in 768; a line at $0, 432 and 395; a line at +$5, 793 and 115. Letting the empty rule run on means its lead reached the line by luck, despite trailing just holding by about $4 a year on average. A lower line stops a real lead less often and lets an empty rule run on more often, and after three months no line does both well.

How wide luck is

Across the 1,000 markets' 2.6 million days, rule 1 with the push led by 1.7 cents a day on average, and one day's lead had a standard deviation of $0.73: how far a day's lead typically strays from its average, written σ (sigma). It is smaller than a day's typical move because the rule is in the market only some days, and its lead is $0 on those. The error bar is the span around the average lead where the result lands about 95 times in 100 (lesson B10); over n days it reaches 1.96 × σ × √n either side (it grows with √n because up and down days partly cancel). Over 20 trading days, four weeks, that is 1.96 × 0.73 × 4.47 = 1.96 × 3.26 = $6.39 either side of an average lead of 20 × 0.017 = $0.34: a few weeks cannot tell this lead from none. After two years, 522 days, the bar is $32.69 either side of $8.87, and a line at $0 still stopped the real rule in 299 of 1,000 markets and let the empty one run on in 329. The lead grows with the days, the bar only with their square root. Rule 1's actual forward leads fell inside the band in 924 to 948 of 1,000 markets at each length, against the 950 the formula says, because these markets have calm and wild spells.

What a forward test can't do

It is one stretch of days: one draw, with its error bar. It can't promise that the market stays the same; in the lesson's simulation the push never changes, while in a real market an effect can fade (lesson C3). And it can become the best of many again: on the 1,000 markets with the push, all twenty rules were forward tested on 2023 and the best kept. It led by $12.19 on average in 2023 and trailed by $1.54 in 2024, before costs; part of the fall below zero is the sitting-out handicap.

Formula: how many days a forward test needs

Over n days a rule whose true average lead is d a day leads by d × n on average, and the error bar reaches 1.96 × σ × √n. Set the two equal, d × n = 1.96 × σ × √n, divide both sides by d × √n, √n = 1.96 × σ ÷ d, and square both sides: a sample-size calculation. At that length the average lead equals the bar, so the bar's lower edge just touches $0.

n = (1.96 × σ ÷ d)²
n
the number of forward days at which a lead equal to its average, d × n, reaches the edge of its 95% error bar
d
the rule's true average lead a day. It is unknown: a picked backtest's figure is flattered, so n from it comes out too small
σ
the standard deviation of one day's lead
1.96
the multiplier for a 95% error bar (lessons B5 and B10)
Worked example: rule 1, with the push

σ = $0.73 and d = $0.017. 1.96 × 0.73 = 1.4308. 1.4308 ÷ 0.017 = 84.16. 84.16² = 7,082.9: about 7,083 days, or 7,083 ÷ 260 = 27.2 years of trading days. Even then a test lands below its average about half the time, so it clears zero by its whole bar only about half the time. Twice the lead needs a quarter of the days.

Holds whenBreaks when

d is taken from a picked backtest, which is flattered, so n comes out too small. It also breaks when the market changes under the rule (lesson C3, Regimes and decay).

One more worked example

Made-up numbers

A rule, fixed and run once as planned, has a true average lead of $0.04 a day, and one day's lead has σ = $0.80. 1.96 × 0.80 = 1.568. 1.568 ÷ 0.04 = 39.2. 39.2² = 1,536.6: about 1,537 forward days, or 1,537 ÷ 260 = 5.9 years.

Common mistakes

Check yourself

  1. A rule's true average lead is $0.02 a day and one day's lead has σ = $0.50. About how many forward days does it need for a lead at its average to reach the edge of its error bar?
    Answer

    1.96 × 0.50 = 0.98. 0.98 ÷ 0.02 = 49. 49² = 2,401 days, about 2,401 ÷ 260 = 9.2 years, for one fixed rule run once, with days that don't change each other and a market that stays the same.

  2. What makes a forward test different from a held-out test?
    Answer

    A held-out test runs the rule once on past days set aside before the work: they existed, so someone could have looked at them. A forward test runs the unchanged rule on days that did not exist when the rule was fixed, so nobody could have looked at them, tuned on them or picked with them.

  3. (from lesson B10) A forward test of 16 days ends with a lead of $2, and one day's lead has σ = $0.75. The error bar on a lead over n days is lead ± 1.96 × σ × √n. Is zero inside it?
    Answer

    √16 = 4. 0.75 × 4 = $3. 1.96 × 3 = $5.88 either side, from 2 − 5.88 = −$3.88 to 2 + 5.88 = $7.88. Zero is inside: the result cannot tell this rule from one with no lead.

  4. Why did the pick's lead fall from the backtest to the forward test on average, while rule 1's own lead did not?
    Answer

    The pick was the best of 20 backtests, so its backtest lead was partly luck that the forward days did not repeat; in 188 of the 1,000 markets with the push it was a rule with nothing real in it. Rule 1 on its own was not chosen for its backtest, so its backtest lead had no extra luck in it on average: $4.37 a year, then $4.33 forward.

Try it yourself

Run the script. Every number in this lesson comes from one Python script (below). It builds the simulated markets with the push and without it, makes the 19 readings that push nothing, backtests the twenty rules, keeps the pick, runs it forward, and counts what the stop rules and the best of 20 forward tests did. Change the stop line, the length of the forward test or the number of rules, and see what moves.

By hand, in a spreadsheet. Before the first forward day, write the stop rule in a cell with today's date: the rule, the length, the line. Then, as days arrive, put each day's lead in column A (0 on a day the rule is in, minus the day's move on a day it is out). The lead so far is =SUM(A:A), and its error bar reaches =1.96*STDEV.P(A:A)*SQRT(COUNT(A:A)) either side. With a guess of the true lead a day in B1 and of one day's standard deviation in B2, the days needed are =(1.96*B2/B1)^2.

The script needs simmarket.py, the course's simulated market, in the same folder.

The code behind every number (Python, on the course's simulated-market generator; market seeds 2026111601 to 2026112601)
Loading…

Going further: clearing the bar most of the time, and looking more than once

Clearing the bar most of the time. At n = (1.96 × σ ÷ d)² a test clears zero by its whole bar only about half the time. To clear it about 80 times in 100, the average lead must sit a further 0.84 standard errors above the bar's edge, where 0.84 is the bell curve's 80th percentile (about 80 in 100 draws fall below 0.84 standard deviations above the average): n = ((1.96 + 0.84) × σ ÷ d)². For rule 1: 2.80 × 0.73 = 2.044, ÷ 0.017 = 120.24, squared = 14,458 days, about 55.6 years. The chance that a test clears its bar when a real effect of a given size is there is called the test's power.

Looking every day. The lesson's stop rule looks once, on the day it names. Looking at every check and stopping at the first dip is several looks, and each look is one more chance for luck to trigger the stop. On the 1,000 markets, a line at $0 checked at all five lengths (20, 65, 130, 260 and 522 days), stopping at the first time below, stopped rule 1 with its real push in 738, against 299 when checked only at 522 days. Without the push it stopped it in 902.

Honest notes

Where this comes from