Genetic Algorithm Trading: How GAs Build and Test Strategies

    Yes, genetic algorithms can build trading strategies: they breed and score thousands of candidate rule sets on past data, keeping the fittest. Genetic algorithm trading software does the search fast, but every result still needs testing on data the search never saw.

    By GSB Pro · Updated

    What is a genetic algorithm in trading?

    A genetic algorithm (GA) is a search method borrowed from evolution. Instead of testing every possible strategy, it starts with a random batch, keeps the better ones, and breeds new ones from them.

    That matters because the number of possible strategies is enormous. A strategy might combine several indicators, each with its own settings, plus entry, exit and stop rules. Testing every combination one by one would take far too long.

    A GA does not guarantee it finds the single best strategy. It finds good candidates quickly, and that speed is both its value and its main risk. To see a GA at work on futures data, look at GSB's genetic search.

    How a genetic algorithm works, in plain English

    Four ideas do most of the work:

    • Population. A batch of candidate strategies, each with its own mix of rules and settings. These are its "genes".
    • Fitness. One score that ranks every candidate, worked out from a backtest. Net profit, profit factor or a mix of measures are common choices.
    • Crossover. Two good candidates are combined, so the child takes some genes from each parent.
    • Mutation. A small random change, such as a different indicator length. It keeps the search from getting stuck on one idea.

    Each round is called a generation. The fitter candidates breed, the weaker ones drop out, and the process repeats. Over many generations the best fitness rises quickly at first, then flattens as better strategies get harder to find.

    Because the search starts from random candidates, two runs with identical settings usually give different results. That is useful for variety, but it means one run tells you little on its own.

    Genetic optimisation vs genetic programming

    These two terms are often mixed up. In general use, the difference is what the algorithm is allowed to change.

    Genetic optimisationGenetic programming
    What evolvesParameter values in rules you already wroteThe rules themselves, often as expression trees
    Who designs the logicYouThe search
    Typical useTuning a moving-average length or stop sizeDiscovering new combinations of conditions
    Main riskOver-tuning settings to past dataOver-tuning settings and logic, with far more combinations tried

    Many strategy generators sit between the two. They assemble strategies from a library of building blocks, such as built-in indicators, and tune those blocks' settings at the same time. Each tool works differently, so check its own documentation.

    The wider the search, the larger the data-mining risk. A tool that can change the logic as well as the settings tries far more ideas, so it needs stricter testing afterwards.

    The data-mining risk, and how to control it

    A GA is very good at finding strategies that fit past data, including some that only fit by luck. Test enough combinations and a few will show a smooth equity curve purely by chance. This is called data mining bias or curve fitting.

    The fitness score cannot tell a lucky fit from a real edge, because both look equally good on the build data. Control comes from what you do around the search:

    • Score on training data only. Keep test data and a recent untouched period that the search never sees.
    • Use hard filters. Reject candidates with too few trades, a low profit factor or an uneven equity curve, whatever their fitness.
    • Keep the search space tight. Fewer indicators and options leave less room to fit noise.
    • Repeat runs. Because results vary, compare settings over several identical runs, not one.
    • Walk forward the finalists. See our walk-forward analysis guide.
    • Judge groups, not one winner. Look at how similar strategies perform together, not just the best one.
    • Include realistic costs. Commission and slippage remove many fragile strategies early.

    How GSB applies genetic search

    Genetic System Builder (GSB), made by Peter Zwag at Trademaid, uses a genetic algorithm to build futures trading systems. It builds from its library of built-in indicators, such as RSI, DMI, ROC, CCI, Momentum, TRIX and roofing filters. You set up the market and data; GSB searches; you review and test what it finds. The genetic search feature page shows the process with screenshots.

    The genes. A system's genes are on its Parameters tab. They are each indicator's name, its data stream, its parameter values and its weight. The GA mixes and tweaks these pieces inside the limits your settings file sets. So GSB searches which indicators to use as well as their settings, within a structure you define.

    The settings. The Genetic Algorithm section sets three numbers. Generations is how many times each population evolves, and Population is the systems per generation. Restarts throws the population away and starts a fresh search. The starter natural gas file uses 25, 200 and 5,000. Advanced view adds Random Seed, Max. Crossover Attempts and Early-Stop Reset-and-Restart. Each is explained on the Interface Reference.

    Fitness and filters. GSB ranks systems by Fitness Criteria, by default Net Profit × Average Trade, worked out on the training data only. Net profit alone can reward tiny trades that don't cover costs; average trade alone can reward a few lucky trades. Performance filters are separate: hard pass-or-fail rules on trades, profit factor and Pearson (how straight the equity curve is).

    Finding indicators first. GSB's Statistics tab ranks indicators by how often they appear in the top systems versus the bottom ones. Trademaid's recommended route uses two passes: one build picks about 10 indicators, then a second build uses only those. It keeps the main search tight. See Building Trading Strategies.

    The GA in walk-forward too. GSB also uses a genetic search to re-optimise each system during a walk-forward. The Strategy Guide uses GA Generations 120 and GA Population 120 for a three-indicator system, and treats 100 × 100 as the floor. Details are on Walk-Forward Optimization.

    Controlling the data-mining risk. GSB keeps test data the search never used, groups similar systems into families, and verifies finalists on other bar intervals. Peter runs every settings comparison four times, because 20 identical runs in one of his tests gave very different counts of passing systems. The checks are summarised in the Robustness Lab.

    Honest limits

    GSB runs on Windows and is built around futures and the platforms it exports to: TradeStation, MultiCharts and NinjaTrader 8. It isn't a tool for testing your own indicator ideas. It has a learning curve, which the free GSB Pro training is designed to shorten. The free 14-day trial has a smaller indicator set than the full version.

    So, can genetic algorithms build trading strategies?

    They can build strategies that pass a backtest, quickly and in large numbers. Whether any of those strategies holds up is a separate question, answered only by testing on unseen data.

    Treat a GA as a fast idea generator, not an oracle. The search is the easy part. The real work is the testing and selection that follow, which is why most of the Strategy Guide covers them. All results from any GA are hypothetical backtests.

    Frequently asked questions

    What is a genetic algorithm in trading?

    It is a search method modelled on evolution. It starts with a population of random strategies, scores each by a backtest, breeds the better ones and repeats over many generations.

    Can genetic algorithms build trading strategies?

    Yes, they can build strategies that pass a backtest. Whether those strategies hold up on new data has to be tested separately, using out-of-sample data and walk-forward analysis.

    What is the difference between genetic programming and genetic optimisation?

    Genetic optimisation tunes the parameter values of rules you wrote. Genetic programming evolves the rules themselves. Many strategy generators combine the two by assembling building blocks and tuning their settings.

    Are strategies from a genetic algorithm curve-fit?

    They are prone to it, because the search tries so many combinations that some fit past data by luck. Testing on data the search never used is how you separate those from more robust ones.

    Why do two genetic algorithm runs give different results?

    Each run starts from random candidates, so it follows a different path through the search. That is why settings changes should be compared over several identical runs.

    Do I need to code to use genetic algorithm trading software?

    Not with every tool. GSB builds systems without you writing code, then generates code for TradeStation, MultiCharts or NinjaTrader 8 that you copy across.

    Try it on your own market

    Start a free 14-day GSB trial and learn the method with our free 53-lesson video course. GSB Pro is an authorised Trademaid reseller and earns a commission if you buy.

    Further reading