A single build and test split gives you one answer. Walk-forward testing, from Level 4, repeats it, moving forward through time. By the end of this lesson, you'll have coded that loop and read what it returns. You'll also know which choices to write down before you run it.
1 of 8
What you'll learn
- Write a score function. It tests one setting on one stretch of data.
- Loop through the windows. Choose a setting on one year, then test it on the next.
- Join the test windows. Together they make one out-of-sample record.
- Compare build and test scores. Best build scores may not carry forward.
- Write your plan down first. Note window lengths, how you choose, and the warm-up.
Exercise
- Run the walk-forward loop on your own file, with your own score and three or four choices.
- Write down the window lengths and how the setting is chosen before you run it, then compare each build score with its test score.
Quick check
1. In walk-forward testing, which results go into the joined record?
2. A setting scored best in its build year, then lost in its test year. What does that suggest?
3. Why does the score function shift the holding column by one day?
0 of 3 answered.
Finished this lesson?
Tick it off and keep your place
Read the full notes
Walk-forward testing in code is a loop: choose a setting on one window, test it on the next, move forward and repeat, then join the test windows. Write down the windows and how the setting is chosen before you run it.
- Building rules on one window of history, testing them on the stretch just after it, then moving both windows on and doing it again.More in the glossary (Walk-forward testing): choose a setting on one window, test it on the window just after, move both forward, repeat, and join the test windows into one History kept back while a strategy’s rules are built, then used once to test the finished rules unchanged. The part used to build them is in-sample.More in the glossary record.
- A score: a function score(data, n) that tests one setting on one stretch. Made-up rule for learning the loop: hold long while the close is above its n-day The average of the last few closing prices, worked out again on every candle and drawn as a line.More in the glossary, signal shifted one day so it can't peek.
- The loop: for each test year, build = df.loc[str(year - 1)], test = df.loc[str(year)], best = max(choices, key=lambda n: score(build, n)) (lambda makes a one-line function that gives each setting's build score), then score(test, best) goes into the list.
- Made-up result, random prices, One whole unit of a price. On the S&P 500, a move from 5,000 to 5,001 is one point, and each futures contract sets what a point is worth in dollars.More in the glossary before costs: 2023 +70.8, 2024 −198.8, 2025 −387.0, joined −515.0. Best build scores (467.0, 309.5) didn't carry forward.
- Write down first: window lengths and step; how the setting is chosen (best score, or a steady zone); how many choices; the warm-up. More: Walk-forward analysis.
Worked example
Made-up numbersReading the walk-forward
Made-up numbers · points, before costs · random made-up prices
- 2024: best build score 467.0, then −198.8 in its test year.
- 2025: best build score 309.5, then −387.0 in its test year.
- Test years joined: 70.8 − 198.8 − 387.0 = −515.0
- The best build setting didn't carry forward.
On random prices there's nothing to carry forward, and walk-forward shows that gap.
Your turn
DrillMade-up numbers · 5 years of data · choose on 1 year · test on the next · move 1 year at a time
Trading is risky. You can lose money. Test results are hypothetical, and a test can't tell you what will happen next. This lesson explains how testing works. It isn't advice.