Spyros Agrafiotis
Spyros Agrafiotis

Reputation: 3

Understanding Pinescript NZ function usage in this code

Please help me confirm my understanding of the following Pinescript function which implements a Kalman filter. I have included some comments in the code with my understanding/questions on what the code is trying to accomplish. Thanks!

kalman(x, g) =>
    //x is a moving average (MA) and g is a constant
    kf = 0.0 
    dk = x - nz(kf[1], x) //Calculate the difference between current bar's MA value and 
                          //MA value at the previous bar?
    smooth = nz(kf[1], x) + dk * math.sqrt(g * 2) //Is nz(kf[1], x) equal to the previous 
                                                  //bar's MA value?
    velo = 0.0
    velo := nz(velo[1], 0) + g * dk //Not sure what nz(velo[1], 0) is supposed to model. 
                                    //Is that building velo by adding velo's value on the 
                                    //previous bar to g*dk?
    kf := smooth + velo

Upvotes: 0

Views: 1696

Answers (1)

vitruvius
vitruvius

Reputation: 21382

nz() will replace na values with the given value.

nz(kf[1], x) will check if kf[1] (kf's value on the previous bar) is na. If it is na, it will replace it with x.

Upvotes: 2

Related Questions