1
votes

I wrote a Pine script as follow for considering RSI indicator. In this code for calculating RSI, need to subtract closing price of the two neighboring candles (close- close1). As you can see in the below image the pine script close-close1= 0.10 while 97.42-97.31=0.11!. This difference come from what? enter image description here

for calculating RMA the Pine script use sum1 please explain how this value is computed with an example.

Let me assume we have the following closing price data for 3 days. exactly what happens has occurred for computing RSI.

enter image description here

  // @version=4

    study(title = "MyRSI", overlay=true)

   rolling_moving_average(data, length) =>
       alpha =  length 
       sum = 0.0
       for index = length to 0
          if sum == 0.0
             sum := data[index]
          else
            sum := (data[index] + (alpha - 1) * nz(sum[1]))/alpha 

    MyRSI(data, length) =>
       u = max(data - data[1], 0.0)
       d = max(data[1] - data, 0.0)
       rgain = rolling_moving_average(u,length)
       rloss = rolling_moving_average(d,length)
       rs = rgain / rloss 
       rsi = 100 - 100 / ( 1 + rs )
       rsi



    MyRmagain(data, length) =>
       u = max(data - data[1], 0.0)
       d = max(data[1] - data, 0.0)
       //rgain = rolling_moving_average(u,length)
       u  


    MyRmaloss(data, length) =>
       u = max(data - data[1], 0.0)
       d = max(data[1] - data, 0.0)
       //rloss = rolling_moving_average(d,length)
       d

    //atr2 = MyRmagain(close, 3)
    plot(MyRmagain(close, 3), title="mm[1]", color=#00FF00)
    //plot(close)
    //atr2 = MyRmaloss(close, 3)
    plot(MyRmaloss(close, 3), title="mm[1]", color=#FF0000)
    plot(close,"close",#0000FF)
    plot(close[1],"close",#ff00FF)
    //atr2 := rolling_moving_average(close, 14)
    //plot(atr2, title="EMAUP2", color=#FF00FF)

    //atr = rma(close, 14)
    //plot(atr, title="EMAUP", color=#FF00FF)
    //plot(MyRSI(close, 14))
1

1 Answers

0
votes

It's a problem of precision. DXY's default is two numbers after the point whereas there could be more of number after the point:

enter image description here

To fix thix behaviour, change the precision in the settings: enter image description here

That should help.