Loading…

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 · MT4 · v1.0 · Free

About Genetic Algorithm Helper

Genetic Algorithm Helper is a script for MetaTrader 4. It has 3 settings, such as Population size, Generations and Random seed. MQL source code included.

Files in the download: GeneticAlgorithm.mqh, GeneticAlgorithm.txt, GeneticAlgorithmDemo.mq4.

Settings

SettingDefaultWhat it does
Population size40individuals per generation
Generations80generations to run
Random seed12345fixed seed so the demo output is repeatable

How to install

  1. Download the ZIP from your PolyMaria library and unzip it.
  2. In MetaTrader 4 open File → Open Data Folder, then go to MQL4 → Scripts.
  3. Copy the .mq4 file(s) there. Put any .mqh files in MQL4 → Include.
  4. Open the .mq4 file in MetaEditor and press Compile (F7) if MetaTrader does not show it.
  5. Drag the script onto a chart to run it once.

Download the PDF guide

Tags: MT4 Script · MQL4 Source Code

MQL4 & MQL5 scripts · PolyMaria