Walkthrough · Step by step · Stock Trading
Out of Sample Testing: How to Hold Back Data for a Backtest
Out of sample testing scores a trading rule set on history it never touched while you built it. The split, the gap at the boundary and the single honest run are what make the number worth anything.
Short answer
Out of sample testing means scoring a finished trading method on a stretch of history that played no part in building or tuning it. Split the data by date, build and tune on the earlier part, write the rules down, then run them once, with costs, on the later part and compare the per-trade result with the tuned one.
- 1
Split the history by date
Decide the split before testing anything, such as the first 70% of the sessions for building and the last 30% held back, and keep the held-back part later in time.
- 2
Leave a gap at the boundary
Drop a stretch at least as long as the longest holding period between the two parts, so no trade straddles the line.
- 3
Build and tune on the early part only
Choose rules and settings using the in-sample data alone, with commissions and slippage charged on every trade.
- 4
Write the rules and the pass mark down
Record every rule, every setting and the result you will accept before the held-back data is opened.
- 5
Run the held-back data once
Apply the finished rules to the later stretch without changes and record the trades, costs included.
- 6
Compare per-trade results
Set the out-of-sample expectancy against the in-sample figure, and size any live trading from the lower one.
A backtest report shows +54R on 120 trades and a smooth line climbing to the right. Every one of those trades happened on data the rules were tuned on. So the report answers one question, how well the settings fit that history, and next year’s trades depend on something else entirely.
Out of sample testing is the standard check. Hold back part of the history, finish the method without looking at it, then let the finished rules meet it once.
What separates in-sample from out-of-sample data?
In-sample data is everything you used to make a decision. Picking a moving average length, setting a stop distance, adding a volume filter because it removed a bad month: each choice used the data in front of you, and each one fits some of that history’s noise along with any real pattern. Out-of-sample data is history the method never saw during those choices.
Order matters. With market data the held-back stretch should come after the tuning stretch, because live trading always happens after the backtest, and a test should face the same direction.
Splits around 70/30 are a common example. Nothing official sets that ratio. What decides the split is the number of trades each side will hold, and a method that trades twice a month needs far more history than one that trades daily before its held-back stretch says anything.
How does the split work on real numbers?
Take a hypothetical swing method, a decade of daily bars, and a rule that holds positions for up to ten sessions.
Build and tune on the first 1,764 sessions. Write the rules down. Then run the last 756 once.
R is the amount risked on each trade. The method still made money on data it hadn’t seen. It made a third as much per trade, and that third is the number to plan with: position sizes, account targets and the drawdown you should expect all come from +0.15R. A thin edge like that also means long losing runs, and the losing streak calculator shows how long a run to plan for at a given win rate.
Why does the boundary need a gap?
Most explainers stop at the split and leave out what happens at the line itself.
Say a trade opens three sessions before the split and closes seven sessions after it. Its outcome depends on prices inside the held-back stretch, yet it sat in the tuning data and helped choose the settings. The settings have now seen a little of the test. With hundreds of trades near the line, a little becomes enough to lift the out-of-sample figure.
The fix is a gap. Set aside the sessions between the two parts, ten of them here to match the longest hold, and use them for neither. A method with indicators that look back 200 sessions has a second, smaller problem of the same kind, since its first test signals are calculated partly from tuning-period prices; starting the scored test after a full lookback window closes that leak too.
Random splits break the same way, only worse. Shuffling days into a tuning pile and a test pile, the way a k-fold split does for data where order doesn’t matter, puts Tuesday in one pile and Wednesday in the other, and market data from neighboring days is close to a copy. Keep the split in date order.
How many looks does the held-back data allow?
One. After that, it’s tuning data.
The temptation is gradual. The first run disappoints, a filter gets added, the run is repeated and looks better. The second run is no longer a test. Repeat the cycle often enough and luck alone will produce a pass.
With twenty variants run against one held-back stretch, the odds favor a false pass. That’s the reason to write the rules and the pass mark down before opening the data, and to count every variant you tried when you judge the one that passed.
What does published research show about held-back results?
R. David McLean and Jeffrey Pontiff, in the Journal of Finance in 2016, tracked 97 variables that published studies had found to predict stock returns. Portfolio returns built on them were 26% lower out of sample, after each original study’s data ended, and 58% lower once the research was published.
Campbell Harvey, Yan Liu and Heqing Zhu, in the Review of Financial Studies the same year, looked at how many factors researchers had tested and argued that a newly proposed one should clear a t-statistic above 3.0, a higher bar than the usual 2.0, because so many tries make lucky results likely. Professional researchers with peer review lose a large share of their measured edge on data they didn’t fit, so a home-built method with a dozen settings should expect at least as much shrinkage.
Which biases survive any split?
A clean split can’t fix bad data. It tests whatever you feed it.
- Survivorship: testing a stock rule on today’s index members leaves out every company that was removed, merged or delisted along the way, and those departures were often the worst performers. Membership changes at each reconstitution, as equal weighted indices shows for one kind of index, so a fair test uses the member list as it stood on each date.
- Look-ahead: earnings figures are stamped with the quarter they cover but published weeks later, and economic data gets revised. A rule that trades on a number before the public could have seen it flatters both halves of the test.
- Costs: commissions, spreads and slippage belong in both halves from the first run. A held-back edge of +0.15R can vanish on slightly worse fills.
Where does a single holdout fall short?
One held-back stretch gives one verdict on one set of years. A walk-forward test repeats the idea across the whole history, tuning on one window, testing on the next and rolling forward, so the same history produces far more untouched trades to judge. The argument for judging a method only on its held-back results, and the objection that old data misleads anyway, is made in full in out-of-sample testing is the only part of a backtest that counts.
Testing a method before money goes in is one of the things a course should teach, and it sits on the list of what to look for in free stock trading courses, while the stock trading hub collects the rest of the material on building and sizing a method.
People also ask
What percentage of data should be out of sample?
No standard exists. Splits around 70/30 or 80/20 are common examples, and the right share depends on how often the method trades. Count the trades the later stretch will hold; a method that trades a few times a year may need a longer history, or a walk-forward test that rolls the split through the whole record.
Can you split trading data randomly for out-of-sample testing?
It is a poor idea for market data. Prices on neighboring days are linked, so a random split puts near-copies of test days into the tuning set and the test result comes out too good. Split by date, keep the held-back stretch after the tuning stretch, and leave a gap between them.
What if the strategy fails out of sample?
Treat the failure as the answer for that idea. Adjusting the rules and rerunning them on the same held-back data turns it into tuning data, and the next pass means little. Start again with a changed idea and fresh data, or extend the history so a new untouched stretch exists.
Sources
- Does Academic Research Destroy Stock Return Predictability?, The Journal of Finance (R. David McLean and Jeffrey Pontiff), vol. 71, no. 1, 2016, pp. 5-32
- … and the Cross-Section of Expected Returns, The Review of Financial Studies (Campbell R. Harvey, Yan Liu and Heqing Zhu), vol. 29, no. 1, 2016, pp. 5-68