title: tradingview close -close[1] accuracy
body: <p>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- close<a href="https://i.stack.imgur.com/imYwn.jpg" rel="nofollow noreferrer">1</a>). As you can see in the below image the pine script close-close<a href="https://i.stack.imgur.com/imYwn.jpg" rel="nofollow noreferrer">1</a>= 0.10 while 97.42-97.31=0.11!. This difference come from what?  <a href="https://i.stack.imgur.com/imYwn.jpg" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/imYwn.jpg" alt="enter image description here"></a></p>
  
  <p>for calculating RMA the Pine script use sum<a href="https://i.stack.imgur.com/imYwn.jpg" rel="nofollow noreferrer">1</a> please explain how this value is computed with an example.</p>
  
  <p>Let me assume we have the following closing price data for 3 days. exactly what happens has occurred for computing RSI.  </p>
  
  <p><a href="https://i.stack.imgur.com/rW5Hg.jpg" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/rW5Hg.jpg" alt="enter image description here"></a></p>
  
  <pre><code>  // @version=4
  
      study(title = "MyRSI", overlay=true)
  
     rolling_moving_average(data, length) =&gt;
         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) =&gt;
         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) =&gt;
         u = max(data - data[1], 0.0)
         d = max(data[1] - data, 0.0)
         //rgain = rolling_moving_average(u,length)
         u  
  
  
      MyRmaloss(data, length) =&gt;
         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))
  </code></pre>