title: Calculate/replicate RSI from Tradingview's pine script
body: <p>Im trying to recreate Tradingviews pine script RSI code into Javascript code. But having a hard time figuring out how it works. I made the basic RSI using a normal moving average calculation. But the pine script uses exponential weighted moving average. And there documentation is really hard to follow to me. This is the pine script.</p>
  <pre class="lang-js prettyprint-override"><code>//@version=4
  study(title=&quot;Relative Strength Index&quot;, shorttitle=&quot;RSI&quot;, format=format.price, precision=2, resolution=&quot;&quot;)
  len = input(14, minval=1, title=&quot;Length&quot;)
  src = input(close, &quot;Source&quot;, type = input.source)
  up = rma(max(change(src), 0), len)
  down = rma(-min(change(src), 0), len)
  rsi = down == 0 ? 100 : up == 0 ? 0 : 100 - (100 / (1 + up / down))
  plot(rsi, &quot;RSI&quot;, color=#7E57C2)
  band1 = hline(70, &quot;Upper Band&quot;, color=#787B86)
  bandm = hline(50, &quot;Middle Band&quot;, color=color.new(#787B86, 50))
  band0 = hline(30, &quot;Lower Band&quot;, color=#787B86)
  fill(band1, band0, color=color.rgb(126, 87, 194, 90), title=&quot;Background&quot;)
  </code></pre>
  <p>This is what I oould make of it in Javascript:</p>
  <pre class="lang-js prettyprint-override"><code>// Period = 200
  // Close variable is 200 closed values. Where [0] in array = oldest, [199] in array = newest value.
  
  /**
   * Relative strength index. Based on closed periods.
   * 
   * @param {Array} close 
   * @param {Integer} period 
   * @returns 
   */
  function calculateRSI(close, period) {
  
      // Only calculate if it is worth it. First {period - 1} amount of calculations aren't correct anyway.
      if (close.length &lt; period) {
          return 50;
      }
  
      let averageGain = 0;
      let averageLoss = 0;
      const alpha = 1 / period;
  
      // Exponential weighted moving average.
      for (let i = 1; i &lt; period; i++)
      {
          let change = close[i] - close[i - 1];
  
          if (change &gt;= 0) {
              averageGain = alpha * change + (1 - alpha) * averageGain;
          } else {
              averageLoss = alpha * -change + (1 - alpha) * averageLoss;
          }
      }
  
      // Tried this too, but seems to not really matter.
      // To get an actual average.
      // averageGain /= period;
      // averageLoss /= period;
  
      // Calculate relative strength index. Where it can only be between 0 and 100.
      var rsi = 100 - (100 / (1 + (averageGain / averageLoss)));
  
      return rsi;
  }
  </code></pre>
  <p>The results this function gives on my chart is not too bad, but it just isn't the same as I have it in Tradingview. I belive im missing something that the pine script does and I don't.</p>
  <p><strong>Things I dont understand of the pine script:</strong></p>
  <ul>
  <li>When does it do a <code>for loop</code>? I don't see it in there functions. If they don't, how do they calculate the average for a period of longer than 2? You have to loop for that right?</li>
  <li>How does the <code>rma</code> function work? <a href="https://www.tradingview.com/pine-script-reference/v4/#fun_rma" rel="nofollow noreferrer">This is their docs</a>.</li>
  </ul>
  <p>I might have too many questions on this, but I think if you show a somewhat working example in Javascript of the RSI calculation like they do. Then I can probably make sense of it.</p>
  <p><strong>Is my calculation in Javascript correct to the one in the pine script?</strong></p>