The Smoothed RSX - Kalman is a two-stage momentum oscillator.
It takes a high-quality, noise-reduced momentum signal (the RSX) and applies a predictive tracking algorithm (a single-state Kalman filter) to mathematically anticipate where that momentum is heading next.
Think of the RSX as a car driving on a winding road, and the Kalman filter as the GPS trying to predict the car's exact path based on its current speed and direction.
Stage 1: The RSX Engine (The Signal)
Standard RSI is notoriously "noisy" and jagged. RSX, originally developed by Mark Jurik, solves this by applying a double-smoothed algorithm:
A true Kalman filter uses complex matrix algebra to track an object's state (position and velocity) while accounting for sensor noise. Because Thinkorswim evaluates code strictly top-down and doesn't support matrix math, this uses a streamlined, 1-dimensional recursive equation.
The formula continuously balances three elements:
Kalman_t = Kalman_{t-1} + [Velocity + (Stiffness\times Error)] \times Gain
How to Tune the Output
It takes a high-quality, noise-reduced momentum signal (the RSX) and applies a predictive tracking algorithm (a single-state Kalman filter) to mathematically anticipate where that momentum is heading next.
Think of the RSX as a car driving on a winding road, and the Kalman filter as the GPS trying to predict the car's exact path based on its current speed and direction.
Stage 1: The RSX Engine (The Signal)
Standard RSI is notoriously "noisy" and jagged. RSX, originally developed by Mark Jurik, solves this by applying a double-smoothed algorithm:
- Momentum Extraction: It calculates the raw bar-to-bar price change (mom) and the absolute value of that change (moa).
- Double Smoothing: Instead of a simple moving average, it runs those momentum values through two cascaded exponential filters (f1/f2 and a1/a2).
- The Result: A perfectly bounded 0–100 oscillator that eliminates the high-frequency jitter of standard RSI while introducing almost zero lag. This acts as the "clean" baseline for our filter.
A true Kalman filter uses complex matrix algebra to track an object's state (position and velocity) while accounting for sensor noise. Because Thinkorswim evaluates code strictly top-down and doesn't support matrix math, this uses a streamlined, 1-dimensional recursive equation.
The formula continuously balances three elements:
- The Current Position (Kalman_{t-1}): Where was the smoothed line on the previous bar?
- The Velocity: How fast, and in what direction, is the Kalman line already moving? This is calculated by looking at the difference between the last two points: (Kalman_{t-1} - Kalman_{t-2}).
- The Innovation/Error: How far away is the raw RSX from the current Kalman prediction? This difference is multiplied by the stiffness to act as a magnetic pull, ensuring the Kalman line doesn't drift off into space.
Kalman_t = Kalman_{t-1} + [Velocity + (Stiffness\times Error)] \times Gain
How to Tune the Output
- The gain Input: This is the filter's sensitivity. At 1.0, it fully respects the velocity, which can cause a"sine wave" overshooting effect. Dropping it to 0.5 dampens the velocity, making the line track the raw RSX more tightly.
- The stiffness: This is the stiffness of the spring connecting the Kalman line to the raw RSX. Increasing it (e.g., to 0.05) forces the Kalman line to snap back to the raw RSX much faster.
Code:
# The Smoothed RSX - Kalman
# Description: RSX with Stable Recursive Kalman Filter
# by whoDAT
# 3/2026
declare lower;
input inpRsiPeriod = 14; # RSX period
input gain = 0.99; # Kalman Gain (Sensitivity)
input stiffness = 0.01; # Kalman Stiffness
input inpPriceType = {default CLOSE, OPEN, HIGH, LOW, HL2, HLC3, OHLC4};
input overboughtLevel = 80;
input underboughtLevel = 20;
def price;
switch (inpPriceType) {
case OPEN:
price = open;
case HIGH:
price = high;
case LOW:
price = low;
case HL2:
price = (high + low) / 2;
case HLC3:
price = (high + low + close) / 3;
case OHLC4:
price = (open + high + low + close) / 4;
default:
price = close;
}
# --- RSX Calculation ---
def Kg = 3.0 / (2.0 + inpRsiPeriod);
def Hg = 1.0 - Kg;
def mom = price - price[1];
def moa = AbsValue(mom);
def f1 = CompoundValue(1, Kg * mom + Hg * f1[1], 0);
def f2 = CompoundValue(1, Kg * f1 + Hg * f2[1], 0);
def momOut = 1.5 * f1 - 0.5 * f2;
def a1 = CompoundValue(1, Kg * moa + Hg * a1[1], 0);
def a2 = CompoundValue(1, Kg * a1 + Hg * a2[1], 0);
def moaOut = 1.5 * a1 - 0.5 * a2;
def rsiValue = if moaOut == 0 then 50 else (momOut / moaOut + 1) * 50;
def rsxRaw = if inpRsiPeriod > BarNumber() then Double.NaN else Min(Max(rsiValue, 0), 100);
# We check for NaNs at every step of the recursion to prevent the plot from disappearing
rec kalman = if IsNaN(rsxRaw) then Double.NaN
else if IsNaN(kalman[1]) then rsxRaw
else kalman[1] + ( ( (if IsNaN(kalman[2]) then 0 else (kalman[1] - kalman[2])) + stiffness * (rsxRaw - kalman[1])) * gain);
# --- Final Plotting ---
plot RSXLine = rsxRaw;
plot SmoothedRSXLine = kalman;
# Logic for direction and coloring
def isUp = kalman > kalman[1];
def isDown = kalman < kalman[1];
def rsxState = if isUp then 1 else if isDown then 2 else rsxState[1];
RSXLine.SetDefaultColor(Color.WHITE);
SmoothedRSXLine.SetLineWeight(2);
SmoothedRSXLine.AssignValueColor(if rsxState == 1 then Color.LIME else if rsxState == 2 then Color.PINK else Color.GRAY);
# Thresholds
plot Overbought = overboughtLevel;
plot Oversold = underboughtLevel;
plot MidLine = 50;
Overbought.SetDefaultColor(Color.LIGHT_GRAY);
Overbought.SetStyle(Curve.SHORT_DASH);
Oversold.SetDefaultColor(Color.LIGHT_GRAY);
Oversold.SetStyle(Curve.SHORT_DASH);
MidLine.SetDefaultColor(Color.LIGHT_GRAY);
MidLine.SetStyle(Curve.SHORT_DASH);
Last edited by a moderator: