Loading…

Particle Swarm Optimiser

Particle swarm optimisation moves a population of candidate solutions ("particles") around the search space, each one pulled toward the best spot it has personally found and the best spot the whole swarm has found. It typically closes in on a smooth optimum faster than a genetic algorithm, at the cost of being more prone to premature convergence on a bumpy one. Like GeneticAlgorithm.mqh in this catalogue, this header cannot call a caller-supplied fitness function directly, because MQL4 has no function pointers. It uses the same ask-tell interface instead: PSOInit builds a swarm, the caller reads one particle's position with PSOGetPosition, evaluates it however it wants - a backtest score, a formula, anything - and hands the whole fitness array back to PSOTellFitness once every particle in the swarm has a score. That call updates personal and global bests and moves every particle one step. The loop always lives in the caller's code. Fitness is maximised by convention. Minimising a cost is one negation away. The detail that makes or breaks a from-scratch PSO implementation is velocity clamping. Textbook PSO updates velocity as a sum of an inertia term and two pulls (toward the particle's own best and the swarm's best), with nothing stopping that sum from growing every iteration. Left unclamped, particles routinely pick up enough speed to fly straight past the optimum and out of the useful search space, and the swarm ends up oscillating wider instead of settling down. This header clamps velocity to a configurable fraction of each dimension's search range, and additionally zeroes the velocity component that pushed a particle past a bound, rather than letting it press against the wall every subsequent step. Notes and limitations - Positions are real-valued and bounded. There is no discrete or categorical variant; round inside your own fitness function if a parameter must land on a fixed set of choices. - No convergence detection. The caller decides how many iterations to run - watch PSOBestFitness plateau, as the demo's printed trace shows. - inertiaWeight, cognitiveWeight and socialWeight interact strongly. The defaults (0.7 / 1.5 / 1.5) are the commonly cited starting point in the PSO literature, not a guarantee for every objective shape. - maxVelocityFraction trades exploration speed for stability. A very small value can make the swarm converge slowly on a wide search space; a very large one reintroduces the overshoot problem this header exists to avoid. - Randomness comes from MathRand() through MathSrand(); a nonzero seed gives a fully repeatable run, which the demo relies on. - Pure computation, no chart or trade calls - safe to call from a script, indicator or expert with no side effects.

Script · MT4 · v1.0 · Free

About Particle Swarm Optimiser

Particle Swarm Optimiser is a script for MetaTrader 4. It has 3 settings, such as Swarm size, Iterations and Random seed. MQL source code included.

Files in the download: ParticleSwarm.mqh, ParticleSwarm.txt, ParticleSwarmDemo.mq4.

Settings

SettingDefaultWhat it does
Swarm size30particles in the swarm
Iterations60ask-tell rounds 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