PINE LIBRARY
Kalman Filter

Kalman Filter
Joint-state Linear / EKF / UKF steps for CV and CA price filters. Helpers private; consumer owns buffers and noise inputs.
Exported functions
Matrix helpers, Jacobian, and sigma-point utilities stay private inside the library.
Usage
The consumer owns var state/covariance buffers and workspace arrays, then calls a step each bar:
[code]
//version=6
indicator("Example", overlay = true)
import igor_sinkovec/kalman_filter/1 as kf
var float[] x = array.new_float(3, na)
var float[] P = array.new_float(9, 0.0)
var float[] xpred = array.new_float(3, 0.0)
var float[] Ppred = array.new_float(9, 0.0)
var float[] F = array.new_float(9, 0.0)
var float[] Ft = array.new_float(9, 0.0)
var float[] tmp = array.new_float(9, 0.0)
var float[] K = array.new_float(3, 0.0)
int n = 2
if barstate.isfirst or na(array.get(x, 0))
kf.initFilter(x, P, n, close)
float filtered = kf.linearStep(x, P, n, close, 0.01, 0.001, 0.0, 3.0, xpred, Ppred, F, Ft, tmp, K)
plot(filtered)
[/code]
Important notes
Licence
© igor_sinkovec — Mozilla Public License 2.0.
Library "kalman_filter"
initFilter(x, P, n, z)
Initialize joint-state mean and covariance (level = z, other components 0; diagonal P = 1).
Parameters:
x (array<float>): State vector buffer (length >= 3)
P (array<float>): Covariance buffer (9 elements, row-major 3x3 layout)
n (int): State dimension: 2 (CV) or 3 (CA)
z (float): Initial measurement (level)
Returns: Initial level z
linearStep(x, P, n, z, q0, q1, q2, R, xpred, Ppred, F, Ft, tmp, K)
One linear joint-state Kalman predict/update step (level observation).
Parameters:
x (array<float>): State vector (in/out)
P (array<float>): Covariance (in/out)
n (int): State dimension: 2 or 3
z (float): Measurement
q0 (float): Process noise (level)
q1 (float): Process noise (velocity)
q2 (float): Process noise (acceleration; used when n = 3)
R (float): Measurement noise
xpred (array<float>): Workspace: predicted state
Ppred (array<float>): Workspace: predicted covariance
F (array<float>): Workspace: transition matrix
Ft (array<float>): Workspace: F transpose
tmp (array<float>): Workspace: matrix multiply temp
K (array<float>): Workspace: Kalman gain
Returns: Filtered level
ekfStep(x, P, n, z, dmp, q0, q1, q2, R, xpred, Ppred, F, Ft, tmp, K)
One extended Kalman step with velocity damping nonlinearity.
Parameters:
x (array<float>): State vector (in/out)
P (array<float>): Covariance (in/out)
n (int): State dimension: 2 or 3
z (float): Measurement
dmp (float): Velocity damping coefficient
q0 (float): Process noise (level)
q1 (float): Process noise (velocity)
q2 (float): Process noise (acceleration; used when n = 3)
R (float): Measurement noise
xpred (array<float>): Workspace: predicted state
Ppred (array<float>): Workspace: predicted covariance
F (array<float>): Workspace: Jacobian / transition
Ft (array<float>): Workspace: F transpose
tmp (array<float>): Workspace: matrix multiply temp
K (array<float>): Workspace: Kalman gain
Returns: Filtered level
ukfStep(x, P, n, z, dmp, q0, q1, q2, R, alpha, beta, kappa, xpred, Ppred, F, Ft, tmp, tmp2, Lchol, xi, yi, K, Ysig)
One unscented Kalman step with the same velocity damping as ekfStep; falls back to EKF on Cholesky failure.
Parameters:
x (array<float>): State vector (in/out)
P (array<float>): Covariance (in/out)
n (int): State dimension: 2 or 3
z (float): Measurement
dmp (float): Velocity damping coefficient
q0 (float): Process noise (level)
q1 (float): Process noise (velocity)
q2 (float): Process noise (acceleration; used when n = 3)
R (float): Measurement noise
alpha (float): UKF alpha
beta (float): UKF beta
kappa (float): UKF kappa
xpred (array<float>): Workspace: predicted state
Ppred (array<float>): Workspace: predicted covariance
F (array<float>): Workspace: unused except EKF fallback
Ft (array<float>): Workspace: unused except EKF fallback
tmp (array<float>): Workspace: unused except EKF fallback
tmp2 (array<float>): Workspace: scaled covariance for Cholesky
Lchol (array<float>): Workspace: Cholesky factor
xi (array<float>): Workspace: sigma point state
yi (array<float>): Workspace: transformed sigma point
K (array<float>): Workspace: Kalman gain / cross-cov accumulator
Ysig (array<float>): Workspace: stored sigma rows (21 floats)
Returns: Filtered level
Joint-state Linear / EKF / UKF steps for CV and CA price filters. Helpers private; consumer owns buffers and noise inputs.
Exported functions
- initFilter() — initialize state mean and diagonal covariance from the first measurement
- linearStep() — one linear joint-state KF predict/update (level observation)
- ekfStep() — one extended KF step with velocity damping
- ukfStep() — one unscented KF step with the same nonlinearity (falls back to EKF if Cholesky fails)
Matrix helpers, Jacobian, and sigma-point utilities stay private inside the library.
Usage
The consumer owns var state/covariance buffers and workspace arrays, then calls a step each bar:
[code]
//version=6
indicator("Example", overlay = true)
import igor_sinkovec/kalman_filter/1 as kf
var float[] x = array.new_float(3, na)
var float[] P = array.new_float(9, 0.0)
var float[] xpred = array.new_float(3, 0.0)
var float[] Ppred = array.new_float(9, 0.0)
var float[] F = array.new_float(9, 0.0)
var float[] Ft = array.new_float(9, 0.0)
var float[] tmp = array.new_float(9, 0.0)
var float[] K = array.new_float(3, 0.0)
int n = 2
if barstate.isfirst or na(array.get(x, 0))
kf.initFilter(x, P, n, close)
float filtered = kf.linearStep(x, P, n, close, 0.01, 0.001, 0.0, 3.0, xpred, Ppred, F, Ft, tmp, K)
plot(filtered)
[/code]
Important notes
- Consumers wire observation noise, process-noise diagonals, and UKF (α, β, κ) themselves.
- Buffers use a fixed 3×3 row-major layout (9 covariance slots) even when n = 2.
Licence
© igor_sinkovec — Mozilla Public License 2.0.
Library "kalman_filter"
initFilter(x, P, n, z)
Initialize joint-state mean and covariance (level = z, other components 0; diagonal P = 1).
Parameters:
x (array<float>): State vector buffer (length >= 3)
P (array<float>): Covariance buffer (9 elements, row-major 3x3 layout)
n (int): State dimension: 2 (CV) or 3 (CA)
z (float): Initial measurement (level)
Returns: Initial level z
linearStep(x, P, n, z, q0, q1, q2, R, xpred, Ppred, F, Ft, tmp, K)
One linear joint-state Kalman predict/update step (level observation).
Parameters:
x (array<float>): State vector (in/out)
P (array<float>): Covariance (in/out)
n (int): State dimension: 2 or 3
z (float): Measurement
q0 (float): Process noise (level)
q1 (float): Process noise (velocity)
q2 (float): Process noise (acceleration; used when n = 3)
R (float): Measurement noise
xpred (array<float>): Workspace: predicted state
Ppred (array<float>): Workspace: predicted covariance
F (array<float>): Workspace: transition matrix
Ft (array<float>): Workspace: F transpose
tmp (array<float>): Workspace: matrix multiply temp
K (array<float>): Workspace: Kalman gain
Returns: Filtered level
ekfStep(x, P, n, z, dmp, q0, q1, q2, R, xpred, Ppred, F, Ft, tmp, K)
One extended Kalman step with velocity damping nonlinearity.
Parameters:
x (array<float>): State vector (in/out)
P (array<float>): Covariance (in/out)
n (int): State dimension: 2 or 3
z (float): Measurement
dmp (float): Velocity damping coefficient
q0 (float): Process noise (level)
q1 (float): Process noise (velocity)
q2 (float): Process noise (acceleration; used when n = 3)
R (float): Measurement noise
xpred (array<float>): Workspace: predicted state
Ppred (array<float>): Workspace: predicted covariance
F (array<float>): Workspace: Jacobian / transition
Ft (array<float>): Workspace: F transpose
tmp (array<float>): Workspace: matrix multiply temp
K (array<float>): Workspace: Kalman gain
Returns: Filtered level
ukfStep(x, P, n, z, dmp, q0, q1, q2, R, alpha, beta, kappa, xpred, Ppred, F, Ft, tmp, tmp2, Lchol, xi, yi, K, Ysig)
One unscented Kalman step with the same velocity damping as ekfStep; falls back to EKF on Cholesky failure.
Parameters:
x (array<float>): State vector (in/out)
P (array<float>): Covariance (in/out)
n (int): State dimension: 2 or 3
z (float): Measurement
dmp (float): Velocity damping coefficient
q0 (float): Process noise (level)
q1 (float): Process noise (velocity)
q2 (float): Process noise (acceleration; used when n = 3)
R (float): Measurement noise
alpha (float): UKF alpha
beta (float): UKF beta
kappa (float): UKF kappa
xpred (array<float>): Workspace: predicted state
Ppred (array<float>): Workspace: predicted covariance
F (array<float>): Workspace: unused except EKF fallback
Ft (array<float>): Workspace: unused except EKF fallback
tmp (array<float>): Workspace: unused except EKF fallback
tmp2 (array<float>): Workspace: scaled covariance for Cholesky
Lchol (array<float>): Workspace: Cholesky factor
xi (array<float>): Workspace: sigma point state
yi (array<float>): Workspace: transformed sigma point
K (array<float>): Workspace: Kalman gain / cross-cov accumulator
Ysig (array<float>): Workspace: stored sigma rows (21 floats)
Returns: Filtered level
Pine kitaplığı
Gerçek TradingView ruhuyla, yazar bu Pine kodunu açık kaynaklı bir kütüphane olarak yayınladı, böylece topluluğumuzdaki diğer Pine programcıları onu yeniden kullanabilir. Yazarı tebrik ederiz! Bu kütüphaneyi özel olarak veya diğer açık kaynaklı yayınlarda kullanabilirsiniz, ancak bu kodun yayınlarda yeniden kullanılması Topluluk Kuralları tarafından yönetilir.
Feragatname
Bilgiler ve yayınlar, TradingView tarafından sağlanan veya onaylanan finansal, yatırım, alım satım veya diğer türden tavsiye veya öneriler anlamına gelmez ve teşkil etmez. Kullanım Koşulları bölümünde daha fazlasını okuyun.
Pine kitaplığı
Gerçek TradingView ruhuyla, yazar bu Pine kodunu açık kaynaklı bir kütüphane olarak yayınladı, böylece topluluğumuzdaki diğer Pine programcıları onu yeniden kullanabilir. Yazarı tebrik ederiz! Bu kütüphaneyi özel olarak veya diğer açık kaynaklı yayınlarda kullanabilirsiniz, ancak bu kodun yayınlarda yeniden kullanılması Topluluk Kuralları tarafından yönetilir.
Feragatname
Bilgiler ve yayınlar, TradingView tarafından sağlanan veya onaylanan finansal, yatırım, alım satım veya diğer türden tavsiye veya öneriler anlamına gelmez ve teşkil etmez. Kullanım Koşulları bölümünde daha fazlasını okuyun.