Nelder-Mead Simplex Optimizer
Calibrating a handful of continuous parameters - an indicator period expressed as a real number, a threshold, a weighting factor - normally means either a brute-force grid search or reaching for the strategy tester's optimiser, which is slow, external to the running terminal, and cannot be called from inside an OnInit or a scheduled recalibration. The Nelder-Mead downhill simplex method solves this without derivatives, without a grid, and in a few dozen function evaluations for a well-behaved cost surface. The obstacle in MQL4 is that every textbook description of Nelder-Mead assumes you can hand the optimiser a callback: minimise(f, x0). MQL4 has no function pointers, so that signature cannot be written. This header uses the standard workaround instead: an ask-tell loop. GetNextPoint() returns the parameter vector that needs evaluating, your own code computes whatever cost that point implies - reading indicator buffers, summing squared error against recent bars, anything - and SubmitValue() feeds the number back in. The optimiser class never sees or calls your objective, which is also exactly what keeps this submission self-contained. The MQL5 port keeps the same ask-tell shape even though MQL5 does have a function-pointer mechanism of its own, because ask-tell is what lets an objective built from indicator handles, trade history or anything else stay entirely on the caller's side of the fence. Internally it runs the full standard move set: reflect the worst vertex through the centroid of the rest, expand further if that reflection beat the current best, contract if it did not beat even the second-worst, and shrink the entire simplex toward the best vertex when even contraction fails to improve anything. A zero or negative initial step is replaced with a small safe default, because a degenerate simplex - every vertex coincident - can never move regardless of how many iterations it is given, and that failure mode is silent unless it is guarded against explicitly. Convergence is judged by the spread between the best and worst simplex value, not by a fixed evaluation count alone, so a well-conditioned problem finishes early and a difficult one still stops on the iteration budget rather than looping indefinitely. Notes and limitations - This is a local optimiser. On a cost surface with several local minima it converges to whichever one the starting simplex happens to fall into, not necessarily the global minimum. Run it from several starting points and keep the best result if the surface is suspected to be multi-modal. - The objective function is entirely the caller's responsibility. Nothing here validates that it is well-defined, continuous, or even deterministic - an objective that changes value between calls at the same point will confuse the simplex geometry. - Coefficients (reflection, expansion, contraction, shrink) use the standard textbook values and are not exposed as inputs. Tuning them rarely helps a genuine convergence problem and just gives a miscalibrated caller another way to break it. - No thread or state concerns beyond the instance itself: create one CNelderMead per concurrent optimisation and do not share an instance across unrelated calibrations.
Script · MT4 · v1.0 · Free
About Nelder-Mead Simplex Optimizer
Nelder-Mead Simplex Optimizer is a script for MetaTrader 4. It has 1 setting, such as Max iterations. MQL source code included.
Files in the download: NelderMeadOptimizer.mqh, NelderMeadOptimizer.txt, NelderMeadOptimizerDemo.mq4.
Settings
| Setting | Default | What it does |
|---|---|---|
| Max iterations | 200 | Iteration budget for every example 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: Grid · MT4 Script · MQL4 Source Code