From Apollo to TradingView: The Kalman Filter's Origins
The Kalman filter was introduced by Hungarian-American engineer Rudolf Kálmán in 1960 and was immediately adopted by NASA for the Apollo navigation system. Its job: estimate the true position and velocity of the spacecraft based on noisy sensor measurements.
The filter solves a fundamental problem — how do you extract the genuine signal from a stream of measurements that are corrupted by random noise? It does so by maintaining an estimate of the current state and updating that estimate with each new measurement, weighted by a confidence factor (the Kalman gain) that balances trust in the model versus trust in the measurement.
Financial price series face exactly the same problem: the 'true' price trend exists beneath layers of noise generated by market microstructure. The Kalman filter's mathematical framework translates directly to this domain.
How the Kalman Filter Works: The Core Mechanics
The standard Kalman filter maintains two quantities: a state estimate (in trading: the filtered price and its velocity) and an error covariance (a measure of uncertainty in the estimate). At each new price observation, it performs two steps:
- ✦Predict: Project the current state estimate forward one step using the state transition model. In price terms: new estimated price = last filtered price + last estimated velocity.
- ✦Update: Incorporate the new observation (the actual close price) to correct the prediction. The degree of correction is governed by the Kalman gain K = P / (P + R), where P is the prediction uncertainty and R is the measurement noise parameter.
The Q and R Parameters: Tuning the Filter
The two tunable parameters — Q (process noise) and R (measurement noise) — determine the filter's behaviour:
Q controls how much the filter trusts its own model versus new data. A higher Q makes the filter more responsive (less lag, more noise). A lower Q makes it more stable (more lag, less noise).
R controls how much noise is assumed in the measurements. A higher R makes the filter treat observations as noisier and rely more on its own model.
The ratio Q/R is what effectively determines the Kalman gain. For trading applications on 30M Gold (XAUUSD), typical values are Q = 0.001–0.01 and R = 0.1–1.0. Highly volatile instruments like crypto benefit from higher Q values to maintain responsiveness.
An adaptive Kalman filter — one that adjusts Q dynamically based on recent price volatility (ATR) — outperforms fixed-parameter implementations across different market conditions. This is the approach used in Quantum DeCasteljau.
Kalman Filter vs Moving Averages: A Direct Comparison
Both the Kalman filter and moving averages attempt to smooth price data, but they differ fundamentally in how they handle noise and lag:
- ✦Lag: A 20-period SMA introduces ~10 bars of lag. The Kalman filter's lag is adaptive — it reduces lag when the filter is confident in the trend direction, and increases stability when uncertainty is high.
- ✦Noise response: SMAs weight all observations equally; EMAs weight recent observations more. Neither distinguishes between a genuine price move and random noise. The Kalman filter explicitly models noise and downweights observations it classifies as anomalous.
- ✦Velocity tracking: The Kalman filter maintains an internal velocity estimate (rate of price change), which is not available from a moving average. This velocity can be used to predict where the next bar is likely to open — providing a forward-looking component absent from traditional averages.
- ✦Parameter adaptivity: A moving average's behaviour is fixed by its period. An adaptive Kalman filter can tighten or loosen its responsiveness in real time based on recent ATR, making it more robust across volatile and quiet market phases.
Using Kalman Velocity for Signal Generation
Beyond noise reduction, the Kalman filter's velocity estimate provides a directly tradeable signal. Positive velocity indicates the filter's model 'believes' price is moving upward; negative velocity indicates a downward trend in the filtered price series.
Crossings of the velocity through zero — from negative to positive or vice versa — correspond to the Kalman filter's assessment of a trend reversal in the smoothed price. These signals lag real-time reversals but are dramatically less prone to false triggering than raw price crossings.
In Quantum DeCasteljau, the Kalman velocity is fed as one of eight inputs to the ML ensemble, contributing to the overall signal score rather than being used as a standalone trigger. This avoids the risk of acting on a velocity signal that is contradicted by other models.
Implementing Kalman Filter in Pine Script v6
The core Kalman filter update cycle in Pine Script v6 requires two `var` variables to persist state across bars: the filtered price estimate and the prediction error covariance. On each bar, the predict step projects the estimate forward, and the update step incorporates the new close price.
Key implementation notes for Pine Script: avoid recalculating the entire filter history on every bar (use `var` declarations). Ensure the filter only updates on `barstate.isconfirmed` if it feeds into non-repainting signals. The Q and R parameters should be user-configurable inputs to allow tuning for different instruments and timeframes.