DEV Community

Cover image for Genetic algorithms for trading strategy optimization
Weston Carnes
Weston Carnes

Posted on • Originally published at stellarbytecapital.com

Genetic algorithms for trading strategy optimization

Cross-post. Original: stellarbytecapital.com/blog/genetic-algorithm-trading-strategy

A genetic algorithm is a wonderful way to find a trading strategy that made money in the past and will never make money again. Point it at a backtest, let it breed for a few hundred generations, and it will hand you a gorgeous equity curve built entirely out of noise. The technique isn't the problem — the way most people wire it up is. Done with discipline, a genetic algorithm (GA) is one of the best tools for optimizing a real edge. Done naively, it's the fastest overfitting machine ever invented.

Why a GA at all

A trading strategy usually has a handful of parameters: lookback windows, entry/exit thresholds, sizing, stops. The search space is large, bumpy, and non-differentiable — you can't take a clean gradient through a backtest. Grid search explodes; hand-tuning is slow and biased.

A GA fits this shape: each candidate strategy is an individual, scored by a fitness function, with strong ones kept and bred via crossover (mix two parents' parameters) and mutation (perturb a value). Over generations the population drifts toward high-fitness regions — no gradient required.

The trap: the fitness function is the strategy

A GA doesn't optimize your strategy — it optimizes your fitness function, ruthlessly and literally. Whatever you reward, it maximizes, including the parts you didn't mean. Reward raw backtest return, and it finds the one parameter set that caught three lucky spikes and levered into them. Most "GA overfitting" is really fitness misspecification.

A fitness function should reward:

  • Risk-adjusted return, not raw return (Sharpe/Sortino base, so it can't win by cranking leverage).
  • Consistency across sub-periods — score on several time slices and penalize variance between them.
  • Drawdown and tail risk — explicitly penalize max drawdown and ugly loss streaks.
  • Trade-count sanity — penalize too few (no significance) or too many (fees eat it).
  • Simplicity — a mild penalty on knife-edge parameter values. Robust edges live on plateaus, not spikes.

The real defense: out-of-sample by construction

Even a good fitness function overfits if it sees all your data. The key guardrail: the GA must never be scored on data you'll use to judge the final result.

  • Walk-forward, not one big backtest. Evolve on an in-sample window, measure the winner on the next out-of-sample window it never trained on. Roll forward and repeat. A strategy profitable across many out-of-sample windows has something real.
  • Hold out a final vault. Keep a recent slice the GA — and you — never touch during development. If performance falls off a cliff there, the "edge" was overfit, full stop.

Engineering it so it doesn't lie to you

The GA is only as trustworthy as the backtest underneath it. Two non-negotiables:

  • The strategy under evolution is a pure function — market state in, decision out, no network/clock/hidden state. Otherwise its fitness score is non-deterministic and the GA optimizes noise.
  • Backtest and live share one code path — no point evolving against a backtest that behaves differently in production.

Knobs that matter

  • Mutation rate that decays over generations — explore early, refine late.
  • Elitism — carry the best few individuals unchanged so you never lose your champion.
  • Diversity pressure — penalize populations that all look alike, so the GA can jump basins.
  • Reproducibility — seed the randomness and log every generation.

A backtest tells you what would have happened. Out-of-sample discipline tells you whether the strategy learned a pattern or just the past.


We're Xingyao Byte — building quant trading systems, secure AI-execution layers, and payment platforms. Remote, async-first → stellarbytecapital.com

Top comments (0)