Kalman Filter For Price Smoothing
A moving average weights every bar in its window equally and only stops reacting to an old bar once it falls off the back of the window - the lag is built into the definition. A Kalman filter works differently: it keeps a running belief about where the price actually is, and updates that belief in proportion to how much the new tick is trusted against the filter's own prediction. There is no window and no period. The trade-off between smoothness and responsiveness is controlled entirely by the ratio between two noise inputs - how much the underlying level is believed to drift between ticks (process noise) against how much any single tick is distrusted (measurement noise). Two independent trackers are included. The 1D filter assumes a random-walk model - the level moves, but there is no separate trend to track - and is the cheap default for plain smoothing. The 2D filter adds a second state variable for velocity (trend per bar), estimated purely from how the level's recent surprises correlate with each other. Because it carries a trend estimate explicitly, the 2D filter's velocity output flips sign close to a genuine reversal, which the demo shows directly rather than asking you to eyeball it in the smoothed level. Both trackers come in two shapes. KalmanUpdate1D and KalmanUpdate2D are stateful step functions - the way a live EA should actually call this header, once per tick or once per new bar, holding one persistent state struct for the life of the chart. KalmanFilterSeries and KalmanFilterVelocitySeries are batch wrappers that run the same recursion over a whole array at once, for backtesting or for building a smoothed indicator line. The batch functions exist for convenience; they do not replace the step functions inside a live loop, because rebuilding the filter from scratch every tick throws away exactly the accumulated belief that makes a Kalman filter worth using over a moving average. Notes and limitations - This is a linear Kalman filter with a fixed model (random walk, or constant velocity). It does not adapt the model itself, and it is not the extended or unscented variant needed for a genuinely nonlinear process - for price series that assumption is standard practice, not a shortcut. - The noise inputs are not automatically calibrated from the data. Started from bad values, both filters converge to sensible tracking within a few dozen bars, but the very first outputs on a short series can be poor - the demo seeds the state from the first price, which is the standard fix. - The 2D filter's covariance can, in principle, grow unbounded if processNoiseVelocity is set far larger than the actual variability of the trend - watch state.p11 in an unfamiliar configuration rather than trusting it blindly. - Neither filter repaints in the indicator sense - each output depends only on the current and all previous inputs, never a future one - but a Kalman filter is still fundamentally reactive, not predictive; it estimates where the price is now; it does not forecast where it goes next.
Script · MT5 · v1.0 · Free
About Kalman Filter For Price Smoothing
Kalman Filter For Price Smoothing is a script for MetaTrader 5. It has 3 settings, such as Process noise, Measurement noise and Bar count. MQL source code included.
Files in the download: KalmanFilter.mq5.mqh, KalmanFilter.txt, KalmanFilterDemo.ex5, KalmanFilterDemo.mq5.
Settings
| Setting | Default | What it does |
|---|---|---|
| Process noise | 0.01 | 1D filter: how fast the level is allowed to drift |
| Measurement noise | 0.5 | 1D filter: how much each price tick is distrusted |
| Bar count | 40 | How many synthetic bars to generate |
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: Moving Average · Trend · MT5 Script · MQL5 Source Code