ScriptSpotlight: Kalman Filters on Tradingview

Posted on the 10 August 2022 by Diana Trang @altcointrading_

Kalman filter is a recursive algorithm that was developed to track a moving target and predict its future value. Its advantage is that it doesn’t ignore noise and statistical errors, instead it uses them as a part of its predictive estimate.

The filter has been widely used in navigation and engineering, but it has been no less useful in the asset management industry.

In any finance-related field, Kalman will usually be applied to a time series of data - and not just price data, it could be any economic performance indicators.

In trading and technical analysis particularly, the Kalman filter is used as a smoothing tool that offsets some of the lag that smoothing indicators usually have.

If you even chart, that should tell you that Kalman filter will do better than a moving average, if your goal is to smooth out price action with as little lag as possible.

Here’s a paper that documents a backtest of four different Kalman filter strategies.

Kalman filters on TradingView

Kalman filters have of course made it to TradingView’s public library. I’ve picked and reviewed the one script that I thought would be best to show off the basics.

If you want to explore the library yourself, do search for both “Kalman” as well as the alternative spelling “Kahlman” - there is a bunch of scripts under each of them.

Kalman Filter by Loxx in the Public Library

The Kalman Filter script by Loxx is just a transcript of MT4’s Kalman Filter done in pine script to make it compatible with TradingView.

On the charts it looks pretty much like a well-smoothed moving average, except it doesn’t lag:

TradingView’s BTCUSD on 9 Aug 2022 with Kalman Filter by Loxx

Other than that, this pine script was updated to change color based on “velocity”, a part of the Kalman formula that you won’t find in moving averages.

In Kalman used in technical analysis, velocity says how fast the price changes from candle to candle, and in which direction.

When the price decreases over time, the velocity component of Kalman will be negative. When that trend reverses, it will turn positive. In theory the change in velocity could point you to price reversals.

I find that on crypto markets, the velocity change lags, and that even on 5M timeframes. I looked at BTCUSD, ETHUSD, SOLUSD and their ratios, in the default settings it always lags.

When I increased the K value, which is used as a multiplier of velocity in this formula, it led to an improvement with lag but as it happens, it also started giving more false signals. The velocity would flip back and forth when market wasn’t really trending.

In forex, where Kalman is used a lot, the velocity lags as well but you end up losing less money through it as the forex markets’ moves are not as sharp as they are in crypto. A long way ahead until we get that sort of conditions!

Either way, even forex traders recommend Kalman filter mainly for confirmation of trends and price dynamics.

As for that, Kalman filter is already an effective trend tracker in crypto markets. For that purpose it works well for both short-term charts as well longer time frames. Below is the weekly BTCUSD chart with Kalman filter in default settings.

You will still the most value if you combine it with a good momentum indicator such as RSI and its transformations.

Kalman Filter by Loxx in the Public Library

There are other combos out there too, of course. For one, you can also use pine script strategies that combine advanced moving averages like Hull MA with Kalman filters to improve your trend tracking. The linked post includes one that is in the TradingView public library.