Lesson 476 and Lesson 660 described walk-forward as "a test that repeats while moving the picking period and the applying period forward in time order," and section 3 of Lesson 306 showed an example repeated five times. In this lab you compute that procedure from start to finish. The order of the calculation is how to split the periods, what to pick again at each step, and how to join the results.
The benchmark is the result of picking the best value after seeing the whole period. A number computed with a value picked after the fact has already seen that period, so it cannot be used as an out-of-sample result. Walk-forward prevents this by letting each step pick using only the data up to that point.
The data is synthetic data built so that the regime changes midway. The last section counts, in numbers, why a gap is left between training and testing when a forward-looking label is used. Shaking the result in several directions to see whether it is robust is done in Lesson 591.
1. The data and the hypothetical teaching rule
The prices are synthetic data made with a fixed seed. The first 1,460 periods are regime A, with normally distributed returns of period mean 0.0005 and standard deviation 0.009, and the last 1,260 periods are regime B, with mean −0.0004 and standard deviation 0.015. Period 1460, where the regime changes, is the exact middle of the 2,520 usable periods. The first 200 periods are used only to build the long moving averages.
The seeds were fixed before writing the lesson, 10 seeds starting from 20261110. The step-by-step calculation follows the first seed, 20261110, alone, and the comparison numbers are written as medians and ranges over the 10 seeds. Looking at the results, the first seed turned out to give the highest value of the 10 in several numbers (section 4).
The rule is a hypothetical teaching rule. If the close is above the n-period moving average, hold for the next period; otherwise cash. The candidates for n are 6 values: 5, 10, 20, 50, 100 and 200. The picking rule was also set before seeing results. In the training window, pick the n with the highest annualized Sharpe ratio (based on 252 daily returns), and on a tie, the smaller n. Costs and the cash rate are set to 0.
2. A value picked after the fact and a single out-of-sample split
Picking after seeing all 2,520 periods, n = 5 was best for the first seed, with a full-period Sharpe ratio of 0.81. This number is measured over the whole period used for the picking, so it is not an out-of-sample result. The median over the 10 seeds was 0.40, and the first seed's 0.81 was the highest of the 10.
A single out-of-sample split picks with the first 60% (periods 200-1711) and applies the pick to the last 40% (1712-2719). For the first seed, n = 5 was picked again, and a training Sharpe ratio of 1.33 became 0.32 in the test segment. In medians over the 10 seeds, a training value of 0.69 became −0.17 in the test segment. The test segment lies entirely within regime B. Where you cut changes which regime goes into the test segment, and a single split is tied to that one cut.
3. Picking again with rolling and anchored walk-forward
Walk-forward starts by making a split table. The rolling method uses 504 training periods, 126 test periods and a step of 126 periods. The first training window is periods 200-703 and the first test window is 704-829; moving this way over 16 steps, the last test window ends at 2719. The anchored method has the same test windows but keeps the start of the training window fixed at 200 and only extends its end.
Once made, the split table is checked automatically. At every step it confirms that the training end is not later than the test start, that test windows do not overlap and that nothing goes outside the sample, and it stops the calculation if any of these fails.

For the first seed, the n picked by the rolling method moved among 4 values over the 16 steps and changed 4 times. Across the 10 seeds it changed from 2 to 9 times, with a median of 6. At step 7, picked with the end of regime A (training 956-1459), n = 50 was chosen, and it produced a Sharpe ratio of −1.32 in the test window 1460-1585, where regime B began. The anchored method picked n = 5 at all 16 steps for the first seed, because it keeps carrying the old data.
If the picked value swings a lot from step to step, that itself is a result to record. Rolling reacts quickly to regime changes but also reacts to noise; anchored is stable but accepts regime changes late. You cannot decide in advance which is better, so choose one and write it in the plan.
4. The joined out-of-sample result and the after-the-fact best value
Joining the test-window returns of each step in order gives an out-of-sample record of periods 704-2719, 2,016 periods. Compare it with the result of applying, from the start of the same segment, the value picked after the fact. In medians over the 10 seeds, the joined Sharpe ratio of rolling walk-forward was 0.15, anchored walk-forward 0.10, and the after-the-fact best value 0.37.
The after-the-fact best value is a number that had already seen the segment when it was picked, while walk-forward is a number that saw only the data up to each step. Taking the difference for each seed, the after-the-fact best value was 0.03-0.50 (median 0.14) higher than rolling walk-forward, and it was higher in all 10 seeds. This difference is the part added by picking after the fact.

Looking at the first seed alone, it is easy to misread the size of the numbers. The first seed's rolling walk-forward was 0.61 and its after-the-fact best value 0.72, both the highest of the 10 seeds. The anchored method picked n = 5 every time and came out at the same 0.72 as the after-the-fact best value. The figure below shows the curves of this first seed.

On the other hand, the first seed's difference of 0.11 was fifth from the bottom of the 10, around the middle. What this experiment shows is not the size of the numbers but the difference between after-the-fact picking and walk-forward, and the size depends on the settings and seeds of the synthetic data.
5. Leave a gap for forward-looking labels
When picking a rule, "the return over the next h periods" is sometimes used as a label. In this lesson, with h = 20, the label of period t is made from the returns of periods t through t + 19. Then the labels at the end of the training window already contain the returns of the test window.
In the 16-step split, if nothing is left empty between the training end and the test start, the last 19 labels of the training window at each step contain test-window returns. In general, the number of overlapping labels is h − 1 − gap. With a gap of h − 1 = 19 periods, the overlapping labels become 0, but because the test windows are pushed back, the steps drop by one to 15. This gap is also called purging or embargo; this course calls it the gap.
The gap needed depends on how the label is defined. If, following the one-period shift, the label is the return of periods t + 1 through t + h, the number of overlapping labels is h − gap. Then, with h = 20, the gap must be 20 periods or more. So write down the start and end of the label together with the gap.
Without a gap, the training results carry part of the test period's information, and that step's out-of-sample result is no longer out of sample. The split-table check looks only at the order of the training end and the test start, so record the label length separately and put in the gap yourself.
The record for this lesson is the split table and the table of picks by step. Write side by side the window lengths, the step, rolling or anchored, and the gap; the value picked at each step and the test-window result; and the joined out-of-sample result next to the after-the-fact best value for the same segment. If you practiced with synthetic data, also write the number of seeds and the medians and ranges.
Try it yourself
- From the results, read the values picked at each step by the rolling method for the first seed, and write the value picked and the test-window Sharpe ratio at the step just after the regime changed.
- Compare the joined rolling walk-forward Sharpe ratio with the after-the-fact best value for the same segment, as medians over the 10 seeds, and also write the range of the differences computed for each seed.
- Assumptions: 1,000 periods of data, 300 training periods, 100 test periods, a step of 100 periods, a gap of 4 periods, rolling method. Write the training and test windows of the first three steps yourself, and count how many steps there are in all.
- In question 3, if the label is the return of periods t through t + 9 as in the text, is a gap of 4 periods enough? How many labels overlap at each step?
Answers
First, the rolling method picked 5, 5, 10, 10, 10, 10, 50, 5, … in order. In the first test window after regime B began (step 7), the value picked was n = 50, chosen with data from the end of regime A, and the test-window Sharpe ratio was −1.32.
Second, in medians, rolling walk-forward is 0.15 and the after-the-fact best value is 0.37. The differences computed for each seed were 0.03-0.50 (median 0.14), and the after-the-fact best value was higher in every seed. For the first seed alone, it is 0.61 and 0.72, a difference of 0.11. The after-the-fact best value does not go in the out-of-sample box of a report.
Third, the first step is training 0-299, test 304-403. The second step is training 100-399, test 404-503, and the third step is training 200-499, test 504-603. The test window must fit within the 1,000 periods, so there are 6 steps, with test starts at 304, 404, …, 804.
Fourth, it is not enough. 10 − 1 − 4 = 5, so at each step the last 5 labels of the training window contain test-window returns. The gap must be raised to 9 periods or more. If the label were taken as periods t + 1 through t + 10, 10 periods or more would be needed.
Data: synthetic data made with fixed seeds 20261110-20261119 (10 seeds, fixed before writing the lesson), with normally distributed returns of mean 0.0005 and standard deviation 0.009 in periods 0-1459, and mean −0.0004 and standard deviation 0.015 in periods 1460-2719. The step-by-step calculation follows the first seed, 20261110, and the comparison numbers are medians and ranges over the 10 seeds.
