In-Sample vs Out-of-Sample Testing: How to Avoid Overfitting a Trading Strategy
Almost every beginner finds a "perfect" setting at some point: the moving-average length, the stop distance or the time filter that turns a mediocre backtest into a beautiful one. Then it is switched to a demo account and the beauty disappears. This is overfitting, also called curve fitting, and the standard defence is to keep some data hidden from the search. This guide explains in-sample and out-of-sample testing, the maths of why searching hurts you, and a simple routine to apply on a demo account. All figures below are made-up demo and past examples for training; nothing here is a signal or a recommendation.
What in-sample and out-of-sample mean
- In-sample (IS) data is the part of your history you are allowed to look at while designing and tuning the method.
- Out-of-sample (OOS) data is a later part you do not touch until the design is frozen. You test once, write down the result, and accept it.
The idea is simple. A method tuned on a set of data will always look good on that data, because you chose the settings that fit it, noise included. Only data that played no part in the choice can tell you whether the method found something real or just memorised the past.
Why searching creates false winners
Every time you try another setting, you give luck another chance to look like skill. Suppose a setting that has no real edge has a 5% chance of looking "significant" on a given history. Test several settings and the chance that at least one looks good grows fast:
| Settings tested | Chance at least one looks significant (1 - 0.95^n) |
|---|---|
| 1 | 5.0% |
| 5 | 22.6% |
| 20 | 64.2% |
| 100 | 99.4% |
These figures assume independent settings; real settings are correlated, so exact numbers differ, but the direction does not. The more you search, the more a good-looking result is expected even when nothing is there. That is why the number of variations you tried matters as much as the result you kept, and why it belongs in your journal.
Three ways to split your data
- Simple split. Use the first 60 to 70 percent of the history for design and keep the last 30 to 40 percent untouched. Keep the order in time; never shuffle trades from the future into the design set.
- Walk-forward. Tune on a window (for example two years), test on the next window (six months), then slide both forward and repeat. You collect several out-of-sample slices that were each unseen when the settings were chosen. It costs more work but shows how the method behaves across different market conditions.
- Forward test on a demo account. The cleanest out-of-sample data is data that does not exist yet. Freeze the rules, write them down with the date, and record every trade on a demo account.
The out-of-sample result is a verdict, not feedback. If you look at it, change the rules and retest, it has become in-sample.
Worked example with demo numbers
A demo trader has 100 past trades of one rule set, measured in R-multiples (profit divided by the amount risked). Costs are already included in each result. The first 60 trades are in-sample and the last 40 are out-of-sample.
Demo A, a rule with three settings tried. The best of three combinations shows an average of +0.35R per trade over the 60 in-sample trades. The 40 out-of-sample trades, run once with those settings, average +0.28R. The result shrank, which is normal, but it stayed clearly positive and the worst losing streak was in line with the in-sample one. This is a candidate to forward test on demo, still without any promise.
Demo B, a rule with 200 settings tried. The trader scanned 200 combinations of entry filter, stop distance and time of day. The best looked excellent: +0.45R per trade in-sample. On the 40 out-of-sample trades it averaged -0.05R. Nothing about the market changed; the search simply selected the combination that had fitted the noise best. With 200 tries, the chance of finding something flattering by luck was close to certain.
| Settings tried | In-sample avg R (60 trades) | Out-of-sample avg R (40 trades) | Verdict | |
|---|---|---|---|---|
| Demo A | 3 | +0.35 | +0.28 | Held up, keep forward testing |
| Demo B | 200 | +0.45 | -0.05 | Faded, discard |
Notice that Demo B had the better in-sample figure. A better backtest is not a better method; it is often just a longer search. Remember sample size too: 40 trades is small, so a positive out-of-sample result is encouraging evidence, not proof (see our guide on how many trades you need to test a strategy).
Warning signs of curve fitting
- Too many parameters for the number of trades. A rule with six tunable settings tested on 80 trades has little room to be honest.
- Fragile settings. If a moving average of 21 is great but 20 and 22 are poor, you probably found a spike of luck. Real edges usually sit on a plateau where neighbouring values behave similarly.
- Results that depend on one or two trades. Remove the best two trades and recompute; a rule that dies without them was an outlier, not a method.
A routine you can follow on a demo account
- Write the idea and the rules in your journal before testing. Fix the list of settings you are allowed to try.
- Count and record every variation you test, including the ones you discard.
- Reserve the last third of the data, or a block of future demo trades, as out-of-sample.
- Tune on the in-sample part only. Choose a setting on a plateau, not a peak.
- Run the out-of-sample test once. Record average R, win rate, profit factor and maximum drawdown next to the in-sample figures.
- Accept the verdict. If it failed, you may design a new idea, but then the old out-of-sample block is spent.
Frequently asked questions
What is the difference between in-sample and out-of-sample testing?
In-sample data is used to design and tune a trading method; out-of-sample data is kept aside and used once afterwards to check whether the method works on data it was not fitted to. The second is much better evidence of a real edge.
How much data should I keep out-of-sample?
A common rule of thumb is to hold back roughly 30 to 40 percent of the history, in time order, as long as enough trades remain in both parts to be meaningful. There is no official standard; the more settings you tried, the more you need.
What is walk-forward testing?
Walk-forward testing repeatedly tunes a method on one window of data and tests it on the next unseen window, then moves both windows forward. It produces several out-of-sample results instead of one, which shows how stable the method is over time.
Can I reuse out-of-sample data after it fails?
Not honestly. Once you have looked at the result and changed the rules because of it, that data has influenced the design and is now in-sample. You need new data, such as later history or forward demo trades.
Is a good out-of-sample result a guarantee?
No. Markets change, samples are small and costs vary. A good out-of-sample result lowers the chance that you fitted noise; it does not promise future performance.
Practise it in the Academy
The trading lessons on strategy testing and journalling include demo practicals where you record variations, split results and compare in-sample and out-of-sample metrics. They are free to open and need no sign-up.
Related Academy lessons
- Trading expert: strategy testing, drawdown control and psychology of discipline
- Trading practitioner: journalling, review metrics and a demo practical
- Trading expert: advanced practical on a demo account
Test yourself: free 5-question quiz on this topic — no sign-up
More guides
- PMS Plan Indicators and Thresholds Under MDR Annex III: Worked Examples
- Losing Streak Probability: How Many Losses in a Row Should You Expect?
- MDCG 2019-16 Cybersecurity Under EU MDR: What to Document for Annex I 17.2 and 17.4
- Daily Loss Limit in a Trading Plan: How to Set One (With a Demo Example)
- EN ISO 14155:2026 and the New MDR Harmonised Standards: What Manufacturers Should Do
- What Is Profit Factor? How to Calculate It and What Counts as Good