Kalman Trend And Velocity Overlay
Plenty of "Kalman" indicators published for MT4 are a single exponential smooth with a fixed weight, relabelled. A real Kalman filter tracks a state vector and its own uncertainty about that state, and its effective smoothing weight - the Kalman gain - is recomputed every bar from that uncertainty rather than fixed in advance. This file implements the textbook two-state case: state = [level, velocity], with a linear transition model (level moves forward by the current velocity estimate each bar) and a scalar measurement model (only the close price is observed directly). Each bar runs a genuine predict step - project the state forward, grow the 2x2 covariance matrix by the process noise - followed by a genuine update step - compute the innovation against the new close, compute the Kalman gain from the predicted covariance and the assumed measurement noise, pull the state toward the observation by that gain, and shrink the covariance accordingly. Because the gain comes from the covariance rather than a fixed constant, the filter responds faster right after a shift (when its own uncertainty is high) and settles into a steadier read once the estimate has converged - the actual mechanism a Kalman filter is supposed to provide, not an approximation of one. The level estimate is plotted with a shaded band at plus/minus one standard deviation of its own tracked uncertainty (the square root of the covariance matrix's P11 term), so the width of the band itself is meaningful - a wide band means the filter is currently unsure, a narrow one means it has converged. The velocity state has no natural price-chart scale to plot directly, so its current value is written to a chart comment instead of forced onto an axis it does not belong on. Notes and limitations - The filter is fully recursive from the oldest available bar forward, so its covariance matrix is seeded wide (P11 = P22 = 1.0) on the very first bar it can compute and narrows as it converges - the first several dozen bars after a fresh attach carry a visibly wider band than the steady state, which is the filter converging, not a fault. - The process/measurement noise inputs are not fitted to any specific instrument; they are a starting point a trader tunes by watching how the band width and reaction speed behave on their own chart. There is no universally correct setting - it is a genuine trade-off between responsiveness and smoothness, the same trade-off every filter makes, just made explicit and adjustable here instead of hidden in a fixed averaging period. - The transition model assumes velocity is roughly constant from one bar to the next (a straight-line local model). It has no concept of acceleration or of a cycle, so it will not out-perform a cycle-aware filter like this catalogue's roofing-filter or Voss indicators on a genuinely oscillating instrument - it is a trend/velocity estimator, not a cycle tool. - Because every value is computed once from closed-bar history with no lookahead, it never repaints: every value shown for a closed bar is final.
Indicator · MT4 · v1.0 · Free
About Kalman Trend And Velocity Overlay
Kalman Trend And Velocity Overlay is an indicator for MetaTrader 4. It has 5 settings, such as Process noise level, Process noise velocity and Measurement noise. MQL source code included.
Files in the download: KalmanTrendVelocity.ex4, KalmanTrendVelocity.mq4, KalmanTrendVelocity.txt.
Settings
| Setting | Default | What it does |
|---|---|---|
| Process noise level | 0.01 | Process noise for the level state - higher lets level move faster |
| Process noise velocity | 0.01 | Process noise for the velocity state - higher lets trend speed change faster |
| Measurement noise | 1.0 | Assumed noise in the close price itself - higher trusts the filter's own model more |
| Band multiplier | 1.0 | Uncertainty band width, in standard deviations of the level estimate |
| Show comment | true | Show the current velocity estimate as a chart comment |
How to install
- Download the ZIP from your PolyMaria library and unzip it.
- In MetaTrader 4 open File → Open Data Folder, then go to MQL4 → Indicators.
- Copy the .mq4 file(s) there. Put any .mqh files in MQL4 → Include.
- Open the .mq4 file in MetaEditor and press Compile (F7) if MetaTrader does not show it.
- Drag the indicator from the Navigator onto a chart and adjust the settings.
Tags: Moving Average · Trend · MT4 Indicator · MQL4 Source Code