Backtesting Essentials · Lesson 17
Capstone: one idea, from question to verdict
One question about a simulated market, taken through every step of a backtest you can trust. You make each decision; the page keeps the score, then counts what each shortcut does on 1,000 markets.
Know the basics? Read the lesson notesA question, then a hypothesis
This lesson takes one question through every step of this course, in order. You make each decision, and the page keeps the score: a point for each careful choice on the first try.
The market is simulated: made-up daily prices and volumes from 2015 to 2022 (volume is the number of shares traded in a day). You stand at the end of 2022. The question: when a day closes up on unusually heavy trading, does that say which way the next day goes? You will learn whether this market has anything to find only after your verdict.
A hypothesis is a statement a test can count against, and it needs a fair yardstick. These simulated markets are built to rise on average, so a rule that holds one on some days tends to make money for that reason alone. So the rule is compared with holding's share: just holding's result (in the market every day) times the share of days the rule is in. The rule's result minus holding's share is its excess. Results are counted after costs, what each trade pays to buy and sell. Which can be tested?
What the rule knows, and when
The exact rule. A heavy day: its volume is the highest of the last 10 trading days, itself included. A heavy up day also closes above the day before. After one, the rule holds $100 of the market for 1 day. Those two numbers are settings; you will vary them later.
Look-ahead bias is a backtest using something not yet known when it trades (the lesson Look-ahead bias). A day's close and its full volume are known only when it ends, at 16:00; its open is its first price, at 09:30. When can the rule buy?
So a day's hold runs from one open to the next: it makes $100 × (next open ÷ open − 1).
Check the data before any run
Before any run, look at the data itself (the lesson Data quality). This is the raw file: prices and volumes exactly as recorded, nothing corrected.
The raw file, 2015–2022 (simulated)
Line: the daily close (left scale, dollars). Bars: volume (lower scale, millions of shares a day).
On 2020-10-05 the close goes from $210.79 the day before to $102.47, and stays near that level. Volume jumps to more than double from then on. What happened? (A 2-for-1 split cuts each share into two; a bad print is one wrong price.)
The clean file is split-adjusted: prices before the split halved and volumes doubled, so the series runs on as one. The split explains a doubling of volume; the rest of the jump is a wild spell, a stretch of large moves, that began around then. In the raw file a day's hold over the split shows −$50.73 on $100, a fall that never happened, and it flows into just holding's result and so into holding's share. The plan uses the clean file.
One market that traded all eight years is tested, so testing only what survived to today (survivorship bias) can't arise here.
The plan, before any run
Some things are fixed before any result exists, so no result can move them. The years are split in two: working years, where versions of the rule are tried, and held-out years, set aside for one run of one chosen version (the lesson The held-out test).
The line. A result counts as a finding only if its error bar, the range luck could have moved it, sits wholly above $0. That bar is the excess ± 1.96 standard errors, a standard error being how far luck typically moves the excess. An excess with nothing behind it, averaging $0, clears that about 2.5 times in 100 (another 2.5 fall below the bar's low end).
Two choices are yours.
A spread is the gap between the price to buy and the price to sell; a trade (days in a row in the market) pays it once, 2 cents on $100 at the open here. Left out, costs can hide that a small result is gone after trading. Costs or years chosen after a look can be chosen to fit the results, and if every year helps choose, none is left to test on.
Rule: buy at the next open after a heavy up day. Data: the clean file. Costs: 2 cents a trade.
Working years: 2015–2018. Held-out years: 2019–2022, one run. Line: z above 1.96.
The pick: the logged version with the most dollars of excess on the working years.
Nine versions, every one logged
A version is one full set of the rule's settings. Two are open: how many days the volume must top (5, 10 or 20, about a week, two weeks or a month) and how many days to hold (1, 2 or 5). That makes 9. Every version run on the working years goes in the version log, kept or not.
Each version with nothing in it is one more chance to clear the line by luck. With n such versions, each clearing it with chance p, the chance that at least one does is 1 − (1 − p)n (the lesson The best of many). Run all nine.
Your version log on 2015–2018 (simulated, after costs)
Each box: one version's excess and z. Rows: the days the volume must top. Columns: the days held.
With all 9 logged and p = 0.025 (the line's 2.5 in 100), that chance is 1 − 0.9759 = 0.204, about 20 in 100 if the nine were separate tries; fewer, since neighbouring versions share most of their days and so their luck. None cleared 1.96: the highest z was 1.61. The plan's pick, the most dollars of excess, is volume at the top of 5 days, held 5 days: $24 after costs (z = 1.61).
Before the held-out run
Look at the pick's place in the grid. It sits in a corner, and its neighbours, the versions one step away, made $10, −$5 and −$4. A lone high result is a peak; a wide patch of similar ones, a plateau (the lesson Robustness). Neighbours share most of their days, and so their luck: either shape can be luck, and neither replaces the held-out run.
What goes to the held-out years?
One run on the held-out years
The pick runs once on 2019–2022, settings unchanged. Will it clear the line?
The pick on 2019–2022: its excess, added up day by day (simulated, after costs)
Line: the running excess. Bar at the end: its error bar. Dashed: $0.
You guessed a finding. There was none.
You guessed no finding, and there was none.
Over the 1,043 held-out days the excess was −$23. One day's excess has a standard deviation (its typical distance from its average) of σ = $0.65, more than the $0.46 on the working years, because the held-out years were wilder: a typical day moved $1.32 against $0.96. Luck adds up more slowly than days, with √n: √1,043 = 32.3, so the standard error is 0.65 × 32.3 = 21.0 and the bar reaches 1.96 × 21.0 = 41.2, about $41, either side: from −23 − 41 = −$64 to −23 + 41 = $18. The bar reaches $0: no finding.
A verdict you'd defend
Which verdict would you sign?
Now the answer. This market was built with nothing to find: it comes from the course's simulated-market generator with the one effect it can contain, a link between volume and direction, switched off. Its prices move in calm and wild spells (stretches of small and of large moves), and a day's volume follows how wild the day is, never which way it goes. So no finding was right here. This shows how a careful test behaves when nothing is there; it says nothing about whether real markets have such an effect.
It could have gone wrong either way. With nothing to find, the line is cleared by luck about 2.5 times in 100. And a small real excess could have been missed: a true excess as large as the bar's reach, $41 over these four years, would clear the line only about half the time, because the measured excess lands above its true value about half the time. Had the pick passed, the next evidence would be a forward test: the rule, unchanged, on days that did not exist when it was fixed, with the result that would stop it written down first (the lesson From backtest to forward test).
The arithmetic of a verdict
The verdict rests on three lines. The first takes out the market's average rise, for a hypothesis about direction. The other two say how far luck could have moved the result: the error bar is a 95% confidence interval (a range built so that luck alone puts the true value outside it only about 5 times in 100), and z is a z-score: the same excess counted in standard errors, read against 1.96.
- R
- the rule's result over the test days, after costs
- H
- just holding's result over the same days
- f
- the share of those days the rule is in the market
- σ
- the standard deviation of one day's excess (its typical distance from its average)
- n
- the number of test days; σ × √n is the standard error
- 1.96
- the multiplier for a 95% interval
R = −$23.74, H = −$1.90 (over these four years this market happened to go nowhere), f = 0.372. Holding's share: 0.372 × −1.90 = −$0.71. Excess: −23.74 + 0.71 = −$23.03. Standard error: 0.65 × 32.3 = 21.0, so the bar is ± 41.2 and z = −23 ÷ 21.0 = −1.1: no finding.
- one version, chosen before the run, runs once as planned;
- one day's excess doesn't change the next day's;
- enough days that luck in the total is close to the usual bell shape.
The run is the best of several on the same days: the bar then sits around a flattered number (the lesson The best of many). Calm and wild spells, as here, make 95 in 100 approximate.
New numbers. Over 900 test days a rule made R = $40 after costs, in on a quarter of the days, while just holding made $80. One day's excess has σ = $0.50. How many dollars either side does its error bar reach?
Your score, and what each shortcut does
- A hypothesis with a fair yardstick
- A rule that reads only what was known
- The data checked
- Costs fixed in advance
- Untouched years kept for one run
- The pick sent once, unchanged
- A verdict with its error bar
One market is one draw. The same study ran on 1,000 more markets from the same generator, each with nothing to find, so every finding is a false one. With every choice made carefully, in how many does the plan still end with a finding?
Findings in 1,000 markets with nothing to find (simulated)
You guessed about 0. It was 30.
You guessed about 30, and it was 30.
You guessed about 150. It was 30.
The careful plan ended with a finding in 30 of 1,000, near the 25 that 2.5 in 100 gives: the line lets luck through that often by design. The figure shows each shortcut alone, and your path: it starts with the shortcut behind each choice you missed; tap the shortcuts to switch them. No costs and the raw file moved the count only a little (34 and 38 against 30, within chance here): a 2-cent cost is small, and the raw file's fake fall pushes results both ways (the box below). With no untouched years the best of 9 is judged on the years that picked it: 93. Buying at the open uses a price the rule couldn't know yet: 976. All counts are this simulation's, after costs where costs are charged.
For the precise
The raw file. The fake fall lands in the held-out years of every market. In 833 of 1,000 the pick was out of the market that day, so the fall lowered only holding's share and raised the excess (by $5.45 in the middle market); but that one huge day also widened the error bar (by 15% in the middle market), so z moved little. In the other 167 the pick held through the fall and lost. On the working years both files give the same triggers, so the pick was the same in 972 of 1,000 (the rest differ by cent rounding of the doubled prices).A friend's backtest
What is the first problem to raise?
Is it a finding?
What is its z?
Hint: excess = R − f × H; standard error = σ × √n, and √1,600 = 40; z = excess ÷ standard error.
After a pass
What is the next step that adds evidence?
Lesson done
One question went through every step: a hypothesis with a fair yardstick, a rule that reads only what was known, checked data, costs and a split fixed before any run, every version logged, one held-out run, and a verdict with its error bar. Here the verdict was no finding, excess −$23 ± $41 after costs, on a simulated market built with nothing to find; that says nothing about whether volume ideas work in real markets, and it does not show that nothing is there, since a small real excess could hide inside the bar. In this lesson's simulation, on 1,000 such markets, the careful plan still ended with a finding in 30, by luck; without untouched years it was 93, and buying before the day's close and volume were known, 976.
Key ideas
- Verdict
- what a test measured, with its error bar, its years and its costs, and no more; "no finding" is not proof that nothing is there
- Raw file, clean file
- prices and volumes as recorded, and the same adjusted for events such as a split
- Shortcut
- a step skipped or done after seeing results; in this lesson's simulation some added false findings only a little (no costs, the raw file) and some a lot (no untouched years, buying before the close was known)
Formulas
excess = R − f × H · error bar = excess ± 1.96 × σ × √n · z = excess ÷ (σ × √n)
For one version chosen before the run and run once as planned, on days whose excesses don't change each other; around the best of several runs on the same days the bar sits on a flattered number.