Discussing the article: "The Kalman Filter for Price Smoothing: An Exact Implementation in MQL5"

Check out the new article: The Kalman Filter for Price Smoothing: An Exact Implementation in MQL5.

This article implements the scalar Kalman Filter for price smoothing in MQL5 using the standard local-level model and the exact prediction–update equations. It explains how process and measurement noise (Q, R) shape the Kalman gain, builds two independently configured filters into a fast/slow crossover, and provides a complete indicator and Expert Advisor that trade a stability-confirmed signal via iCustom, giving a reproducible baseline for testing and tuning.

Most trend-following tools face a fundamental choice: react quickly to new price action and accept more noise, or smooth aggressively and accept more lag. A moving average makes this trade-off implicitly, through its period length, with no explicit way to reason about how much to trust a new price versus the tool's own prior estimate.

The Kalman Filter, developed by Rudolf Kalman in 1960 and famously used for spacecraft navigation on the Apollo program, approaches this differently. Rather than a fixed weighting scheme, it maintains an explicit estimate of its own uncertainty at every step, and uses that uncertainty to calculate, mathematically, exactly how much weight a new observation deserves. When the filter is confident in its current estimate, it leans on that. When a new observation is more informative than what came before, it leans on that instead. This weighting — the Kalman gain — is not a value entered directly; it is computed deterministically from the noise parameters Q and R that are chosen by hand, and it converges toward a steady-state value set by their ratio. The gain formula itself involves no manual tuning, but the Q and R inputs that drive it are still a user choice.

In practice, applying the Kalman Filter to price data comes with three specific challenges:

  1. Choosing the right underlying model — the Kalman Filter is a general framework, not a single formula, and requires an explicit model of how the state is assumed to evolve between observations before any of the actual filtering math can be applied.
  2. Two noise parameters with no obvious default — the filter's behavior is entirely governed by two variances, process noise and measurement noise, and unlike a moving average's single period input, there is no similarly intuitive starting point for either.
  3. Maintaining recursive state correctly — like the causal filters covered elsewhere in this series, the Kalman Filter's output at any bar depends entirely on its own state from the previous bar, and getting that recursion's initialization subtly wrong produces results that look plausible but are not mathematically correct.

This article works through all three, using the exact, standard Kalman Filter equations found in control theory and statistics references — not a simplified or trading-specific variant. The underlying math is verified independently before implementation, two independently-configured filters are combined into a practical crossover system, and the complete indicator and Expert Advisor are built and explained from the ground up.


Author: Syed Jawad Hussain Naqvi