What is out-of-sample testing, and how do I set one up?
Short answerOut-of-sample testing means judging a trading rule on data that played no part in designing it. Before you look at any results, you split the history in two: an in-sample part where you design and tune the rule, and a later out-of-sample part that you run the finished rule on once. A properly reserved test counts whatever it shows, including when it weakens the case for the rule. The rule passes only if it still makes money there after costs, over enough trades and in more than one kind of market. In a synthetic market built to have no edge, the best of 16 moving-average crossovers made +19.7% in-sample and then lost 9.0% out of sample.
Tuning can find a setting that looks good on past data even when the rule has no edge, so the reserved run is what tells you whether to keep a rule, drop it or gather more data before risking money. Walk-forward testing repeats the idea: design on one window, test on the next, move forward and repeat. Either method stays honest only if you don't change the rule after seeing the test result.
Valmy noteAs specified on 26 November 2026, a Valmy backtest runs one strategy version on a UTC date range you choose, with a pinned dataset and a versioned cost model, so a split can be set up as two backtests. Valmy v1 has no built-in parameter optimizer or walk-forward tool: the specification schedules historical optimization after v1. This lesson teaches the method, and the live app may differ from the specification.
In-sample, out-of-sample and walk-forward: the terms#
In-sample results show how well a rule was fitted to the past; only out-of-sample results estimate how it might behave on data it has not seen. The terms name which part of the history did which job.
- In-sample data is the history you use to design a rule: its indicator, settings and exits. Every choice made while looking at it fits the rule more closely to that history.
- Out-of-sample data is history you set aside before designing anything and use once, at the end. It should come after the in-sample period, because markets change and the test should resemble the future you will trade in.
- Overfitting is fitting a rule to accidents of the in-sample data rather than to a pattern that repeats, most often by trying many settings and keeping the best. Why did my backtest work when live trading didn't? explains it.
- Walk-forward testing is a sequence of small splits: tune on a design window, test on the window right after it, then shift both forward. The test windows join into one out-of-sample record.
How to set up a split before you look#
Set up a split in this order, before seeing any results: fix the rule family, choose the split date, fix the costs, write down the search and the pass mark, tune only on the in-sample part, then run the frozen rule once on the rest.
- Decide the rule family first, for example "a long/flat crossover of two simple moving averages". How do I turn something I noticed on a chart into a rule I can test? covers this step.
- Choose the split by date. A common choice is the first two thirds for design and the last third for the test. Keep the test period whole and later in time, not a random set of candles, because neighboring candles share information.
- Fix the costs and the fill rule before tuning, so the search can't favor settings that only work without costs. See How much do fees and slippage change a backtest? and What is look-ahead bias?.
- Write down the search and the pass mark: the settings you will try, how you will pick the winner, and what the test must show, for example "positive after costs over at least 20 round trips".
- Tune only on the in-sample range.
- Run the frozen rule once on the test range and record the result, good or bad.
- If it fails, the test data are used up. The next idea needs data the old one never touched: newer history, or a paper-trading period (How do I paper trade without fooling myself?).
Setting up the split in Valmy, as specified#
Valmy's specification describes what a manual split needs (as of 26 November 2026). A backtest request names the strategy version, the instrument, a UTC date range, the starting balances and a cost-model version. The service reports data coverage before you submit and refuses periods with gaps. Results include the equity series, net return, maximum drawdown and costs split into fees, slippage estimate and funding. Each run pins its dataset revision, fee schedule, slippage model and parameters, so the same version on the same data should give the same decisions.
Run your candidate versions on the in-sample range only, pick one, then run that version once on the out-of-sample range. Every parameter change creates a new immutable version, so the version history doubles as a count of how many settings you tried, which matters, as a later section shows.
Worked example: tuning a crossover on a market with no edge (synthetic data)#
This example tunes a moving-average rule on a market built so that no rule can have an edge. The best of 16 settings made +19.7% in-sample and lost 9.0% out of sample: overfitting when there is nothing real to find.
Assumptions.
- Market A is 180 days of synthetic five-minute candles (51,840 candles). Each candle's change is drawn independently at random, with no trend and a typical size of 0.12% (about 2% a day), so past prices carry no information about future ones.
- The rule is long or flat: buy with all available USDT when the simple moving average (SMA) of the last f closes crosses above the SMA of the last s closes, and sell everything on the cross below. Valmy's v1 language can express it, since the SMA is its one indicator (What does a moving average actually measure?).
- The search tries 16 pairs: fast SMAs of 24, 48, 96 or 144 candles (2 to 12 hours) against slow SMAs of 288, 480, 672 or 960 candles (24 to 80 hours). Each stays within the specification's suggested lookback limit of 1,000 bars. The winner is the pair with the highest in-sample net return.
- Execution. A signal on a closed candle fills at the next candle's open. In this synthetic market each candle opens at the previous close. Each fill pays an assumed 0.05% slippage and a 0.10% fee, the base Binance Spot rate given in How much do trading fees cost me over a year? (as of 26 November 2026), so a round trip costs about 0.30%. Each test starts flat with 10,000 USDT and closes any open position when it ends.
- The split. Days 1 to 120 (34,560 candles) are in-sample. Days 121 to 180 (17,280 candles) are out-of-sample.
In-sample: the best of 16 looks convincing#
On the first 120 days, the 16 pairs returned between −28.4% and +19.7%, with a median of −4.5%. Seven of the 16 made money. The winner, SMA(96) against SMA(672), turned 10,000 USDT into 11,973 USDT (+19.7%) over 36 round trips. Holding the asset over the same days returned +10.1%. A trader might conclude that 8-hour and 56-hour averages capture something real. Here we know they can't: the data have no pattern.
Out of sample: one run, one answer#
Run once on days 121 to 180, the chosen pair lost 9.0% over 21 round trips. None of the 16 pairs made money out of sample: their median was −9.6%, and the chosen pair ranked 8th of 16. Holding returned +1.1% over the same 60 days.
The in-sample ranking told us nothing about the out-of-sample ranking. That is the signature of overfitting: the search found the setting that best matched the accidents of the first 120 days.
Walk-forward testing: design, test, then move forward#
Walk-forward testing repeats the split several times, so one lucky or unlucky test period carries less weight. On Market A, every design window found a profitable setting, yet the six test windows together lost 20.8%. Robert Pardo's book on evaluating trading strategies describes the procedure as walk-forward analysis. Each round re-tunes the rule on a design window, tests the chosen setting on the next window, then moves both forward. A rolling design window keeps a fixed length. An anchored one always starts at the beginning of the data and grows.
On Market A, each round tuned the same 16 pairs on 60 days and tested the winner on the next 20 days. The windows moved forward 20 days at a time, giving six tests that cover days 61 to 180 without overlapping.
| Window | Design days | Test days | Chosen pair | Design return | Test return | Test round trips |
|---|---|---|---|---|---|---|
| 1 | 1–60 | 61–80 | 24 / 960 | +30.7% | −8.7% | 11 |
| 2 | 21–80 | 81–100 | 96 / 960 | +21.0% | −3.0% | 5 |
| 3 | 41–100 | 101–120 | 96 / 960 | +12.3% | +2.5% | 7 |
| 4 | 61–120 | 121–140 | 96 / 672 | +1.9% | −2.4% | 5 |
| 5 | 81–140 | 141–160 | 48 / 288 | +4.3% | −4.4% | 15 |
| 6 | 101–160 | 161–180 | 96 / 672 | +5.6% | −6.4% | 10 |
The design windows averaged +12.6%. Joined together, the six tests returned −20.8% over 53 round trips, and only one of six made money. The search also chose four different pairs in six windows. When the "best" setting keeps changing, the search is usually chasing noise.
The same tests on a market with real trends (synthetic data)#
A test is useful only if it can also pass a rule that works. Market B was built so that a slow crossover has a real edge: it alternates up and down trends lasting 8 to 14 days, each moving 8% to 14%, with the same random noise as Market A. Everything else in the procedure stayed the same, and this time the chosen pair made +38.7% in-sample and +25.2% out of sample.
| Market B | In-sample (days 1–120) | Out-of-sample (days 121–180) |
|---|---|---|
| Chosen pair SMA(48)/SMA(960) | +38.7% (29 round trips) | +25.2% (15 round trips) |
| Median of the 16 pairs | +28.3% | +19.0% |
| Pairs that made money | 16 of 16 | 15 of 16 |
| Holding the asset | +24.2% | +11.3% |
The walk-forward test agreed: five of six test windows made money, and together they returned +38.2% over 50 round trips. The search chose a fast SMA of 48 in five windows and a slow SMA of 960 in four.
A pass tends to look like this: an out-of-sample result weaker than the in-sample one but still positive after costs, neighboring settings that also work, and a search that keeps choosing similar settings. Market A showed the opposite on each count.
How often a single split gets it right (modeled)#
One example could be luck, so the same split was repeated on 100 no-edge markets like A and 100 trending markets like B, each generated with a different seed. Each time, the 16 pairs were tuned on 120 days and the winner was tested on 60.
| 100 modeled markets of each kind | No edge (like A) | Trending (like B) |
|---|---|---|
| Winner made money in-sample | 44 | 93 |
| Median in-sample return of the winner | −3.1% | +24.1% |
| Winner made money out of sample | 22 | 64 |
| Median out-of-sample return of the winner | −8.6% | +5.5% |
The test separates the two kinds of market most of the time, but not always. In 22 of the 100 no-edge markets the tuned rule made money out of sample by luck, and in 36 of the 100 trending markets a rule with a real edge failed its 60-day test. A single out-of-sample test is evidence, not proof. In the no-edge markets, costs of about 0.30% per round trip meant even the best of 16 usually lost money.
Why you get only one honest look at the test set#
The out-of-sample result is honest only the first time you see it. If you then change anything, even one parameter, because of what the test showed, the test data have influenced the design. They are now in-sample data, and the next result on them is just another in-sample result.
Suppose the trader in the example had looked at all 16 out-of-sample results and kept the best one. The "test" would then report −4.1% for SMA(48)/SMA(288) instead of −9.0%. It looks better, but it is no longer a test, because the pair was chosen on the test data.
The number of settings tried also matters. Bailey, Borwein, López de Prado and Zhu (2014) showed that a backtest which hides how many configurations were tried can't be assessed for overfitting, and that a hold-out test alone can't correct for it. In their example, with five years of data, trying about 45 independent configurations is enough for the best one to be expected to show an annualized Sharpe ratio of 1 in-sample even when every one has an expected Sharpe ratio of zero out of sample. With two years of data, 7 tries are enough. The Sharpe ratio, average return divided by its volatility, is explained in How do I compare a strategy with simply holding?.
What luck alone can produce from 16 tries#
Bailey and coauthors give a formula for the best in-sample Sharpe ratio expected from N independent tries that all have no edge. For 16 tries on 120 days of data, it is about 3.1 per year. The winning pair in Market A had an in-sample Sharpe ratio of 2.0, below what luck alone would be expected to produce. The 16 crossovers are related to each other, so the true figure for them is lower than 3.1, but the conclusion stands: a Sharpe ratio of 2.0 from the best of 16 over four months is not evidence of an edge.
Three habits keep the look honest: record every version you test (Valmy's version history does this if you keep each attempt), decide the pass mark before running the test, and use unseen data for any further test.
How much data you need: count trades, not candles#
The amount of evidence in a backtest depends on the number of independent trades, not on the number of candles. Market A's in-sample period held 34,560 five-minute candles, but the winning rule made only 36 round trips. Thirty-six outcomes are a small sample, especially when a few large trades dominate.
Calculating whether an in-sample result could be luck#
A simple check treats each round trip as one draw and asks how many standard errors the average trade lies from zero. For Market A's winner the answer was 0.74, far short of the 2 or more that would be hard to get by chance.
- Average trade: +0.61%. Standard deviation of the 36 trades: 4.92%.
- Standard error of the average: 4.92% ÷ √36 = 0.82%.
- t-statistic: 0.61 ÷ 0.82 ≈ 0.74.
- Trades needed for a t-statistic of 2 at this average and spread: (2 × 4.92 ÷ 0.61)² ≈ 263.
Only 11 of the 36 trades won. The best single trade, held from day 43 to day 52, gained 24.2%, and without it the in-sample result would have been −3.6%. A record that depends on one trade says little about the next 36, and this check even ignores that the rule was the best of 16.
The same arithmetic applies to the test period. Twenty trades with a spread of about 5% per trade can't distinguish an average of +0.5% from zero (t ≈ 0.45). A rule that trades rarely needs a longer test, more markets or patience.
Regime coverage: does the history include the markets you fear?#
A regime is a stretch of market behavior, such as a steady rise, a fall, a sideways range, a calm period or a volatile one. A rule's test says little about regimes it never met. A long/flat trend rule tested only during a rally has not been tested in a slow sideways market, where crossovers keep reversing.
Even Market A, a pure random walk, produced different-looking regimes by chance. In-sample, its price rose from 100 to 147.42 by day 53 and fell to 94.46 by day 109, swings a trend-following rule can ride. Out of sample, it stayed between 100.49 and 114.45, a quieter stretch where crossovers whipsaw. Some of the in-sample gain was the luck of meeting big swings.
Before trusting a split, check each part's buy-and-hold return, largest rise and fall, and volatility. A test period with a strong move each way and a sideways stretch tells you more, and walk-forward testing spreads its tests across the whole history. Valmy's specification limits initial historical data to the allowlisted symbols and a bounded date range (as of 26 November 2026), so check the reported coverage before choosing a split.
What could change the conclusion about an out-of-sample test?#
Luck works both ways. In the modeled markets, 22 of 100 no-edge rules passed a 60-day test and 36 of 100 real edges failed one. A pass on a short test with few trades is weak evidence, and so is a failure.
The market can change after the test. An out-of-sample period is still the past. Sullivan, Timmermann and White (1999) adjusted for data snooping and found that the best of thousands of technical rules beat the benchmark on the Dow Jones Industrial Average from 1897 to 1986, but its advantage did not carry into 1987 to 1996.
Formal corrections exist. White (2000) proposed a "Reality Check" that asks whether the best of many tested models beats a benchmark once the size of the search is accounted for. Such tools need the full list of what was tried, one more reason to keep it.
Leaks and costs spoil both halves. A backtest that uses information not yet available at the time (What is look-ahead bias?) or costs lower than you will pay is wrong in-sample and out of sample alike.
A pass is not a forecast. A rule that passes can still lose money for long stretches. Size positions so that the losses you would accept are survivable, as How much should I risk on one trade? explains.
Check your understanding: is this in-sample result evidence?#
Question. A trader tries 24 parameter combinations of a crossover rule on 90 days of five-minute candles. The best one made 12 round trips, with an average trade of +1.4% and a standard deviation of 4.0% per trade, for a total of about +17%. Its in-sample Sharpe ratio was 2.5.
- What are the standard error of the average trade and its t-statistic?
- Roughly how many trades would the rule need for a t-statistic of 2, if the average and spread stayed the same?
- Using Bailey and coauthors' formula, the best of 24 no-edge tries on 90 days is expected to show a Sharpe ratio of about 4.0. What does that say about the 2.5?
- The trader tests the rule on the next 30 days and gets +3% from 4 round trips. Disappointed, they adjust a parameter and run the same 30 days again, getting +7%. Which result is the out-of-sample result, and how much does it tell?
Show the answers
Answer.
- Standard error = 4.0% ÷ √12 ≈ 1.15%. t-statistic = 1.4 ÷ 1.15 ≈ 1.21, well within what chance produces. (Check: uneven trades compound at roughly the average minus half the variance, 1.4% − (4.0%)² ÷ 2 = 1.32% each, and 1.0132 to the 12th power is about 1.17, so the total of about +17% fits. Compounding the average trade itself, 1.014 to the 12th power ≈ 1.18, would overstate it.)
- (2 × 4.0 ÷ 1.4)² ≈ 33 trades, almost three times as many as the rule made.
- A Sharpe ratio of 2.5 is below what the best of 24 tries would be expected to show with no edge at all. On its own, it is not evidence of an edge.
- The +3% is the out-of-sample result, because it was the first and only run of a rule fixed before the test. The +7% came from a rule adjusted after seeing the test data, so it is an in-sample result. Even the +3% rests on only 4 round trips and says very little. An honest next step is to freeze the rule and gather new data, for example in a paper account.
Related lessons#
- Before this lesson: Why did my backtest work when live trading didn't? Overfitting and data snooping
- Next: How do I paper trade without fooling myself?
- See also: How do I compare a strategy with simply holding?
- See also: How much do fees and slippage change a backtest? and What is look-ahead bias, and how does it get into a backtest?
- See also: What does a moving average actually measure? and How deep can a drawdown get, and how long does recovery take?