Loading…

State Space Trend Models

An exponential moving average and a local level Kalman filter produce superficially similar output (both are a smoothed running level), which is why "state-space" gets applied so often to what is really just EMA with extra vocabulary. The real difference is what an EMA cannot do at all: a Kalman filter carries an explicit variance of its own state estimate forward every step, shrinking it as evidence accumulates and inflating it during quiet periods, and that variance is what a genuine prediction interval is built from - not a fixed multiple of a rolling standard deviation bolted on afterward. An EMA has no notion of "how sure am I right now"; its smoothing constant never changes no matter how much data has been seen. This library's Kalman gain starts near 1 (trust the first observation almost completely) and settles toward its steady-state value as the state covariance converges - visibly different behaviour from an EMA's constant weighting from bar one, and the demo shows this directly. LocalLevelState implements y = level + noise, level_t = level_{t-1} + noise (a random-walk-plus-noise model). LocalTrendState adds a second state, trend, so level_t = level_{t-1} + trend_{t-1} + noise and trend_t = trend_{t-1} + noise - the state-space formulation behind what Holt's linear exponential smoothing only approximates with two fixed smoothing constants and no covariance at all. Both models expose a forecast function that projects the state forward any number of bars with no new observations, returning both the point forecast and its variance, so a caller can build a genuine widening prediction interval with the horizon - something no plain moving average, however weighted, can produce on its own. Notes and limitations - processVar/obsVar are variances the CALLER supplies, not estimated from the data automatically (no maximum-likelihood/EM hyperparameter fitting is included - that is a materially larger undertaking than this "Lite" library's scope). Reasonable starting points: obsVar around the variance of typical bar-to-bar noise on the instrument, processVar a small fraction of that (the demo's values are a worked example, not universal defaults). - The local linear trend model's level and trend innovations are treated as independent (a diagonal process noise matrix) - the standard simplification used in the structural time series literature absent a specific reason to model a correlated innovation. - Prediction intervals assume Gaussian innovations - a reasonable default, but not a guarantee for any specific instrument's actual noise distribution. Treat the interval as an honest reflection of the model's OWN uncertainty, not a guarantee about real market outcomes. - Multi-step forecasts (LlForecastAhead/LtForecastAhead) project the CURRENT filtered state forward assuming no future shocks - by construction they cannot anticipate a future regime change, only quantify growing uncertainty about one.

Script · MT5 · v1.0 · Free

About State Space Trend Models

State Space Trend Models is a script for MetaTrader 5. It has 1 setting, such as Bars. MQL source code included.

Files in the download: StateSpaceTrendModels.mq5.mqh, StateSpaceTrendModels.txt, StateSpaceTrendModelsDemo.ex5, StateSpaceTrendModelsDemo.mq5.

Settings

SettingDefaultWhat it does
Bars300length of each synthetic test series

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: Moving Average · Trend · MT5 Script · MQL5 Source Code

MQL4 & MQL5 scripts · PolyMaria