How TradingView Pine Script RMA works?

Viewed 1285

I faced the problem that I wanted to implement the same indicators as in the trading view, but I can't correctly implement the RMA indicator.

If I refer to the documentation, I get the following code:

pine_rma(src, length) =>
    alpha = length
    sum = 0.0
    sum := na(sum[1]) ? sma(src, length) : (src + (alpha - 1) * nz(sum[1])) / alpha

Everything in it is clear to me, except for the when na(sum[1]) condition may occur. And do I understand correctly that nz(sum[1]) is the previous value of sum.

When I try to implement it in golang, they write the following code:

func RMA(value []float64, period int) float64 {
    var sum float64 

    for i := period - 1; i >= 0; i-- {
        sum = (sum * (float64(period) - 1) + value[i]) / float64(period)
    }

    return sum
}

And I am having strong discrepancies in the returned data.

I tried to understand what values ​​are obtained in order from the RMA with a period of 3, when the input parameters for it are

src[0] = 0, src[1] = 0 and src[2] = 0.12

and we get that

rma[0] = 0.06, rma [1] = 0.04 and rma [2] = 0.03

and I don't understand where such numbers are taken, because I substitute src in the original formula, we get the most different numbers.

I took the benchmark data from the down array created for RSI from the FORD MTR CO DEL, 1D, Cboe BZX chart for September 2, 2020.

0 Answers
Related