title: How TradingView Pine Script RMA function works internally?
body: <p>I'm trying to re-implement the <code>rma</code> function from <code>TradingView</code> <code>pinescript</code> but I cannot make it output the same result as the original function.</p>
  
  <p>Here is the code I developed, the code is basically the <code>ema</code> function, but it differs greatly from the <code>rma</code> function <code>plot</code> result when charting:</p>
  
  <pre><code>//@version=3
  study(title = "test", overlay=true)
  
  rolling_moving_average(data, length) =&gt;
      alpha = 2 / (length + 1)
      sum = 0.0
      for index = length to 0
          if sum == 0.0
              sum := data[index]
          else
              sum := alpha * data[index] + (1 - alpha) * sum
  
  atr2 = rolling_moving_average(close, 5)
  plot(atr2, title="EMAUP2", color=blue)
  
  atr = rma(close, 5)
  plot(atr, title="EMAUP", color=red)
  </code></pre>
  
  <p>So my question is how is the <code>rma</code> function works internally so I can implement a clone of it?</p>
  
  <p>PS. Here is the link to the documentation <a href="https://www.tradingview.com/study-script-reference/#fun_rma" rel="nofollow noreferrer">https://www.tradingview.com/study-script-reference/#fun_rma</a> It does show a possible implementation, but it does not work when running it.</p>