Home > Blog > Genetic algorithm optimisation in MT5 EAs

MT5EAOptimizationGenetic AlgorithmStrategy Tester

Genetic algorithm optimisation in MT5 EAs

Published: 2026-10-02Read time: about 3 min
⏱ This article reflects information as of its publish date. EA performance figures (PF, DD, annual return) change with live trading and re-validation — check the latest on the EA pages. See the latest EA results →

A Practical Introduction to Genetic Algorithm Optimization in MT5 EAs

When tuning EA parameters, MT5's Strategy Tester offers two optimization modes: exhaustive search (testing every possible parameter combination) and genetic algorithm (GA).

When the number of combinations is enormous, genetic algorithm is the standard approach for exploring the space in a realistic amount of time. This article explains the fundamentals of genetic algorithm optimization and the key practical points to keep in mind.

Key takeaways

  • If there are more than tens of thousands of combinations, search with a genetic algorithm (GA) instead of exhaustive search
  • Choose recovery factor or Sharpe ratio as the evaluation metric, and exclude solutions with few trades
  • Decide on the values to adopt not from the single top-scoring result, but by checking the clustering of the top 10–20 results, parameter sensitivity and a split-period test

The Optimization Workflow (Overview)

StepWhat to doDetails
1Limit the parameters to optimize to a few at the core of the logicNarrow Down What Gets Optimized
2Look at the number of combinations and decide between exhaustive search and GAWhen to Use Each Method
3In the tester, set "Fast genetic algorithm" and the evaluation metricConfiguring the Strategy Tester
4Run the optimization and check convergence in the "Optimization Results" tabRunning the Optimization
5Remove overfit solutions with a split-period test, a comparison of top solutions and a sensitivity checkAvoiding Overfitting
6If it takes too long, shorten it with parallel agents or the cloudLeveraging Parallel Optimization

What Is a Genetic Algorithm?

A genetic algorithm (GA) is an optimization technique modeled on biological evolution. The process works roughly as follows:

  1. Generate multiple sets of random parameters ("individuals")
  2. Run a backtest for each individual and score it using an evaluation metric (PF, recovery factor, etc.)
  3. Select the top-scoring individuals as "parents" and produce a next generation through crossover and mutation
  4. Repeat this cycle — with each generation, the parameters converge toward higher-scoring values

Where exhaustive search sweeps through every combination, GA narrows its search to promising regions, cutting computation time by orders of magnitude.

When to Use Each Method

Exhaustive searchGenetic algorithm (GA)
How it searchesSweeps through every combinationNarrows the search to promising regions
Suitable scaleFew parameters, fewer than a few thousand combinationsMany parameters, tens of thousands of combinations or more
Computation timeGrows in proportion to the number of combinationsShorter by orders of magnitude

For example, three parameters each stepped across 10 values yields 1,000 combinations — exhaustive search is perfectly fine. But five parameters each stepped across 20 values produces 3.2 million combinations, at which point genetic algorithm becomes necessary.

Configuring the Strategy Tester

To enable optimization, set the following in the Strategy Tester panel:

Selecting the optimization mode and ticking the parameter to optimize
Selecting the optimization mode and ticking the parameter to optimize

  • Optimization: Select "Fast genetic algorithm"
  • Result: Choose an evaluation metric such as "Maximum balance," "Custom max," or "Maximum recovery factor"
  • Inputs tab: Check the parameters you want to optimize and set the start value, end value, and step size

Choosing Your Evaluation Metric

What you define as "good" determines the outcome. Here are the most common metrics and their characteristics:

MetricWhat it measuresCharacteristics
Maximum balance (profit)Maximizes profit aloneIgnores risk, making overfitting easy to stumble into
Recovery factorNet profit ÷ maximum drawdownBalances return and risk
Sharpe ratioAccounts for the stability of returns over timeEmphasizes stability
Custom maxFreely defined inside the EA code (e.g., PF × trade count)Can be designed to fit your goal

Using recovery factor or Sharpe ratio is the most practical approach for finding useful parameters while avoiding overfitting.

Running the Optimization

Once optimization starts, the "Optimization Results" tab fills with high-scoring parameter sets, generation by generation.

MT5's genetic algorithm automatically runs through hundreds of generations, converging toward top solutions.

Optimization Results tab (results for each parameter combination)
Optimization Results tab (results for each parameter combination)

Common Pitfalls

SymptomLikely cause
Converges too quicklyThe parameter search range may be too narrow
Never convergesThe evaluation metric may be inappropriate, or there may be too much noise
Converges to an obviously strange valueResults with very few trades have not been filtered out

You can eliminate low-sample noise solutions by baking a filter into your evaluation function — for example, "exclude any result with fewer than 50 trades."

Avoiding Overfitting

The more you optimize, the closer you get to parameters that look perfect on historical data but fail in live markets. Here are four practical rules to prevent that.

RuleWhat to check
1. Split your data periodWhether results hold up on the later out-of-sample period
2. Compare the top 10–20 resultsWhether the top solutions cluster in a similar region
3. Check parameter sensitivityWhether results stay intact when parameters are shifted ±10–20%
4. Keep some parameters fixedWhether you keep fixed values that are natural for the logic

1. Split Your Data Period

With ten years of historical data, optimize on the first seven years and use the remaining three years as an out-of-sample test. If results deteriorate on the latter period, the strategy has overfit to the earlier one.

2. Compare the Top 10–20 Results

Picking only the single top-scoring result is risky.

Check whether the top solutions cluster in a similar region. If they do, adopt a parameter set near the center of that cluster.

3. Check Parameter Sensitivity

Take your candidate parameters and shift them ±10–20%, then verify that performance does not collapse. If it does, that solution is overfit to a narrow "spike" in the optimization landscape.

4. Keep Some Parameters Fixed

Always retain at least a few parameters that are not optimized — values that are naturally justified by the strategy logic. This reduces the risk of overfitting.

For example, you might optimize the EMA periods while keeping the ATR multiplier fixed.

Leveraging Parallel Optimization

MT5 supports parallel optimization across multiple CPU cores. Increasing the number of agents under "Tester" → "Settings" reduces computation time.

You can also use the MQL5 Cloud Network to run large-scale optimizations on remote agents worldwide, though this incurs costs. The efficient approach is to first narrow down the parameter range locally, then offload the fine-grained sweep to the cloud if needed.

Narrow Down What Gets Optimized

The basic rule is to limit optimization to a small number of parameters at the core of the logic, such as the entry lookback period and the stop-loss distance.

Fixing all other parameters at naturally justified values makes it easier to guard against overfitting.

A backtest's PF can be pushed up almost indefinitely by optimizing more, but the probability of the EA failing in future markets rises along with it. Choosing modest numbers and running them consistently is the practical answer for live trading.

Free EA Download

This site's free EA DONCHIAN ENGINE (USDJPY H1) has a backtest over 9.4 years of USDJPY H1 data (2017-2026, Dukascopy real ticks) with PF 1.50, a 34.5% win rate, max DD of 10.8%, and 631 trades.

See the free EA DONCHIAN ENGINE

We recommend brokers with high execution quality so that backtest and optimization results translate reliably into live trading performance.

View our recommended broker list

📧 Free 8-email course + subscriber-only pack of 13 MT5 indicators

Right after you sign up, you get a download link for a subscriber-only pack of our 13 MT5 indicators: compiled files (.ex5), source code and a setup guide. Then 8 emails on algo-trading essentials, reading backtests correctly and choosing a broker (one a day for the first 5 days, then 7, 14 and 30 days after signing up). You also get notified before each price rise of the versions sold on the MQL5 Market (launch pricing that steps up as copies sell). Our EAs are free on this site with a live account at a partner broker.

* Privacy strictly protected. You can unsubscribe at any time.

Have a question about EAs or FX?

You can post it on the Nattzy trading forum (free sign-up). Questions beyond this article are welcome too — EA settings, choosing a broker and more.

Ask on Nattzy ↗

Comments & questions