OPEN-SOURCE SCRIPT
Kalman Ema Crosses - [JTCAPITAL]

Kalman EMA Crosses - [JTCAPITAL] is a modified way to use Kalman Filters applied on Exponential Moving Averages (EMA Crosses) for Trend-Following.
The Kalman filter is a recursive smoothing algorithm that reduces noise from raw price or indicator data, and in this script it is applied both directly to price and on top of EMA calculations. The goal is to create cleaner, more reliable crossover signals between two EMAs that are less prone to false triggers caused by volatility or market noise.
The indicator works by calculating in the following steps:
Buy and Sell Conditions:
-Buy Condition: When the Kalman-smoothed EMA 1 crosses above the Kalman-smoothed EMA 2, a bullish crossover is confirmed.
-Sell Condition: When EMA 1 crosses below EMA 2, a bearish crossover is confirmed.
Users may enhance the robustness of these signals by adjusting process noise, measurement noise, or EMA lengths. Lower measurement noise values make the filter react faster (but potentially noisier), while higher values make it smoother (but slower).
Features and Parameters:
-Source: Selectable price input (Close, Open, High, Low, etc.).
-EMA 1 Length: Defines the fast EMA period.
-EMA 2 Length: Defines the slow EMA period.
-Process Noise: Controls how much randomness the Kalman filter assumes in price dynamics.
-Measurement Noise: Controls how much uncertainty is assumed in raw input data.
-Kalman Usage: Option to apply Kalman filtering either before EMA calculation (on price) or after (on EMA values).
Specifications:
Kalman Filter
The Kalman filter is an optimal recursive algorithm that estimates the state of a system from noisy measurements. In trading, it is used to smooth prices or indicator values. By balancing process noise (expected volatility) with measurement noise (data uncertainty), it generates a smoothed signal that reacts adaptively to market conditions.
Exponential Moving Average (EMA)
An EMA is a weighted moving average that emphasizes recent data more heavily than older data. This makes it more responsive than a simple moving average (SMA). EMAs are widely used to identify trends and momentum shifts.
EMA Crossovers
The crossing of a fast EMA above a slow EMA suggests bullish momentum, while the opposite suggests bearish momentum. This is a cornerstone technique in trend-following systems.
Dual Kalman Filtering
Applying Kalman both to raw price and to the EMAs themselves reduces whipsaws further. It creates crossover signals that are not only smoothed but also validated across two levels of noise reduction. This significantly enhances signal reliability compared to traditional EMA crossovers.
Process Noise
Represents the filter’s assumption about how much the underlying market can randomly change between steps. Higher values make the filter adapt faster to sudden changes, while lower values make it more stable.
Measurement Noise
Represents uncertainty in price data. A higher measurement noise value means the filter trusts the model more than the observed data, leading to smoother results. A lower value makes the filter more reactive to observed price fluctuations.
Trend Coloring & Fill
The use of dynamic colors and filled regions provides immediate visual recognition of trend states, helping traders act faster and with greater clarity.
Enjoy!
The Kalman filter is a recursive smoothing algorithm that reduces noise from raw price or indicator data, and in this script it is applied both directly to price and on top of EMA calculations. The goal is to create cleaner, more reliable crossover signals between two EMAs that are less prone to false triggers caused by volatility or market noise.
The indicator works by calculating in the following steps:
- Source Selection
The script starts by selecting the price input (default is Close, but can be adjusted). This chosen source is the foundation for all further smoothing and EMA calculations. - Kalman Filtering on Price
Depending on user settings, the selected source is passed through one of two independent Kalman filters. The filter takes into account process noise (representing expected market randomness) and measurement noise (representing uncertainty in the price data). The Kalman filter outputs a smoothed version of price that minimizes noise and preserves underlying trend structure. - EMA Calculation
Two exponential moving averages (EMA 1 and EMA 2) are then computed on the Kalman-smoothed price. The lengths of these EMAs are fully customizable (default 15 and 25). - Kalman Filtering on EMA Values
Instead of directly using raw EMA curves, the script applies a second layer of Kalman filtering to the EMA values themselves. This step significantly reduces whipsaw behavior, creating smoother crossovers that emphasize real momentum shifts rather than temporary volatility spikes. - Trend Detection via EMA Crossovers
-A bullish trend is detected when EMA 1 (fast) crosses above EMA 2 (slow).
-A bearish trend is detected when EMA 1 crosses below EMA 2.
The detected trend state is stored and used to dynamically color the plots. - Visual Representation
Both EMAs are plotted on the chart. Their colors shift to blue during bullish phases and purple during bearish phases. The area between the two EMAs is filled with a shaded region to clearly highlight trending conditions.
Buy and Sell Conditions:
-Buy Condition: When the Kalman-smoothed EMA 1 crosses above the Kalman-smoothed EMA 2, a bullish crossover is confirmed.
-Sell Condition: When EMA 1 crosses below EMA 2, a bearish crossover is confirmed.
Users may enhance the robustness of these signals by adjusting process noise, measurement noise, or EMA lengths. Lower measurement noise values make the filter react faster (but potentially noisier), while higher values make it smoother (but slower).
Features and Parameters:
-Source: Selectable price input (Close, Open, High, Low, etc.).
-EMA 1 Length: Defines the fast EMA period.
-EMA 2 Length: Defines the slow EMA period.
-Process Noise: Controls how much randomness the Kalman filter assumes in price dynamics.
-Measurement Noise: Controls how much uncertainty is assumed in raw input data.
-Kalman Usage: Option to apply Kalman filtering either before EMA calculation (on price) or after (on EMA values).
Specifications:
Kalman Filter
The Kalman filter is an optimal recursive algorithm that estimates the state of a system from noisy measurements. In trading, it is used to smooth prices or indicator values. By balancing process noise (expected volatility) with measurement noise (data uncertainty), it generates a smoothed signal that reacts adaptively to market conditions.
Exponential Moving Average (EMA)
An EMA is a weighted moving average that emphasizes recent data more heavily than older data. This makes it more responsive than a simple moving average (SMA). EMAs are widely used to identify trends and momentum shifts.
EMA Crossovers
The crossing of a fast EMA above a slow EMA suggests bullish momentum, while the opposite suggests bearish momentum. This is a cornerstone technique in trend-following systems.
Dual Kalman Filtering
Applying Kalman both to raw price and to the EMAs themselves reduces whipsaws further. It creates crossover signals that are not only smoothed but also validated across two levels of noise reduction. This significantly enhances signal reliability compared to traditional EMA crossovers.
Process Noise
Represents the filter’s assumption about how much the underlying market can randomly change between steps. Higher values make the filter adapt faster to sudden changes, while lower values make it more stable.
Measurement Noise
Represents uncertainty in price data. A higher measurement noise value means the filter trusts the model more than the observed data, leading to smoother results. A lower value makes the filter more reactive to observed price fluctuations.
Trend Coloring & Fill
The use of dynamic colors and filled regions provides immediate visual recognition of trend states, helping traders act faster and with greater clarity.
Enjoy!
Script open-source
Dans l'esprit de TradingView, le créateur de ce script l'a rendu open-source, afin que les traders puissent examiner et vérifier sa fonctionnalité. Bravo à l'auteur! Vous pouvez l'utiliser gratuitement, mais n'oubliez pas que la republication du code est soumise à nos Règles.
Clause de non-responsabilité
Les informations et les publications ne sont pas destinées à être, et ne constituent pas, des conseils ou des recommandations en matière de finance, d'investissement, de trading ou d'autres types de conseils fournis ou approuvés par TradingView. Pour en savoir plus, consultez les Conditions d'utilisation.
Script open-source
Dans l'esprit de TradingView, le créateur de ce script l'a rendu open-source, afin que les traders puissent examiner et vérifier sa fonctionnalité. Bravo à l'auteur! Vous pouvez l'utiliser gratuitement, mais n'oubliez pas que la republication du code est soumise à nos Règles.
Clause de non-responsabilité
Les informations et les publications ne sont pas destinées à être, et ne constituent pas, des conseils ou des recommandations en matière de finance, d'investissement, de trading ou d'autres types de conseils fournis ou approuvés par TradingView. Pour en savoir plus, consultez les Conditions d'utilisation.