Loading…

Kalman Smoother With EM Fitting

A live, forward-only Kalman filter can only ever use information up to the current bar - its estimate at bar 100 has no way to benefit from what happened at bar 150. For offline analysis of an already-recorded series that restriction is unnecessary and costs accuracy for nothing. This header adds the two things a bare forward filter does not give: fixed-interval (RTS) smoothing and unsupervised noise-parameter fitting. KalmanForwardFilter runs the standard local-level recursion (state follows a random walk, each observation is that state plus independent noise) and keeps both the filtered (posterior) and predicted (prior) state and variance at every bar - the RTS smoother's backward pass needs both, which is why a plain forward-only implementation usually discards exactly the values this one keeps. KalmanRtsSmoother then walks backward from the last bar to the first, blending each bar's filtered estimate with a correction computed from everything AFTER it, and additionally computes the lag-one smoothed cross-covariance between consecutive states - a quantity that only exists because EM needs it, and that most standalone smoother implementations skip entirely. KalmanEmFit is the actual EM loop: each round runs a full forward filter plus backward smooth under the CURRENT noise variances (the E-step), then KalmanEmMStep re-estimates the process variance Q and measurement variance R directly from the smoothed states via closed-form formulas (the M-step) - no gradient descent, no numerical optimiser, the exact maximum-likelihood update for this model. The reported log-likelihood is guaranteed by the EM algorithm to never decrease from one round to the next, and the demo verifies this holds in practice as well as in theory. This is a different Kalman filter from KalmanTrendVelocity.mq4 elsewhere in this catalogue, which is a live 2-state (level+velocity) forward-only chart indicator with noise inputs the user sets by hand. This header is a single-state, offline, batch-processing library built around smoothing a whole recorded series and fitting its own noise parameters - the two do not overlap and this file includes no code from that one. Notes and limitations - Local-level model only (state = level, a random walk) - there is no trend/velocity term. A series with a strong deterministic trend will be smoothed but the trend itself is treated as slow random-walk drift, not modelled explicitly. - EM for this model is guaranteed to increase (never decrease) the log-likelihood each round, but like any EM procedure it can converge to a LOCAL maximum depending on the starting Q/R - very different starting guesses can occasionally converge to different fitted values on the same data. - Every variance is floored at KALMAN_EM_MIN_VARIANCE (1e-10) so a degenerate fit cannot hand the next round a zero variance and divide by zero; a series that is perfectly flat (zero real variation) will hit this floor rather than genuinely converging. - This is a BATCH algorithm - the whole series must be available before smoothing starts, unlike a live forward-only filter. It is not a drop-in replacement for a tick-by-tick indicator.

Script · MT5 · v1.0 · Free

About Kalman Smoother With EM Fitting

Kalman Smoother With EM Fitting is a script for MetaTrader 5. It has 5 settings, such as Series length, True process var and True measure var. MQL source code included.

Files in the download: KalmanSmootherEm.mq5.mqh, KalmanSmootherEm.txt, KalmanSmootherEmDemo.ex5, KalmanSmootherEmDemo.mq5.

Settings

SettingDefaultWhat it does
Series length250Length of the synthetic test series
True process var0.05True process variance used to generate the series
True measure var1.00True measurement variance used to generate the series
Em iterations10Number of EM rounds to run
Random seed2024Seed so the synthetic series is repeatable

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