Learn › Q&A › How do I avoid overfitting my trading strategy?

How do I avoid overfitting my trading strategy?

Overfitting is tuning until the strategy memorizes the past's noise. Defenses, in order of value: (1) fewer parameters — every knob is a chance to fit noise; two or three beats ten; (2) test out-of-sample — develop on one period, validate on another you never touched during tuning; (3) demand robustness — if RSI-30 works but RSI-28 and RSI-32 fail, you found noise, not signal; (4) prefer round numbers and conventional values over precisely optimized ones (14, 30, 70 — not 13, 27.5, 71); (5) count your attempts — after fifty variations, your 'best' result is partly selection luck. The strategy that survives these tests will look less impressive in-sample and more honest out of it. That's the trade you want.

Overfitting means the rules were shaped to fit the quirks of one stretch of history. The backtest then reports a result the quirks produced, and the result does not carry over to data the rules have not seen. The usual signs are many parameters, an unusually smooth equity curve, and a result that falls apart when any number in the rules moves a little. The overfitting entry in the glossary gives the short definition. The answer below uses measured numbers from DeployQuant's backtests to show where the risk comes from and which habits reduce it.

The data window is 2021-01-04 to 2026-10-02, which is 5.74 years of minute-resolution US market data. Starting cash is $10,000, there is no margin, and the headline runs carry no fees or slippage. The library holds 12 strategies and 59 ETFs. Every number below comes from that one window. That fact is itself the first lesson: a single window of under six years is a small sample, and any rule tuned on it has only that history to be right about.

The short list of defenses, in the order they tend to matter: use few parameters, test on data you did not tune on, check that neighbouring parameter values give similar results, prefer conventional values, and keep count of how many variations you tried. Each one has a measurement from the library attached.

Why few parameters matter

Each parameter is a free choice, and each free choice gives the rules room to bend toward the data. A rule with one number, such as the 200-day average in the 200-day regime filter, has few ways to bend. A rule with a lookback, an entry threshold, an exit threshold, a profit target, a stop and a time exit has many. The weekly 7% target uses a weekly entry, a profit target of 7% and a Thursday afternoon exit. The dip buyer combines a 20-day high, a 10% drop and an 8% target.

The library results show that more moving parts do not buy a better outcome by themselves. The weekly 7% target had a median CAGR of 6.61% across the 59 ETFs and beat buy-and-hold on 34 of them. The dip buyer had a median CAGR of 1.33% and beat buy-and-hold on 22. Both rules were fixed in advance and run on the same funds, so neither number is a tuned result. The point is that complexity is not evidence. A rule earns trust from how it behaves across many funds, not from how many conditions it contains.

A practical test is to count the numbers a rule needs and ask which of them you picked by looking at the chart. A moving-average length that you took from a textbook is one kind of number. A 10% drop that you chose because it produced the best curve on the fund you were studying is another kind. The second kind is where overfitting enters.

Testing on data you did not use to tune

The cleanest defense is to split the history. Develop the rules on one period, then run them once on a period you have not looked at. This is out-of-sample testing. A related method is walk-forward analysis, which tunes on one window, tests on the next, and rolls forward.

The library window shows why the split matters. SPY returned 30.46% in 2021, then lost 18.16% in 2022, then gained 26.18% in 2023. A rule tuned only on 2021 sees a market that went up. A rule tuned on 2022 sees one that went down. Neither tells you what the rule does in the other kind of year. TQQQ moved from a gain of 90.95% in 2021 to a loss of 79.07% in 2022 and back to a gain of 198.32% in 2023. A parameter set picked on any one of those years would be picked for that year.

DeployQuant's backtests cover all of 2021 to 2026, which includes the 2022 drawdown, so the window holds at least one bad stretch. It does not contain a held-out period. If you tune a rule on this window and then read the same window's result, the result is in-sample. A held-out test needs a period or a set of funds the tuning never touched. A different fund is a useful holdout: tune on SPY, then run the same rule on IWM without changing anything.

Paper trading is the last holdout. It runs the rules on live market data that did not exist when the rules were written, and DeployQuant paper trading needs no broker. The data is strictly out of sample because it arrives after the strategy is fixed.

Checking the neighbours of a parameter

A real effect shows up across a range of settings. A noise fit shows up at one setting and disappears next door. If an RSI(14) rule that buys below 30 works, a rule that buys below 28 or 32 should give a similar picture. If only 30 works, the result probably comes from a handful of specific trades.

The size of the sample behind the result decides how much a neighbouring setting can move it. On SPY the 14-day RSI closed below 30 on only 18 sessions in the whole window. The median 5-day forward return after those sessions was 3.74%, against a baseline median of 0.42% for all sessions. With 18 events, moving the threshold from 30 to 32 would add or remove a handful of them, and the median could shift a lot. A statistic built on 18 observations is fragile in exactly the way overfitting needs.

The faster signal has more events. The RSI(2) below 10 condition fired on 146 SPY sessions, with a median 5-day forward return of 0.89% and a 20-day median of 2.29%, against a baseline 20-day median of 1.73%. A larger count gives the neighbour test more power. The RSI(2) snapback strategy built on that signal had a median of 166 round trips across the 59 ETFs, and it beat buy-and-hold on 34 funds. The RSI mean reversion strategy, which waits for RSI(14) under 30, had a median of 15 round trips and beat buy-and-hold on 25.

Strategy pages in the library show parameter variants next to the headline run, so a reader can see how a rule behaves when its numbers move. A flat response to small changes is the pattern worth looking for.

Conventional values over optimized ones

Values like 14, 30, 70, 50 and 200 are common because they are the defaults in most charting tools. Using them means you did not search for them. A value such as 13 or 27.5 usually appears because a search found it.

The library rules use conventional settings on purpose. The golden cross pairs the 50-day and 200-day averages. The SMA 10/50 trend uses 10 and 50. The EMA 12/26 trend uses the pair behind the common MACD setup. None of them was tuned to a fund. Their medians show what untuned rules do: the golden cross had a median CAGR of 2.05% and a median drawdown of 21.84%, the SMA 10/50 trend had 2.12% and 23.02%, and the EMA 12/26 trend had 2.76% and 22.87%. The three are close together, which is what you would expect when the settings are not doing the work.

These medians are modest. An untuned rule on a fund it was not picked for usually looks modest. The more impressive a tuned result looks, the larger the share of it that may be fit.

Counting how many things you tried

Every variation you test is a draw. If you test one rule on one fund, a good result is one observation. If you test many rules on many funds and report the best, the best result is partly the maximum of many draws. The library makes this visible. It holds 12 strategies run on 59 ETFs, and the spread of outcomes across funds is wide.

Take the monthly cycle, which buys the first session of each month and sells the last. Its median CAGR across the 59 ETFs was 5.46%, and its median drawdown was 36.63%. It beat buy-and-hold on only 14 of 59. Somewhere in that set there are funds where the rule looks excellent, and a reader who looked only at those would see a calendar effect. The median says the typical fund did not behave that way. The seasonality entry explains why calendar patterns in small samples are easy to overread.

Compare the momentum strategies. The 3-month momentum rule had a median CAGR of 0% and beat buy-and-hold on 20 funds. The momentum breakout rule also had a median CAGR of 0% and beat buy-and-hold on 21. If you had run both on a dozen funds and kept the best three, you would have a good-looking report from rules whose typical result is flat.

The habit that follows is to write down every variation, including the losers. When a result survives a record of how many attempts it took to find, it carries more weight. When the record is long, the best result needs a discount that no formula can give you.

Small samples and rare events

Rules that trade rarely rest on few observations. The golden cross had a median of 3 round trips across the 59 ETFs. The dip buyer had a median of 2. A rule with 2 trades per fund has too few trades to say much, whatever the return was. A good result from 2 trades can come from one lucky entry.

The market examples show how fast sample sizes shrink under a strict filter. QQQ closed with RSI(14) below 30 on 15 sessions, SPY on 18, TQQQ on 18, and IWM on 33. TLT had 61 such sessions and UVXY had 116. The forward returns after those sessions differ sharply by fund. After an RSI(14) reading under 30, the median 5-day return was 3.69% for QQQ, 10.3% for TQQQ and 1.78% for IWM. For TLT it was 0.02%, and for UVXY it was -1.79%. The same rule means different things on different funds. A rule tuned on the funds where oversold readings recovered would look good and then fail on the ones where they did not.

The 20-day figures point the same way. The median 20-day return after RSI(14) under 30 was 2.86% on SPY and 6.9% on TQQQ. On TLT it was -1.63% and on UVXY it was -5.82%. TLT and UVXY drifted down over the window, so a dip there was often followed by more decline.

Regimes in one window

A rule can be tuned to a regime instead of to a market. The window contains one large equity drawdown. SPY fell 24.51% from 2022-01-03 to 2022-10-12 and recovered on 2023-12-13. QQQ fell 35% from 2021-11-19 to 2022-11-03. TQQQ fell 81.68% from 2021-11-19 to 2022-12-28 and recovered on 2024-12-04. The window also holds a calm, rising stretch from 2023 onward.

A rule with a 200-day filter spends its value in 2022. SPY had 30 crossings of its 200-day average in the window and spent 77.99% of sessions above it. TLT crossed 95 times and spent 35.18% of sessions above it. TLT lost 44.06% from 2021-01-04 to 2023-10-19 and had not recovered by the end of the window. A trend rule that looks good on TLT here is looking at a single long decline. UVXY lost 99.94% over the window, and buy-and-hold on it ended at $215.70 from $10,000. A short-volatility or trend-down rule that works on UVXY is mostly describing the fund's decay.

These are single regimes. A parameter set that did well in 2022 may have done so because 2022 was the only large decline the window held. The way to reduce that risk is to test on a different decade, which the DeployQuant library does not have. Say so in any write-up of a result. State the window, the fund and the number of regimes it contained.

Costs and execution are part of the test

A backtest that ignores costs can flatter a rule that trades often. The headline runs here have no fees or slippage, and cost runs add 5 and 10 basis points. High-turnover rules are the ones most exposed. The RSI(2) snapback had a median of 166 round trips and the weekly 7% target had a median of 106. The monthly cycle had 69. A rule with a high trade count whose edge survives a cost run is more believable than one that only works at zero cost.

Execution assumptions also bear on the answer. Orders fill on minute bars, and prices in the trade lists are adjusted for splits and dividends, so they are lower than the quotes printed at the time. A rule tuned to adjusted prices has been tested against a series no trader saw live. Keep that in mind when a tuned entry level looks precise.

A review routine before trusting a result

A short routine catches most overfitting before money is involved.

The library supports the first six steps with backtests that finish in seconds. DeployQuant allows you to build the rule from blocks, in plain English or in Python, backtest it, read why each trade happened, and then paper trade it. The related question on trusting a backtest covers the other side of the same problem, which is how to read results someone else produced.

Limits of the test

The numbers above describe one window of 5.74 years, daily-decision rules and a specific set of 59 funds. They show how fragile statistics can be at small sample sizes and how much results differ by fund and by year. They do not measure how often a tuned rule fails out of sample, because the library did not run a tuned-versus-held-out comparison. Backtests are hypothetical, and nothing here predicts what a rule will do next.

Frequently asked questions

How do I know if my backtest is overfit?

The common signs are many parameters, a result that changes sharply when a parameter moves a little, and a small number of trades behind a large return. Run the rule on a fund and a period you did not use while tuning. If the result drops a lot, part of the original number was fit to the data.

How many parameters is too many?

No fixed number applies, but each added parameter gives the rules more room to fit noise. The library's simplest rules use one or two numbers, such as the 200-day average in the [200-day regime filter](/learn/strategies/sma200-regime/). Count the numbers in your rule and note which ones you picked by looking at results.

What is out-of-sample testing?

It means running the rules on data that played no part in building them. The data can be a later period, a different fund, or live paper trading. The [out-of-sample testing](/learn/glossary/out-of-sample/) entry defines it, and in-sample results are the best case for any rule.

Why do round numbers like 14, 30 and 70 help?

They are standard settings, so you did not search for them. A value found by a search, such as 13 or 27.5, may owe its result to the sample it was found on. The library's rules use conventional values for that reason.

Does a longer backtest remove overfitting?

A longer window gives more regimes and more trades, which makes a fit to noise harder. It does not remove the risk if you tune on the whole window. The DeployQuant window is 2021-01-04 to 2026-10-02, and it has one large equity drawdown, in 2022.

Can I test for overfitting in DeployQuant?

You can backtest in seconds, change any parameter, and compare runs on different funds and periods. After that you can paper trade the strategy on live data without a broker. Paper trading is the closest check on data that did not exist when the rule was written.

Is a good backtest evidence that a strategy will work live?

A backtest is hypothetical and treats the past as the only test. The [trustworthy backtest](/learn/answers/how-to-tell-if-backtest-is-trustworthy/) answer lists what to check. Treat any result as an upper bound until the rules have run on data they have not seen.

Run your first strategy yourself, free →

Build it from blocks (or type it in English), backtest it on 5.7 years of minute data in seconds, tweak any parameter, then paper trade it on live data. No card, no broker needed to start.

Terms used here

OverfittingdefinitionOut-of-Sample TestingdefinitionWalk-Forward Analysisdefinition

Related questions

How can you tell if a backtest is trustworthy?answered

Backtests are hypothetical, computed by DeployQuant's engine on minute-resolution consolidated US market data (2021-01-04 to 2026-10-02, $10,000 starting capital, no margin, no fees or slippage in the headline run; buy-and-hold puts 98% of the account in at the first open, as the templates do) and do not guarantee future results. Nothing on this page is investment advice. Live trading involves risk of loss.