title: How to implement RSI Divergence in Python
body: <p>I was wondering is there any Python library that covers <code>RSI-Divergence</code> (difference between a fast and a slow <code>RSI</code>) or any guidence about how can I implement its algorithm in Python.</p>
  <p>Already asked question: <a href="https://quant.stackexchange.com/q/40491/51662">Programmatically detect RSI divergence</a>. One of the answer suggests <a href="https://www.quantconnect.com/forum/discussion/1417/zigzag-indicator/p1" rel="noreferrer">quantconnect forum</a> for the Python version but it does not cover anything.</p>
  <p>I was not able to find its mathematical formula but I was able to find the <a href="https://www.tradingview.com/script/fH6e5TuN-RSI-Divergence/" rel="noreferrer">RSI-Divergence in pine-script</a>, as below, but I was not able to convert it into Python since its not possible to debug <code>pine-script</code> using <a href="https://www.tradingview.com/support/solutions/43000561836-what-is-pine-script/" rel="noreferrer">tradingview</a>.</p>
  <blockquote>
  <pre><code>study(title=&quot;RSI Divergence&quot;, shorttitle=&quot;RSI Divergence&quot;)
  src_fast = close, len_fast = input(5, minval=1, title=&quot;Length Fast RSI&quot;)
  src_slow = close, len_slow = input(14,minval=1, title=&quot;Length Slow RSI&quot;)
  up_fast = rma(max(change(src_fast), 0), len_fast)
  down_fast = rma(-min(change(src_fast), 0), len_fast)
  rsi_fast = down_fast == 0 ? 100 : up_fast == 0 ? 0 : 100 - (100 / (1 + up_fast / down_fast))
  up_slow = rma(max(change(src_slow), 0), len_slow)
  down_slow = rma(-min(change(src_slow), 0), len_slow)
  rsi_slow = down_slow == 0 ? 100 : up_slow == 0 ? 0 : 100 - (100 / (1 + up_slow / down_slow))
  divergence = rsi_fast - rsi_slow
  plotdiv = plot(divergence, color = divergence &gt; 0 ? lime:red, linewidth = 2)
  band = hline(0)
  </code></pre>
  </blockquote>