Double Pass - Auto Adjusting Kalman Filter

whoDAT

Member
Plus
The auto-adjusting Kalman filter dynamically updates its estimate for noise. A single pass of the Kalman filter provides good information on the direction and magnitude of the signal in noisy data. Processing any filtered data again through the same filter produces even smoother data (think how double exponential moving averages are compared to the exponential moving average.)

This study allows you to process your chart through the Kalman filter once and/or process that result again producing double Kalman filtered data.

Enjoy!

Code:
##  Double Pass Auto-Adjusting Kalman Filter
##  by whoDAT
# # 1/2026

input price = close;
# The first pass inputs
input length1 = 50; 
input smoothness1 = 2.0;
#the second pass inputs  
input length2 = 50;
input smoothness2 = 2.0;

# FIRST PASS. Calculate Dynamic Volatility
def noise1 = (stdev(price, length1) * smoothness1) + 0.0001;

# FIRST PASS. Kalman Recursive Logic
rec errorEst1;
rec estimate1;

# FIRST PASS. The Kalman Gain: K = (Error in Estimate) / (Error in Estimate + Error in Measurement)
def K1 = if (errorEst1[1] + noise1) != 0
        then errorEst1[1] / (errorEst1[1] + noise1)
        else 0.5; # Default to 0.5 if math fails to prevent flat lines

# FIRST PASS. Update State
estimate1 = CompoundValue(1, estimate1[1] + K1 * (price - estimate1[1]), price);

# FIRST PASS. Update the error estimate for the next calculation
errorEst1 = CompoundValue(1, (1 - K1) * errorEst1[1] + (AbsValue(estimate1 - estimate1[1])), noise1);

# FIRST PASS. Plots
plot Kalman1 = estimate1;
Kalman1.SetDefaultColor(Color.MAGENTA);
Kalman1.SetLineWeight(2);

#######################################

# SECOND PASS. Calculate Dynamic Volatility
def noise2 = (stdev(Kalman1, length2) * smoothness2) + 0.0001;

# SECOND PASS. Kalman Recursive Logic
rec errorEst2;
rec estimate2;

# SECOND PASS. The Kalman Gain: K = (Error in Estimate) / (Error in Estimate + Error in Measurement)
def K2 = if (errorEst2[1] + noise2) != 0
        then errorEst2[1] / (errorEst2[1] + noise2)
        else 0.5; # Default to 0.5 if math fails to prevent flat lines

# SECOND PASS. Update State
estimate2 = CompoundValue(1, estimate2[1] + K2 * (Kalman1 - estimate2[1]), Kalman1);

# SECOND PASS. Update the error estimate for the next calculation
errorEst2 = CompoundValue(1, (1 - K2) * errorEst2[1] + (AbsValue(estimate2 - estimate2[1])), noise2);

# SECOND PASS. Plots
plot Kalman2 = estimate2;
Kalman2.SetDefaultColor(Color.YELLOW);
Kalman2.SetLineWeight(2);
Screenshot 2026-01-17 173700.png
 

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
975 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