Particle Filter Lite
A Kalman filter represents its whole belief about a hidden state as a single Gaussian and updates it with closed-form linear algebra - fast and exact, but only correct when the underlying process really is linear with Gaussian noise. A particle filter instead represents that belief as a weighted cloud of discrete state guesses ("particles"), which degrades gracefully under nonlinear dynamics or genuinely multi-modal beliefs (two plausible states at once, not one blurry average of both) at the cost of being an approximation that needs enough particles to be trustworthy. This library implements the standard bootstrap particle filter cycle: PredictParticles moves every particle through a simple process model with independent random process noise; UpdateWeights re-weights each particle by how well it explains a new measurement, using a Gaussian likelihood; NormalizeWeights rescales the weights to sum to one, detecting and recovering from total weight collapse rather than dividing by zero; EffectiveSampleSize reports the standard 1/sum(weight^2) diagnostic for how degenerate the weight distribution has become; ResampleParticles performs systematic resampling - drawing a fresh particle set proportional to weight, using one shared random offset rather than many independent draws, the standard low-variance resampling scheme - when the effective sample size has dropped too low. SIMPLIFICATION NOTE, read before use. This is deliberately "lite" in two specific ways, both documented in the header itself. First, the process model is a caller-supplied simple additive step (state plus drift plus Gaussian noise) rather than an arbitrary nonlinear callback function - MQL4 has no clean first-class function-pointer mechanism for passing a general model in. Second, only systematic resampling is implemented, not the several alternative schemes (residual, stratified) found in fuller particle-filter libraries. Both are genuine simplifications of the full technique, not omissions hidden from the reader - the core sequential Monte Carlo mechanism (weighted particle set, Gaussian re-weighting, effective-sample-size diagnostic, systematic resampling) is real and working, demonstrated in the accompanying script against a deliberately nonlinear (sine-path) hidden state that a linear model would mis-track. Notes and limitations - Scalar state only. Tracking a vector state (e.g. level and slope together) would need each particle to carry an array rather than one double, which this "lite" header does not implement. - The process model is additive drift plus Gaussian noise, not an arbitrary nonlinear function - see the simplification note above. - Systematic resampling only; no residual or stratified resampling option. - Uses MathRand() via a local Box-Muller transform as its uniform source - adequate for filtering demonstrations, not cryptographic quality. - No live account, order, or symbol-info call anywhere in this library - pure state-estimation arithmetic, no trading risk of its own.
Script · MT5 · v1.0 · Free
About Particle Filter Lite
Particle Filter Lite is a script for MetaTrader 5. It has 5 settings, such as Particle count, Steps and Process noise std. MQL source code included.
Files in the download: ParticleFilterLite.mq5.mqh, ParticleFilterLite.txt, ParticleFilterLiteDemo.ex5, ParticleFilterLiteDemo.mq5.
Settings
| Setting | Default | What it does |
|---|---|---|
| Particle count | 200 | Number of particles in the cloud |
| Steps | 30 | Number of time steps to simulate |
| Process noise std | 0.05 | Process noise std-dev fed to the filter |
| Measurement noise std | 0.15 | Measurement noise std-dev fed to the filter |
| Random seed | 123 | Seed for repeatable output |
How to install
- Download the ZIP from your PolyMaria library and unzip it.
- In MetaTrader 5 open File → Open Data Folder, then go to MQL5 → Scripts.
- Copy the .mq5 file(s) there. Put any .mqh files in MQL5 → Include.
- Open the .mq5 file in MetaEditor and press Compile (F7) if MetaTrader does not show it.
- Drag the script onto a chart to run it once.
Tags: MT5 Script · MQL5 Source Code