Genetic Algorithm Helper
Every genetic algorithm library you have ever used in a language with function pointers works the same way: you hand it a fitness function, it calls that function itself, over and over, until it converges. A plain, non-OOP MQL header has no portable way to accept "here is my function, call it for me" either, so that interface is not the right shape here. This library uses the ask-tell pattern instead, the same shape used by production black-box optimisers when the objective is expensive or lives outside the optimiser's own process. GAInit builds a population of candidate solutions. The caller reads one individual's genes with GAGetGenes, scores it however it wants - run a strategy tester pass, evaluate a formula, query anything - and once every individual in the generation has a score, hands the whole fitness array to GATellFitness in one call. That single call performs one generation's worth of selection, crossover and mutation and returns control to the caller for the next round. The optimisation loop always lives in your code; this header only ever holds and evolves the population. Fitness is maximised by convention. Minimising a cost is one negation away. Selection is tournament-based: three individuals are drawn at random and the fittest of the three is chosen as a parent. This is simpler than roulette- wheel selection and, unlike roulette wheel, is not distorted by a single very negative fitness value skewing every selection probability - a real concern when early generations of a random population can produce wildly bad scores. Mutation replaces a gene outright with a fresh random value inside its bounds, rather than nudging it by a small step. A full-reset mutation needs no step- size parameter tuned per problem and is much less likely to leave the population stuck oscillating near a mediocre local optimum. The cost is slower fine convergence right at the peak, which a low mutation rate and elitism - the best individuals surviving into the next generation unchanged - both compensate for. Notes and limitations - Genes are real-valued and bounded. There is no built-in support for integer- only or categorical genes; round or discretise inside your own fitness function if a parameter must land on a fixed set of choices. - No convergence detection. The caller decides how many generations to run - watch GABestFitness plateau, as the demo's printed trace shows, and stop when it does. - Population size and elite fraction interact: eliteCount is rounded up to at least 1, so a tiny population with a high elite fraction can leave very few slots for fresh crossover and mutation each generation. - Randomness comes from MathRand() through MathSrand(); a nonzero seed gives a fully repeatable run, which the demo relies on. This is identical on both platforms. - Pure computation, no chart or trade calls - safe to call from a script, indicator or expert with no side effects, on either platform.
Script · MT5 · v1.0 · Free
About Genetic Algorithm Helper
Genetic Algorithm Helper is a script for MetaTrader 5. It has 3 settings, such as Population size, Generations and Random seed. MQL source code included.
Files in the download: GeneticAlgorithm.mq5.mqh, GeneticAlgorithm.txt, GeneticAlgorithmDemo.ex5, GeneticAlgorithmDemo.mq5.
Settings
| Setting | Default | What it does |
|---|---|---|
| Population size | 40 | individuals per generation |
| Generations | 80 | generations to run |
| Random seed | 12345 | fixed seed so the demo output is repeatable |
How to install
- Download the ZIP from your PolyMaria library and unzip it.
- In MetaTrader 5 open File → Open Data Folder, then go to MQL5 → Scripts.
- Copy the .mq5 file(s) there. Put any .mqh files in MQL5 → Include.
- Open the .mq5 file in MetaEditor and press Compile (F7) if MetaTrader does not show it.
- Drag the script onto a chart to run it once.
Tags: MT5 Script · MQL5 Source Code