How can you avoid overfitting an Expert Advisor?

A trading robot and the concerns of traders attempting to optimize it

How can you avoid overfitting an Expert Advisor?

The dream of automation is to run an Expert Advisor (EA) 24 hours a day while complying with the strict constraints of prop firms such as FTMO. On paper, all you need to do is optimize a few parameters to achieve a smooth upward capital curve. In reality, most robots fail because they have been over-optimized: They know historical data by heart but don't know how to handle changing markets. This over-optimization, also known as overfitting, is the user's number one enemy. In this article, we will understand this phenomenon and learn howto avoid it, in order to objectively adjust your robot and prepare it for prop firm accounts such as FTMO.

Understanding over-optimization and its dangers

An EA MT5 is a code that applies trading rules. When optimizing these rules, we look for the best combination of parameters (moving average periods, oscillator thresholds, stop sizes, etc.) that would have maximized gains on historical data. The danger arises when we adjust too many parameters or try to adapt to all the irregularities of the past. According to a study published on MQL5, the more parameters we optimize, the more the model adapts to data noise and loses its ability to generalize. In practice, this results in a robot that works perfectly in backtesting but fails when executed in forward testing or on another asset. Over-optimization can also be caused by an insufficient data sample: a small number of trades makes the results insignificant and leaves room for chance.

In practical terms, overfitting often results in:

  • An abnormally high profit factor, sometimes exceeding 5
  • An excessively high success rate (> 80%) in historical data and a sharp decline in real-world conditions
  • Extreme sensitivity to minor changes (change of broker, time zone, timeframe, or instrument)
  • A drawdown that explodes as soon as the market moves away from the ideal configuration

If your robot is performing like this, caution is advised! It's better to spend time analyzing its parameters than to burn through a prop firm account.

Common causes of overfitting

1. Too many parameters optimized simultaneously

The desire to add a new indicator to "filter" out bad signals is understandable. However, each additional parameter exponentially increases the number of possible combinations and increases the risk of adapting randomly. Research on MQL5 recommends limiting the optimization phase to less than three parameters at a time. If you want to add a context moving average (for example, to follow the trend on a higher timeframe ), it is advisable to test this element in a separate strategy to prove that it adds value before integrating it into the main strategy. This is exactly the approach we use in our articles on the Donchian Channel MT5: optimal settings for a Breakout robot or on the Breakout MT5 Strategy: how to optimize it for a prop firm : Each filter is validated separately.

2. Data sample too limited

In a backtest, the sample size corresponds to the number of trades used to estimate performance. A sample that is too small (fewer than 500 transactions or less than three years of data) does not allow us to distinguish between a real advantage and a result due to chance. The greater the number of parameters to be optimized, the larger the sample size must be in order for the results to be statistically significant. This is why we recommend using multi-year data and multiplying currency pairs and timeframes: A robot that performs well on EURUSD in H1, H4, and D1, on GBPUSD and USDJPY, is probably more robust than a robot on a single pair in M15. To learn more about sample size, see our article on Mastering the decisive levels strategy.

3. Poor quality or unrepresentative data

A backtest is only valuable if the data reflects reality. Corrupted historical data, without precise ticks or realistic spreads, distorts the results. The WeMasterTrade website reminds us that the use of high-quality data and the simulation of real conditions (slippage, variable spreads, execution times) are essential for a relevant evaluation. The MT5 Strategy Tester allows you to activate real tick models and add execution delays. For prop firm traders, it is essential to adjust the spread and leverage to those of the FTMO evaluation. We discuss this topic in more depth in our article Robotic money management for FTMO: essential rules.

4. Optimization on a single instrument or timeframe

A model based on a single instrument is fragile. The ThinkMarkets team reminds you to use in-sample and out-of-sample data for backtesting, test on multiple instruments and timeframes, and avoid over-optimizing your strategy. In other words: don't just optimize your robot on EURUSD in H1. Also test it on GBPUSD, AUDJPY, EURUSD, etc., and observe its performance in M30, H4, and D1. Geographic and temporal diversification reveals the true robustness of an algorithm.

5. Ignore out-of-sample validation and forward testing

Optimization must be followed by testing over a period that the robot has never seen (out-of-sample). This forward test can be performed by moving the optimization window (walk-forward technique) or by using a demo account. An article by Titan FX points out that excessive optimization on historical data leads to settings that fail in forward testing and in real-world conditions. The "Forward Optimization" feature in MT5 allows you to reserve part of the history to test the robot without re-optimizing. After optimization, it is essential to monitor performance on this unused segment and repeat the exercise over different periods.

6. Lack of control over actual conditions

A robot may seem perfect in backtesting if you ignore fees and liquidity. In real life, every trade is subject to slippage, the difference between the asked price and the obtained price, and a variable spread. The same source, WeMasterTrade, emphasizes that your backtest must reflect real conditions: Incorporate execution times, dynamic spreads, and commissions. The closer your simulation is to reality, the more you will filter out over-optimized settings.

How to prevent overfitting when creating an EA

Now that we have identified the causes, here is a practical guide to avoiding over-optimization when developing an MT5 EA intended to pass an FTMO challenge.

1. Define simple and robust rules

Before setting up your parameters, lay the groundwork: What are your entry and exit signals, your risk/reward ratio, and your stop level? An effective strategy does not need twenty indicators. In our article What is an MT5 Expert Advisor?, we remind you that a high-performance robot focuses on a few simple and measurable criteria. The clearer the logic, the less likely you are to add "band-aids" that over-optimize.

2. Use a sequential optimization process

Break down optimization into steps: Start by adjusting one or two major parameters (e.g., stop size and trailing stop multiplier). Then lock them in and optimize another secondary parameter (trend filter period, entry interval, etc.). This sequential approach, recommended by experts, reduces dimensionality and avoids mixing too many variables.

3. Test on various samples and under various conditions

Divide your data into three segments:

  1. In-sample training sample: Period used to optimize parameters.
  2. Validation sample (out-of-sample): Period reserved for testing robustness without re-optimizing.
  3. Forward test: Real-time or demo account period to observe the robot's behavior in the current market. The Titan FX guide reminds users that this is the only way to identify settings that are too tailored to the past.

You can also implement a walk-forward analysis, which involves sliding the optimization and validation window over the history. This process multiplies the tests and highlights settings that work in multiple environments. Remember to check performance on different instruments and timeframes to detect any hidden weaknesses.

4. Check statistical significance

A strategy is only reliable if its results are statistically significant. As explained in the MQL5 article, a small sample leaves too much room for chance and gives misleading results. Aim for at least 500 trades or three years of data for a system with few parameters. If your strategy has more than three optimized variables, increase the sample to several thousand trades. The sample size should increase with the number of degrees of freedom.

5. Incorporate additional robustness tests

In addition to walk-forward, other techniques can be used to assess the robustness of an EA:

  • Monte Carlo tests: By permuting the order of trades, randomly modifying spreads, or reducing the sample size, we observe whether performance remains consistent. Excessive deviations reveal fragility.
  • Parameter disruption: Slightly change the optimized values and verify that the EA remains profitable. A robot that cannot handle small variations is likely over-optimized.
  • Cross-backtesting: Test the robot on uncorrelated markets to assess its generalization.

6. Simulate actual costs and constraints

Backtest results must include fees, liquidity, and trading conditions of prop firms. Add realistic slippage, variable spreads, and typical broker commissions. Configure your MT5 model to use real ticks and activate a delay similar to that of your VPS server. This approach is recommended by WeMasterTrade and brings you closer to reality: a robot that can withstand these constraints is more likely to succeed in a real account.

7. Monitor performance indicators and remain critical

During optimization, track several metrics: profit factor, maximum drawdown, reward/risk ratio, mathematical expectation, stability of gains, and number of trades. Be wary of configurations that produce extravagant returns or a ridiculously low number of trades. Also compare the results between the backtest and the forward test: if the profit factor collapses or the success rate drops, this is a sign of over-optimization. To learn more about this topic, read our article Reward/risk ratio: the key to truly profitable trading.

8. Validate in demo account and live account

Once optimization and testing are complete, place your EA on a demo account and let it run for several weeks. Follow the planned money management strategy and monitor the consistency of the results. Then move on to a small real account with reduced risks. This phase allows you to check the parameters under real conditions of latency, slippage, and psychology. Don't forget to adjust your position size according to volatility, as explained in our article How to set a Stop Loss based on price volatility?.

9. Establish a risk management framework

Over-optimization is unforgiving when it encounters poorly managed risk. Set strict money management rules: fixed risk as a percentage of capital, maximum daily drawdown, and mandatory pause after a series of losses. Refer to our guides on algorithmic money management andthe importance of a global MT5 mutex for securing multi-symbol robots. A robust EA relies on robust risk management.

10. Regularly review the code and remain humble

A robot remains a representation of a changing market. A parameter that is optimal today may become obsolete tomorrow. Plan periodic re-optimization sessions, but limit their frequency to avoid overfitting. Monitor logic errors, update libraries, and simplify the code if necessary. Finally, remember that overfitting is insidious: a good developer must know when to recognize that their EA has lost its advantage and be prepared to abandon it.

Conclusion: A robust EA is a modest EA.

Avoiding overfitting is not just a matter of technique, it's a state of mind. You have to accept that perfection does not exist, that past performance is no guarantee of future results, and that simplicity is often more powerful than a mille-feuille of indicators. By combining quality data, parsimonious optimization, out-of-sample validation, multi-instrument testing, rigorous risk management, and constant vigilance, you will greatly increase your chances of successfully passing an FTMO challenge. Be methodical and patient: an EA that progresses slowly but surely is better than a "miracle" robot that explodes in the first month.

Frequently Asked Questions

What is over-optimization of an EA?

Overfitting consists of adjusting an Expert Advisor too precisely to past data, to the point where it reproduces market noise rather than actual behavior. An over-optimized EA may display a perfect capital curve in backtesting, but it collapses when confronted with new contexts or instruments. The main cause is the use of too many parameters and an insufficient data sample.

How many parameters should be optimized at once?

It is advisable to limit optimization to two or three parameters simultaneously. If you want to integrate a new indicator (for example, a moving average of context), test it separately to verify that it provides an advantage. Then add it to your strategy without re-optimizing it. This method reduces dimensionality and decreases the risk of overfitting.

What sample size is needed for a reliable backtest?

The larger the sample size, the more reliable the results. A rule of thumb is to test at least 500 trades or three years of data for a simple strategy. For each additional parameter optimized, increase the number of trades: a system with five parameters may require several thousand trades to achieve a high level of statistical significance.

How to validate an EA and avoid overfitting?

Apply a three-step method: (1) Optimize your robot on a training sample with a limited number of parameters; (2) Test it on a validation sample without adjusting the parameters to check its robustness; (3) Perform a forward test on a demo account or with real money to observe its behavior in real conditions. Repeat these steps over several periods and instruments. Finally, simulate real conditions (slippage, spreads, commissions) and monitor your performance metrics to detect any drift.

Leave a comment

Your e-mail address will not be published. Required fields are marked with *.

Back to top