Optimization

Genetic Algorithms in Trading Optimization

How genetic algorithms search large parameter spaces: populations, fitness, selection, crossover and mutation. When to use them instead of a grid, their pitfalls, and how to keep results robust.

By AlgoXP4 min readUpdated

Genetic optimization: six generations of parameter sets converging toward higher fitness

When a strategy has several parameters with wide ranges, testing every combination becomes impractical. Five parameters with 20 values each already mean 3.2 million backtests. A genetic algorithm (GA) offers a shortcut: instead of trying everything, it evolves a population of parameter sets toward better results, the way natural selection favours fitter individuals.

The vocabulary

  • Individual - one parameter set, for example {lookback: 30, stop: 2.0, target: 3.5}.
  • Gene - one parameter value inside the set.
  • Fitness - the score of a backtest with that parameter set.
  • Population - the group of individuals evaluated in one generation.
  • Generation - one cycle of evaluation and breeding.

In cTrader’s terminology, each optimisation pass is an individual, the cBot parameters are its genes, and the fitness comes from the optimisation criteria.

How the algorithm works

  1. Initial population. Generate parameter sets at random across the allowed ranges and backtest each.
  2. Selection. Favour individuals with higher fitness as parents for the next generation.
  3. Crossover. Combine genes from two parents: the child takes the lookback from one parent and the stop from the other.
  4. Mutation. Change some genes slightly at random, so the search can explore values that no parent had.
  5. Repeat for many generations.
  6. Stop when fitness stops improving - the population has converged.

Because good parameter sets pass their genes on, the population concentrates in promising areas of the search space far faster than a blind sweep.

When to use a genetic algorithm

Good fit:

  • three or more parameters with wide ranges;
  • a smooth-ish landscape where good areas are reasonably large;
  • a first exploration to find where good settings live.

Poor fit:

  • one or two parameters - a grid is fast and shows everything;
  • final selection - you need to see neighbours to judge stability, which a GA does not systematically test.

A practical pattern: genetic search to locate promising regions, then a grid with coarse steps around them to map the landscape and choose from a plateau.

Pitfalls

It finds lucky peaks efficiently. A GA is designed to find the highest fitness. If the highest fitness is an isolated, overfitted spike, the GA will find it faster than any other method.

Premature convergence. The population can collapse into one local area early and never explore others. Repeated runs with different random starts reveal this.

Fitness gaming. The algorithm optimises exactly what you score. Score net profit, and it maximises risk. Score profit factor, and it finds settings that barely trade. Score win rate, and it finds tiny targets with huge stops.

Non-reproducibility. Two runs can recommend different settings. That is not a bug, but it means a single GA result should never be treated as definitive.

Designing a good fitness function

A robust fitness rewards reward-to-risk and penalises weak evidence. A typical structure:

  • primary score: return divided by maximum equity drawdown, or expectancy per trade;
  • penalty or rejection when the trade count is below a minimum, for example 100;
  • optional penalty for very long time in drawdown.

Platforms support this in different ways. cTrader lets you combine several standard criteria to maximise or minimise into one fitness value, and also allows a custom fitness function written in the cBot code, so rules such as “return zero if fewer than 100 trades” are easy to implement.

Keeping GA results honest

  1. Run it several times with the same settings. Compare where the top passes land.
  2. Inspect neighbours of the best passes with a small grid.
  3. Prefer the centre of a stable region to the single fittest individual.
  4. Validate out of sample or with walk-forward analysis.
  5. Stress-test the final choice: higher costs, Monte Carlo reshuffling, removal of the best trades.

Example

A session-breakout bot has five parameters. A full grid would need about 250,000 passes. A genetic run evaluates around 3,000 passes and converges on box lengths of 4-6 hours, stop multiples of 1.2-1.6 ATR and targets of 1.8-2.4 times the box height. Two further runs land in the same region. A coarse grid across that region (about 100 passes) shows a broad plateau, and its centre holds up on the final two years of data. The GA did the exploration; the grid and out-of-sample test did the judging.

Key takeaways

  • Genetic algorithms evolve parameter sets through selection, crossover and mutation.
  • They are fast for large search spaces but excel at finding lucky peaks too.
  • Design the fitness function carefully: risk-adjusted, with a minimum trade count.
  • Use GA to explore, a grid to examine stability, and out-of-sample data to decide.

Related: what strategy optimisation is and parameter stability analysis.

FAQ

Is a genetic algorithm better than a grid search?

It is faster for large search spaces because it does not test every combination. A grid is exhaustive and shows the full landscape, which is better for small spaces and for checking stability. Many traders use a genetic search to find promising areas, then a grid around them.

Why does the genetic algorithm give different results each time?

It starts from a random population and uses random crossover and mutation. Different runs can converge on different good areas. If repeated runs agree, the area is likely robust; if they disagree widely, the landscape is noisy.

What fitness function should I use?

One that rewards risk-adjusted performance and penalises small samples, for example return over drawdown with a minimum trade count. A naive fitness such as net profit leads the algorithm straight to the riskiest settings.

Sources and further reading

  1. cTrader Help: Genetic algorithm - help.ctrader.com
  2. cTrader Help: Optimise a cBot - help.ctrader.com
  3. cTrader Help: Custom fitness functions - help.ctrader.com