Loading…

Bayesian Optimisation Lite

Read this before using it: this header is NOT a Gaussian process and does not claim to be. Real Bayesian optimisation fits a Gaussian process posterior over the objective function, which needs inverting a covariance matrix that grows with every observation - genuinely hard to implement correctly and keep numerically stable in plain MQL4 with no linear-algebra library. Rebuilding that honestly was judged out of scope for what this header can responsibly deliver, so a documented simplification is used instead. The simplification: a Nadaraya-Watson kernel-weighted average of nearby observed objective values stands in for the GP posterior MEAN, and a distance-to-nearest-observation proxy (scaled by the observed objective spread) stands in for the GP posterior UNCERTAINTY. Both use the same RBF kernel a real GP would use as its covariance function, so the surrogate still concentrates predicted uncertainty in genuinely under-explored regions - but the numbers are a heuristic, not a calibrated statistical posterior. Ranking candidate parameter values by Expected Improvement over this cheap surrogate still produces the same qualitative behaviour real Bayesian optimisation is used for: early evaluations spread out to explore, later ones concentrate near the best region found so far, converging on a good parameter value in tens of evaluations rather than an exhaustive scan of hundreds or thousands of strategy-tester runs. This is a single-parameter (1D) optimiser. A multi-dimensional version would need the same kernel machinery extended to a distance metric over several parameters at once, which was left out to keep the surrogate's behaviour easy to reason about and verify - tuning one parameter at a time against a fixed baseline for the others is also, in practice, how most traders actually explore an EA's parameter space. Notes and limitations - Not a Gaussian process. The uncertainty figure it reports is a distance- based heuristic, not a calibrated confidence interval - do not use it to make statistical claims, only to guide which region to try next. - Single parameter only. Optimising several parameters together needs either running this once per parameter with the others held fixed, or a genuinely different multi-dimensional implementation this header does not provide. - The surrogate mean can be a poor predictor far from any observed point (it falls back to the plain observed average there) - the algorithm compensates by ALSO ranking those points as highly uncertain, which is what keeps early search spread out rather than stuck near the first points evaluated, but it is a heuristic compensation, not a guarantee. - Best suited to smooth, single-peaked or few-peaked objective landscapes. A genuinely noisy or highly multi-modal objective can mislead the kernel- weighted mean the same way it would mislead a real GP with too coarse a length scale - try a smaller InpLengthScale if the search seems to settle on the wrong region. - This header never calls the strategy tester itself - EvaluateMyStrategy in the usage example above is the caller's own responsibility to wire in.

Script · MT5 · v1.0 · Free

About Bayesian Optimisation Lite

Bayesian Optimisation Lite is a script for MetaTrader 5. It has 8 settings, such as Range min, Range max and Initial points. MQL source code included.

Files in the download: BayesianOptimisationLite.mq5.mqh, BayesianOptimisationLite.txt, BayesianOptimisationLiteDemo.ex5, BayesianOptimisationLiteDemo.mq5.

Settings

SettingDefaultWhat it does
Range min0.0Parameter search range - minimum
Range max100.0Parameter search range - maximum
Initial points4Initial random evaluations before the search loop starts
Iterations12Surrogate-guided evaluations to run
Length scale12.0Kernel length scale - controls how far influence reaches
Explore slack0.5Expected Improvement exploration slack (xi)
Grid steps400Candidate grid resolution per suggestion
Seed6Random seed

How to install

  1. Download the ZIP from your PolyMaria library and unzip it.
  2. In MetaTrader 5 open File → Open Data Folder, then go to MQL5 → Scripts.
  3. Copy the .mq5 file(s) there. Put any .mqh files in MQL5 → Include.
  4. Open the .mq5 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: MT5 Script · MQL5 Source Code

MQL4 & MQL5 scripts · PolyMaria