Smoothed RSX Kalman For ThinkOrSwim

whoDAT

Active member
Plus
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.
Screenshot 2026-03-26 110309.png


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:
  1. Momentum Extraction: It calculates the raw bar-to-bar price change (mom) and the absolute value of that change (moa).
  2. Double Smoothing: Instead of a simple moving average, it runs those momentum values through two cascaded exponential filters (f1/f2 and a1/a2).
  3. 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.
Stage 2: The Kalman Filter (The Predictor)
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:
  1. The Current Position (Kalman_{t-1}): Where was the smoothed line on the previous bar?
  2. 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}).
  3. 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.
The Simplified Equation:
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:

Join useThinkScript to post your question to a community of 21,000+ developers and traders.

Similar threads

Not the exact question you're looking for?

Start a new thread and receive assistance from our community.

87k+ Posts
1043 Online
Create Post

Similar threads

Similar threads

The Market Trading Game Changer

Join 2,500+ subscribers inside the useThinkScript VIP Membership Club
  • Exclusive indicators
  • Proven strategies & setups
  • Private Discord community
  • ‘Buy The Dip’ signal alerts
  • Exclusive members-only content
  • Add-ons and resources
  • 1 full year of unlimited support

Frequently Asked Questions

What is useThinkScript?

useThinkScript is the #1 community of stock market investors using indicators and other tools to power their trading strategies. Traders of all skill levels use our forums to learn about scripting and indicators, help each other, and discover new ways to gain an edge in the markets.

How do I get started?

We get it. Our forum can be intimidating, if not overwhelming. With thousands of topics, tens of thousands of posts, our community has created an incredibly deep knowledge base for stock traders. No one can ever exhaust every resource provided on our site.

If you are new, or just looking for guidance, here are some helpful links to get you started.

What are the benefits of VIP Membership?
VIP members get exclusive access to these proven and tested premium indicators: Buy the Dip, Advanced Market Moves 2.0, Take Profit, and Volatility Trading Range. In addition, VIP members get access to over 50 VIP-only custom indicators, add-ons, and strategies, private VIP-only forums, private Discord channel to discuss trades and strategies in real-time, customer support, trade alerts, and much more. Learn all about VIP membership here.
How can I access the premium indicators?
To access the premium indicators, which are plug and play ready, sign up for VIP membership here.
Back
Top