Differential Evolution Optimizer
A local optimiser such as Nelder-Mead converges to whichever minimum happens to be nearest the starting simplex. That is fine when the cost surface has one basin, and wrong when it does not - an indicator or model calibration with several plausible parameter regimes can easily have more than one. Running a local method from several starting points and keeping the best is one answer; a population-based global method is another, and differential evolution is one of the more reliable ones while still being simple enough to fit in a single header with almost no tuning. Neither MQL4 nor MQL5 offers a function pointer that can be handed a caller's own objective function, so the textbook signature minimise(f, bounds) cannot be written on either platform. Both versions of this header use the same ask-tell workaround - GetNextPoint() returns the vector to evaluate, your own code computes the cost however it wants, SubmitValue() feeds the number back. The algorithm itself is the standard DE/rand/1/bin scheme. Each generation, a trial vector is built for every member of the population by picking three other distinct members, forming a mutant from one plus a scaled difference of the other two, and binomially crossing that mutant with the current member. The trial replaces its target only if it scores at least as well, which gives DE monotonic improvement across generations without any separate elitism bookkeeping. Mutation factor and crossover rate use widely-tested defaults (0.8 and 0.9) rather than being exposed as inputs a caller would likely leave wrong. Unlike Nelder-Mead, which only needs a starting point, differential evolution needs a bounding box - a lower and upper limit per dimension - because the initial population is seeded uniformly inside it. A population smaller than four is raised to four automatically, because the mutation step needs three distinct donor members plus the target and cannot form a valid trial with fewer. A zero-width bound on any dimension is widened automatically, because seeding every population member at the same coordinate on that axis would make it permanently unsearchable. The algorithm is entirely pure arithmetic over plain double arrays - no positions, no orders, no account state - so the MQL5 port carries no trading- model differences at all; it is the same DE/rand/1/bin scheme running on MathRand/MathMax/MathMin, which behave identically on both platforms. Notes and limitations - This remains a heuristic global search, not a guaranteed global optimum. On a sufficiently deceptive surface it can still miss the true minimum, though it explores far more broadly than a single local run. - Needs meaningfully more evaluations than a local optimiser to make its population-based search worthwhile - do not reach for this over a local Nelder-Mead style optimiser for a smooth, single-basin problem. - The objective function is entirely the caller's responsibility. Nothing here validates that it is well-defined, continuous, or deterministic. - Uses the global MathRand() generator through MathSrand seeding done by the caller (the demo seeds it once from TimeLocal). The class does not reseed it itself, so results are reproducible only if the caller controls the seed. MathRand/MathSrand behave identically on both platforms. - No thread or state concerns beyond the instance itself: create one CDifferentialEvolution per concurrent optimisation.
Script · MT4 · v1.0 · Free
About Differential Evolution Optimizer
Differential Evolution Optimizer is a script for MetaTrader 4. It has 2 settings, such as Population size and Max generations. MQL source code included.
Files in the download: DifferentialEvolution.mqh, DifferentialEvolution.txt, DifferentialEvolutionDemo.mq4.
Settings
| Setting | Default | What it does |
|---|---|---|
| Population size | 30 | Population size for the examples below |
| Max generations | 100 | Generation budget for the examples below |
How to install
- Download the ZIP from your PolyMaria library and unzip it.
- In MetaTrader 4 open File → Open Data Folder, then go to MQL4 → Scripts.
- Copy the .mq4 file(s) there. Put any .mqh files in MQL4 → Include.
- Open the .mq4 file in MetaEditor and press Compile (F7) if MetaTrader does not show it.
- Drag the script onto a chart to run it once.
Tags: MT4 Script · MQL4 Source Code