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!
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);