If you've trained a machine-learning model on market data and gotten suspiciously good cross-validation scores, there's a good chance your validation was lying to you. The default cross-validation everyone reaches for — k-fold — does something catastrophic on time-series data: it shuffles the rows. On a trading model, shuffling means training on tomorrow to predict yesterday, and the result is a backtest that looks brilliant and fails the moment real time only moves forward. None of this is investment advice.
Why k-fold breaks on time series
Standard k-fold cross-validation splits your data into random folds, trains on most of them, and tests on the held-out one — rotating until every row has been a test row. This is the right tool when samples are independent, like classifying unrelated images.
Market data is not independent across time, and the order is the whole point. When k-fold shuffles, it scatters future observations into the training set and past observations into the test set. Your model gets to "learn" from data that, in reality, hadn't happened yet when the prediction needed to be made. That's look-ahead bias, baked directly into your validation procedure — and it inflates your scores because predicting the past using the future is easy and useless.
In time series, when something happened matters as much as what happened. Any validation that ignores chronological order is implicitly assuming you could have known the future — which is exactly the assumption that makes a trading backtest worthless. Preserving time order isn't a nicety; it's the core requirement.
Walk-forward validation: testing the way you'd trade
The fix is to validate the way you'd actually deploy: train on the past, test on the future, and never the reverse. This is walk-forward validation.
You train on an initial window, test on the period immediately after it, then move the window forward and repeat. In an expanding-window version, the training set grows to include everything up to each test period — mimicking an investor who uses all history to date. In a rolling-window version, the training set is a fixed-length window that slides forward, which adapts to changing regimes by forgetting the distant past. Either way, every test period is strictly later than the data the model trained on, so there's no leak. The scores you get are an honest estimate of how the model would have performed forward in time.
Walk-forward validation retrains the model many times over successive windows, so it costs more compute than a single shuffled k-fold. That cost is the price of an honest answer. A fast validation that lies is worse than a slow one that tells the truth — resist the temptation to shuffle just because it's quicker.
Purging and embargoing for overlapping labels
There's a subtler leak that walk-forward alone doesn't fully close. If your labels are built from windows of time — say, "the return over the next five days" — then a training sample near the boundary of your test set overlaps in time with test samples, quietly sharing information across the split.
The fix, popularized in the quant-ML literature, is purging and embargoing: remove training samples whose label windows overlap the test set (purging), and add a small gap after the test set before training resumes (embargoing), so adjacent-in-time leakage doesn't sneak through. If your features or labels look forward over any horizon, you need this; if each sample is genuinely point-in-time, plain walk-forward is enough.
The unifying principle behind all of it is one sentence: information from the future must never touch the training set. K-fold violates it by shuffling; walk-forward respects it by construction; purging and embargoing patch the edge cases. Get this right and your validation scores become trustworthy. Get it wrong and you'll keep deploying models that were never as good as your notebook claimed.
Validation is where most trading models are secretly broken, and shuffled k-fold is the usual culprit. Switch to walk-forward, add purging and embargoing when your labels overlap, and hold to the one rule that makes results trustworthy: the future never gets to teach the past.
Originally published at pickuma.com. Subscribe to the RSS or follow @pickuma.bsky.social for new reviews.
Top comments (0)