Skip to content
Wildflower
Join free
Backtesting in Python (optional)

Free Trading School · Level 7 · Course 7.5

Walk-forward testing in code

Lesson 13 of 15 in Backtesting in Python (optional)Module 3: Results and checks

Advanced · Backtesting in Python

Walk-forward testing in code

Wildflower Trading School · Lesson 7.5.3.3

Wildflower

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

  1. Run the walk-forward loop on your own file, with your own score and three or four choices.
  2. Write down the window lengths and how the setting is chosen before you run it, then compare each build score with its test score.

Next: Shuffling trades to see the range.

Quick check

  1. 1. In walk-forward testing, which results go into the joined record?

  2. 2. A setting scored best in its build year, then lost in its test year. What does that suggest?

  3. 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

Make a free account and we'll remember where you are, so “Shuffling trades to see the range” is waiting next time.

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.

  • (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 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 , 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, 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 numbers

Reading the walk-forward

Made-up numbers · points, before costs · random made-up prices

  1. 2024: best build score 467.0, then −198.8 in its test year.
  2. 2025: best build score 309.5, then −387.0 in its test year.
  3. Test years joined: 70.8 − 198.8 − 387.0 = −515.0
  4. 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

Drill

Made-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.