title: Calculate RSI based on Kraken OHLC
body: <p>I want to exactly mirror the RSI values on <a href="https://cryptowatch.de/markets/kraken/ltc/eur/1h" rel="nofollow noreferrer">cryptowatch.de</a> (In my case LTC-EUR), I used the site <a href="http://stockcharts.com/school/doku.php?id=chart_school:technical_indicators:relative_strength_index_rsi" rel="nofollow noreferrer">stockcharts.com</a>, which explains how to calculate the RSI, to write the calculator in Javascript (node).</p>
  <p>My code so far:</p>
  <pre><code>// data is an array of open-prices in descending date order (the current price is the last element)
  function calculateRSI(data) {
    data = data.reverse(); // Reverse to handle it better
    let avgGain = 0;
    let aveLoss = 0;
  
    // Calculate first 14 periods
    for (let i = 0; i &lt; 14; i++) {
      const ch = data[i] - data[i + 1];
  
      if (ch &gt;= 0) {
        avgGain += ch;
      } else {
        aveLoss -= ch;
      }
    }
  
    avgGain /= 14;
    aveLoss /= 14;
  
    // Smooth values 250 times
    for (let i = 14; i &lt; 264; i++) {
      const ch = data[i] - data[i + 1];
  
      if (ch &gt;= 0) {
        avgGain = (avgGain * 13 + ch) / 14;
        aveLoss = (aveLoss * 13) / 14;
      } else {
        avgGain = (avgGain * 13) / 14;
        aveLoss = (aveLoss * 13 - ch) / 14;
      }
    }
  
    // Calculate relative strength index
    const rs = avgGain / aveLoss;
    return 100 - 100 / (1 + rs);
  }
  </code></pre>
  <p>But the result is always far from the values which are displayed on  <a href="https://cryptowatch.de/markets/kraken/ltc/eur/1h" rel="nofollow noreferrer">cryptowatch.de</a>, what's wrong? How to calculate it correctly?
  (Post in other programming languages are also okay)</p>
  <p>Thanks @jingx but the results are still wrong</p>