Back to Guides
Guide

What Is a Kalman Filter in Trading? It Is Usually an EMA

What a Kalman filter is in trading, in four lines of arithmetic, and why the one-state form that trading pages describe is exactly an EMA.

A Kalman filter is a rule for updating one running estimate of a hidden quantity, where each new price moves the estimate by a fraction of its own surprise, and that fraction is computed from how uncertain the estimate was. In trading the hidden quantity is usually the "true" price with the noise taken out, or the hedge ratio between two instruments.

In the simplest and most commonly published form, the one with a single state, the uncertainty settles to a constant after a warm-up, the gain stops moving, and from that point the filter is exactly an exponential moving average. Every pair of noise settings you can pick is an EMA period in disguise. Give the filter a second state and that stops being true, and that is the one place it does something an average cannot.

What is a Kalman filter in trading?

A recursive estimator: it keeps one number it believes about the market, plus a measure of how unsure it is, and updates both on every bar.

The two steps have ordinary names. Predict carries the estimate forward and lets the uncertainty grow. Update pulls the estimate toward the new price, by an amount set by the current uncertainty. The Kalman gain is what sets that amount.

Two parameters set its behaviour. Q is the process noise, how much the filter expects the real value to move on its own. R is the measurement noise, how much it distrusts the price it just saw. Raise Q and the filter chases price harder. Raise R and it trusts its own history more.

How does a Kalman filter work, in four lines?

This is the one-state random-walk form, written with the gain formula quantneuraledge publishes:

predict:  P  <-  P + Q
gain:     K  =   P / (P + R)
update:   x  <-  x + K * (price - x)
then:     P  <-  (1 - K) * P

x is the filtered price, P the uncertainty. The gain line is quantneuraledge's own: "the Kalman gain K = P / (P + R), where P is the prediction uncertainty and R is the measurement noise parameter".

Look at the update line on its own. A new value equals the old value plus a fraction of the difference between price and the old value. That is the EMA update, written out. The only reason it does not look like an EMA is that K changes from bar to bar at the start.

Everything below about the EMA equivalence holds for the four-line form, the one whose arithmetic the trading pages publish. The version with two states behaves differently.

Which Kalman filter does ChartLabs draw?

The one-state form in those four lines, the one that settles into an EMA. Kalman Filter is one of our built-in indicators: open the chart's Indicators menu, stay on the Standard list and pick it under Moving Averages, where it belongs. It draws on the price pane rather than in a pane of its own.

Ours starts from the first price with the uncertainty set to R, and its settings are Process Noise (Q) 0.01, Measurement Noise (R) 1 and Source, which starts on the close. A Q over R of 0.01 means the line you get before you change anything is an EMA of 20, once the warm-up is over. The two-state version with velocity is not a menu entry, so nothing in that group extrapolates a slope.

Is a Kalman filter just a moving average?

For the one-state form, after a warm-up, yes. It is exactly an EMA, and we can say which one.

Step one: the gain stops moving

The gain converges to the positive root of S squared minus Q times S minus Q times R equals zero, with K then equal to S / (S + R). It gets there quickly, and it gets there whatever the market does: the gain recursion reads no prices at all, so the rows below are iterations of the two lines above rather than bars of any chart. At Q = 0.001 and R = 0.1, starting from an uncertainty of 1.0:

iterationgain
100.125781
1000.095125
3,9990.09512492
closed form0.09512492

The gain matches its closed form to six decimal places by iteration 100, and to eight by the last row.

Step two: with a constant gain, the filter and an EMA are the same series

We ran the filter on 7,092 EUR/USD daily bars from 2004-01-01 to 2026-08-31, and beside it an EMA whose smoothing factor was set to the converged gain. After a 200-bar warm-up the largest difference between the two lines across the whole history was 0.000000 pips at most of the parameter pairs we tried, and 0.071987 pips at the worst of them.

Before the warm-up the two lines differ by up to 93.45 pips. That gap is the whole of the difference: a start-up transient while the gain is still settling, on the way to the same line. The equivalence is a claim about the settled filter, not about its first hundred bars.

Step three: so every Q and R pair is an EMA period

Convert the converged gain with n = 2/K - 1, the standard relation between an EMA's smoothing factor and its period, and the tuning problem turns into a choice of period:

QRQ/Rconverged gainequivalent EMA period
0.0011.00.0010.03112763.3
0.0010.10.010.09512520.0
0.0030.30.010.09512520.0
0.011.00.010.09512520.0
0.010.10.10.2701566.4

Does only the ratio Q/R matter?

Yes, exactly, and quantneuraledge says so: "The ratio Q/R is what effectively determines the Kalman gain".

Our check agrees to eight decimal places. A ratio of 0.01 gives a converged gain of 0.09512492 whether the pair is (0.01, 1.0), (0.1, 10.0) or (1.0, 100.0). That halves the tuning problem, which is worth knowing because the same page lists parameter tuning as the filter's drawback against a moving average.

It also has a consequence that page does not draw. The ranges it offers as typical values for 30-minute gold, Q from 0.001 to 0.01 and R from 0.1 to 1.0, span ratios from 0.001 to 0.1. In EMA periods that is 63.3 down to 6.4: a tenfold range of smoothing inside one recommendation.

Does a Kalman filter really have less lag than a moving average?

At the published numbers, no. It lags very slightly more than the moving average it gets compared with.

On a ramp where price rises 0.1 a bar, lag is how far behind the current price the line sits, in bars:

linelag on the ramp
SMA(20)9.5000 bars
one-state filter, Q=0.01, R=1.0 (= EMA 20.0)9.5125 bars
one-state filter, Q=0.001, R=1.0 (= EMA 63.3)31.1267 bars
one-state filter, Q=0.01, R=0.1 (= EMA 6.4)2.7016 bars

quantneuraledge says "A 20-period SMA introduces ~10 bars of lag", and that is right: for a simple moving average the lag is (n - 1) / 2, which is 9.5. At Q = 0.01 and R = 1.0, a pair inside its own published ranges, the filter sits at 9.5125 bars, a shade further back than that SMA.

The same page also says "the Kalman filter often gives a smoother line with less lag than a fixed-period moving average". It can have less lag, and the route is in the table: at Q = 0.01 and R = 0.1 the equivalent period is 6.4 and the lag is 2.70 bars. Shortening the moving average does the same thing.

TheForexGeek puts the comparison more strongly, that moving averages "often lag behind price changes and can fail to adapt quickly in volatile conditions" and that the filter "reacts more quickly to genuine shifts in market direction". The ramp numbers are what we have to set beside those sentences.

Whether a smoothing line can get its lag to zero at all is the subject of our guide to zero lag moving averages, and the Hull moving average makes a similar low-lag claim through different arithmetic.

Is a Kalman filter better than a moving average in a backtest?

One published test says no, and its own settings show why the comparison was uneven.

An mql5 article compared a Kalman regime filter against a moving average across forex pairs and concluded: "The Kalman filter is not superior to the moving average in this scenario, suggesting that complexity does not always result in better performance". It frames the filter plainly: "one can simply see it as a common regime filter that traders normally use, akin to moving average or linear regression models".

Its code sits on the same page, and it is the four-line form above. Its own comment on the predict step reads "(state does not change)". Its inputs are a measurement variance of 10 and a process variance of 1, and its moving average handle is created with a period of 500 in EMA mode. The article states the pairing in prose as well: "a 500-period exponential moving average and a measurement variance of 10 and process variance of 1 for the Kalman filter".

Run those numbers through the arithmetic above. A process variance of 1 against a measurement variance of 10 is a ratio of 0.1, a converged gain of 0.270156, and an equivalent EMA period of 6.4. The benchmark beside it was an EMA of 500.

So the comparison was between a roughly 6-period average and a 500-period average. The article says both were set to filter a similar number of trades, which is a different thing from matching how much they smooth. We have not re-run the test and its conclusion may well hold. The relationship above would have shown the mismatch before any backtest ran.

Answering a question under the article, its author recommends picking the measurement variance from 1000, 100 or 10 and the process variance from 1, 0.1 or 0.01. Those nine combinations give five distinct lines, because only the ratio matters, and they span equivalent EMA periods from 6.4 to 632.5.

Where does a Kalman filter stop being a moving average?

When you give it a second state: price and velocity. That version is something an average cannot be.

On the same straight ramp, a two-state filter tracks with a lag of 0.000000 bars and recovers the true slope as 0.100000 exactly, at every parameter set we tried. No moving average does either.

So quantneuraledge is right about the advantage that does hold up: the filter "maintains an internal velocity estimate (rate of price change), which is not available from a moving average".

That page sits on both sides of the line, though. Its predict step reads "new estimated price = last filtered price + last estimated velocity", which needs two states, while the gain it publishes is the scalar one-state form. Taken as printed, its arithmetic is the one-state version, which is an EMA. The velocity it also describes requires the second state, and that is where the equivalence ends.

What does the velocity term cost?

The line stops being bounded by its own inputs, which is what every low-lag construction trades away.

Our step test: 300 bars at 100 then 300 at 101, so the highest price anywhere is exactly 101.000000.

filterpeakovershoot, % of step
one-state, Q=0.01, R=1.0101.0000000.00%
one-state, Q=0.001, R=1.0100.999924-0.01% (never reaches it)
two-state, Qp=0.01, Qv=1e-5, R=0.1101.0507145.07%
two-state, Qp=0.001, Qv=1e-6, R=1.0101.15862015.86%
two-state, Qp=0.001, Qv=1e-4, R=1.0101.18781718.78%

The one-state filter never overshoots, because it is an average and all its weights are positive. The two-state filter overshoots because it is extrapolating a slope rather than averaging. In the settings above, zero overshoot comes with the one-state filter's lag, and zero lag comes with the two-state filter's overshoot.

What is a Kalman filter used for in pairs trading?

Estimating the hedge ratio as it changes, instead of fixing it once at the start.

Daytrading.com describes the mechanism: the filter is "applied in the estimation of hedge ratios for pairs trading, where the filter dynamically adjusts the ratio based on new market data", and it is used "for dynamically estimating the β coefficient, also known as the hedge ratio". The Portfolio Optimization book has a section on Kalman filtering for pairs trading that works through the same ground in detail.

That is as far as we go. We have measured nothing on two instruments and have no pairs result of our own.

What are a Kalman filter's assumptions, and when do they break?

A linear system and well-behaved noise.

  • Damanmarkets' glossary entry says "The basic Kalman Filter assumes a linear dynamic system and Gaussian noise", and then writes that out as a state equation and a measurement equation.
  • Daytrading.com says "Kalman Filters operate on the principles of linear dynamical systems".
  • mql5 adds the practical ones: "it assumes linear dynamics, requires careful parameter tuning, may lag in detecting abrupt changes, and is computationally more complex than simpler filters like moving averages".

Pair that last phrase with the step test. At the EMA(20)-equivalent setting, the one-state filter is an EMA(20) facing a gap, so it behaves exactly like one. In that form there is nothing there to handle an abrupt change differently.

How do you test a Kalman filter yourself?

Run the ramp first. It takes a minute and it settles the lag question before you build anything around the filter.

  1. Feed it a straight line rising by a fixed amount each bar. Read the gap between the last price and the line, divide by the step, and that is your lag in bars.
  2. Set your Q and R, then read the gain off K = P/(P+R) after a hundred bars. It will have stopped moving.
  3. Convert it: n = 2/K - 1.
  4. Plot an EMA of that period on the same chart and check the two lines sit on top of each other after the warm-up. If they do, you are tuning an EMA, and you can say so.
  5. Only then step through real bars one at a time, logging the turns the filter made first and the turns that went nowhere.

Exponential Moving Average is in the same menu for step 4. Every moving average can be given its own colour and line style, and one indicator instance can be edited without changing the others, which is how the filter and its equivalent EMA stay apart on one chart.

If you want the two-state form with velocity, you write it in the Pine Script editor, and the Pine Scripts tab has two Examples to start from; a copy reaches the chart once you Save & Apply it. There is no automatic backtest of an indicator, so step 5 stays manual: you step the bars and you keep the log.

Step 5 is manual backtesting, bar by bar with the future hidden. Our beginner's guide to backtesting a strategy covers the loop around it, and forex backtesting covers the pair behaviour and session effects. The bars behind our own figures are sourced from Dukascopy, tick-level where it is available, cleaned to remove anomalies, and written up in clean forex historical data.

Next step

If you want to know whether the filter you are about to tune is just an EMA, step 4 answers it in about five minutes. ChartLabs runs a 7-day free trial with no card, then one plan at $20 a month or $200 a year. Start a free trial of ChartLabs and put the two lines on one chart.

Related Topics

kalman-filterkalman-filter-tradingindicatorsmoving-averagespairs-tradingbacktesting