I found this on the next link: Back Testing RSI Divergence Strategy on FX

The author of the post used the exponential moving average for RSI calculation, using this piece of code:

'''
Assuming you have a pandas OHLC Dataframe downloaded from Metatrader 5 historical data. 
'''
# Get the difference in price from previous step
Data = pd.DataFrame(Data)
delta = Data.iloc[:, 3].diff()
delta = delta[1:]

# Make the positive gains (up) and negative gains (down) Series
up, down = delta.copy(), delta.copy()
up[up < 0] = 0
down[down > 0] = 0
roll_up = pd.stats.moments.ewma(up, lookback)
roll_down = pd.stats.moments.ewma(down.abs(), lookback)

# Calculate the SMA
roll_up = roll_up[lookback:]
roll_down = roll_down[lookback:]
Data = Data.iloc[lookback + 1:,].values

# Calculate the RSI based on SMA
RS = roll_up / roll_down
RSI = (100.0 - (100.0 / (1.0 + RS)))
RSI = np.array(RSI)
RSI = np.reshape(RSI, (-1, 1))

Data = np.concatenate((Data, RSI), axis = 1)

At this point we have an array with OHLC data and a fifth column that has the RSI in it. Then added the next two columns:

  1. Column 6: Data[:, 5] will be for the bullish divergences and will have values of 0 or 1 (initiate buy).
  2. Column 7: Data[:, 6] will be for the bearish divergences and will have values of 0 or -1 (initiate short).

using this variables:

lower_barrier = 30
upper_barrier = 70
width = 10

Here is the code:

# Bullish Divergence
for i in range(len(Data)):
   try:
       if Data[i, 4] < lower_barrier:
           for a in range(i + 1, i + width):
               if Data[a, 4] > lower_barrier:
                    for r in range(a + 1, a + width):
                       if Data[r, 4] < lower_barrier and \
                        Data[r, 4] > Data[i, 4] and Data[r, 3] < Data[i, 3]:
                            for s in range(r + 1, r + width): 
                                if Data[s, 4] > lower_barrier:
                                    Data[s + 1, 5] = 1
                                    break
                                else:
                                    continue
                        else:
                            continue
                    else:
                        continue
                else:
                    continue
  except IndexError:
        pass

# Bearish Divergence
for i in range(len(Data)):
   try:
       if Data[i, 4] > upper_barrier:
           for a in range(i + 1, i + width): 
               if Data[a, 4] < upper_barrier:
                   for r in range(a + 1, a + width):
                       if Data[r, 4] > upper_barrier and \
                       Data[r, 4] < Data[i, 4] and Data[r, 3] > Data[i, 3]:
                           for s in range(r + 1, r + width):
                               if Data[s, 4] < upper_barrier:
                                   Data[s + 1, 6] = -1
                                   break
                               else:
                                   continue
                       else:
                           continue
                   else:
                       continue
               else:
                   continue
   except IndexError:
       pass
Answer from robbinc91 on Stack Overflow
🌐
GitHub
github.com › AmirRezaFarokhy › RSI-Divergence
GitHub - AmirRezaFarokhy/RSI-Divergence
Finding the divergence of the gold market(XAUUSD) based on the RSI indicator. It is one of the trading strategies. Tested and obtained percentage: 75% winning rate. ... # install numpy pip install numpy # install matplotlib pip install matplotlib ...
Starred by 16 users
Forked by 9 users
Languages   Jupyter Notebook 98.3% | Python 1.7% | Jupyter Notebook 98.3% | Python 1.7%
🌐
GitHub
github.com › SpiralDevelopment › RSI-divergence-detector
GitHub - SpiralDevelopment/RSI-divergence-detector: RSI divergence detector finds regular and hidden bullish and bearish divergences
March 22, 2022 - sample_tg_poster.py - Gets the ohlc data from local database and checks if the last candle has RSI divergence
Starred by 110 users
Forked by 39 users
Languages   Python 100.0% | Python 100.0%
🌐
GitHub
github.com › SpiralDevelopment › RSI-divergence-detector › blob › main › rsi_divergence_finder.py
RSI-divergence-detector/rsi_divergence_finder.py at main · SpiralDevelopment/RSI-divergence-detector
RSI_COLUMN = 'rsi' BASE_COLUMN = 'C' TIME_COLUMN = 'T' ANGLE_LIMIT = 45.0 # Limit for angle of divergence lines · · · def calc_percentage_increase(original, new): increase = (new - original) / original · return increase * 100 · · · # cur_candle_idx - index of the candle to which we compare candles in the past to find divergences ·
Author   SpiralDevelopment
Top answer
1 of 4
3

I found this on the next link: Back Testing RSI Divergence Strategy on FX

The author of the post used the exponential moving average for RSI calculation, using this piece of code:

'''
Assuming you have a pandas OHLC Dataframe downloaded from Metatrader 5 historical data. 
'''
# Get the difference in price from previous step
Data = pd.DataFrame(Data)
delta = Data.iloc[:, 3].diff()
delta = delta[1:]

# Make the positive gains (up) and negative gains (down) Series
up, down = delta.copy(), delta.copy()
up[up < 0] = 0
down[down > 0] = 0
roll_up = pd.stats.moments.ewma(up, lookback)
roll_down = pd.stats.moments.ewma(down.abs(), lookback)

# Calculate the SMA
roll_up = roll_up[lookback:]
roll_down = roll_down[lookback:]
Data = Data.iloc[lookback + 1:,].values

# Calculate the RSI based on SMA
RS = roll_up / roll_down
RSI = (100.0 - (100.0 / (1.0 + RS)))
RSI = np.array(RSI)
RSI = np.reshape(RSI, (-1, 1))

Data = np.concatenate((Data, RSI), axis = 1)

At this point we have an array with OHLC data and a fifth column that has the RSI in it. Then added the next two columns:

  1. Column 6: Data[:, 5] will be for the bullish divergences and will have values of 0 or 1 (initiate buy).
  2. Column 7: Data[:, 6] will be for the bearish divergences and will have values of 0 or -1 (initiate short).

using this variables:

lower_barrier = 30
upper_barrier = 70
width = 10

Here is the code:

# Bullish Divergence
for i in range(len(Data)):
   try:
       if Data[i, 4] < lower_barrier:
           for a in range(i + 1, i + width):
               if Data[a, 4] > lower_barrier:
                    for r in range(a + 1, a + width):
                       if Data[r, 4] < lower_barrier and \
                        Data[r, 4] > Data[i, 4] and Data[r, 3] < Data[i, 3]:
                            for s in range(r + 1, r + width): 
                                if Data[s, 4] > lower_barrier:
                                    Data[s + 1, 5] = 1
                                    break
                                else:
                                    continue
                        else:
                            continue
                    else:
                        continue
                else:
                    continue
  except IndexError:
        pass

# Bearish Divergence
for i in range(len(Data)):
   try:
       if Data[i, 4] > upper_barrier:
           for a in range(i + 1, i + width): 
               if Data[a, 4] < upper_barrier:
                   for r in range(a + 1, a + width):
                       if Data[r, 4] > upper_barrier and \
                       Data[r, 4] < Data[i, 4] and Data[r, 3] > Data[i, 3]:
                           for s in range(r + 1, r + width):
                               if Data[s, 4] < upper_barrier:
                                   Data[s + 1, 6] = -1
                                   break
                               else:
                                   continue
                       else:
                           continue
                   else:
                       continue
               else:
                   continue
   except IndexError:
       pass
2 of 4
3

I changed above code a bit hope this helps:

lower_barrier = 30
upper_barrier = 70
width = 5
#Bullish Divergence
for i in range(len(Data)):

   try:
     if Data.iloc[i, 4] < lower_barrier:
         for a in range(i + 1, i + width):
             if Data.iloc[a, 4] > lower_barrier:
                  for r in range(a + 1, a + width):
                     if Data.iloc[r, 4] < lower_barrier and Data.iloc[r, 4] > Data.iloc[i, 4] and Data.iloc[r, 3] < Data.iloc[i, 3]:
                         for s in range(r + 1, r + width): 
                            if Data.iloc[s, 4] > lower_barrier:
                                print('Bullish above',Data.iloc[s+1,1])
                                Data.iloc[s + 1, 5] = 1
                                break
                            else:
                                continue
                    else:
                        continue
            else:
                continue
    else:
        continue
except IndexError:
    pass
#Bearish Divergence
for i in range(len(Data)):
try:
    if Data.iloc[i, 4] > upper_barrier:
        for a in range(i + 1, i + width): 
            if Data.iloc[a, 4] < upper_barrier:
                for r in range(a + 1, a + width):
                    if Data.iloc[r, 4] > upper_barrier and Data.iloc[r, 4] < Data.iloc[i, 4] and Data.iloc[r, 3] > Data.iloc[i, 3]:
                        for s in range(r + 1, r + width):
                            if Data.iloc[s, 4] < upper_barrier:
                                print('Bearish below',Data.iloc[s+1,2])
                                Data.iloc[s + 1, 6] = -1
                                break
                            else:
                                continue
                    else:
                        continue
                else:
                    continue
            else:
                continue
except IndexError:
    pass
🌐
GitHub
github.com › thierryjmartin › freqtrade-stuff › blob › main › RSIDivergence.py
freqtrade-stuff/RSIDivergence.py at main · thierryjmartin/freqtrade-stuff
study(title="Divergence Indicator", format=format.price, resolution="") len = input(title="RSI Period", minval=1, defval=14) src = input(title="RSI Source", defval=close) lbR = input(title="Pivot Lookback Right", defval=5) # lookahead ·
Author   thierryjmartin
🌐
GitHub
github.com › je-suis-tm › quant-trading › blob › master › RSI Pattern Recognition backtest.py
quant-trading/RSI Pattern Recognition backtest.py at master · je-suis-tm/quant-trading
Python quantitative trading strategies including VIX Calculator, Pattern Recognition, Commodity Trading Advisor, Monte Carlo, Options Straddle, Shooting Star, London Breakout, Heikin-Ashi, Pair Trading, RSI, Bollinger Bands, Parabolic SAR, Dual Thrust, Awesome, MACD - quant-trading/RSI Pattern Recognition backtest.py at master · je-suis-tm/quant-trading
Author   je-suis-tm
Find elsewhere
🌐
GitHub
github.com › markpichler › rsi-divergence-detector
GitHub - markpichler/rsi-divergence-detector: A web app I'm developing that aims to assist cryptocurrency traders by detecting RSI divergence on multiple time frames across multiple trading pairs on various exchanges.
A web app I'm developing that aims to assist cryptocurrency traders by detecting RSI divergence on multiple time frames across multiple trading pairs on various exchanges. There was an error while loading.
Starred by 9 users
Forked by 9 users
Languages   Python 100.0% | Python 100.0%
🌐
Reddit
reddit.com › r/algotrading › i created a divergence indicator (python)
r/algotrading on Reddit: I created a divergence indicator (Python)
November 12, 2021 -

TLDR: I developed a divergence indicator in Python. Source code on my GitHub here.

Hi, I thought I would share a divergence indicator I have developed in Python. It can be used to detect divergence between any two datasets, whether that be price and an indicator, or two indicators.

I started by looking at indicators available on TradingView for inspiration and found their built-in divergence indicator, which uses pivot points to locate local turning points in price/indicators. My idea was to use a different method for this, which has turned out to be more responsive and hence less lagging. To do so, I apply a 2-period EMA to hl2 data and detected a new price level when the gradient of the ema changes. The chart below illustrates this idea. The same concept can be applied to indicators.

Dashed lines appear when a new 'support' or 'resistance' level is detected. The sensitivity of this indicator can be controlled by the period of the EMA.

Since this method will sometimes detect new levels due to noise, I filter for 'strong' levels by only accepting levels which last greater than (for example) 2 candles. Now it is possible to detect higher-highs, lower-lows, lower-highs and higher-lows. An example of this is shown in the chart below, where lower-lows in price are detected.

Detection of lower-lows using price levels.

Now the same process can be applied to indicators instead of price data: instead of taking the EMA of price, passing the indicator data straight in is sufficient. This will work for RSI, Stochastics, MACD, or any other data source.

As an example, I have applied it the RSI. As per the definition, regular bullish divergence occurs when price makes a lower-low at the same time an indicator makes a higher-low. By comparing the levels detected in price to the levels detected in the indicator, divergence between them can be detected. The chart below shows an example of regular bullish divergence being detected.

An example of regular bullish divergence being detected.

For comparison, the screenshot below shows TradingView's divergence indicator on the same instrument (EUR/USD on D) at the same time. Although the bullish tag appears earlier on the chart, the signal will not actually appear until the candle I have highlighted with a vertical line (try using the replay function to see this for yourself - it is a bit misleading when the signal would be received live). In this (admittedly cherry-picked) example, the indicator I have developed picks up the signal 3 candles before the TradingView indicator, which equals approximately 100 pips.

TradingView's divergence indicator.

The source code for the indicator can be found on my GitHub (link at the top of this post). The module containing the code is actually a part of my entire trading project, but using the indicators from that module is easy enough.

Note that I have split the processes above into three indicators. There are some parameters which can be used to tune the sensitivity of each, but I will just give a brief overview of how to use them to reproduce the results above. They are also commented in the code itself. The indicators are:

  • find_swings(): to detect the reversal levels. This function accepts OHLC data and indicator data sources. If you pass in the data from an indicator (ie. a 1D array), use the data_type='other' input parameter to specify that it is not OHLC data. This function will return a DataFrame with columns Highs, Lows, Last and Trend. The first three contain the values of the data source at high swings, low swings and the most recent swing, respectively. The Trend column contains 1 when there is an implied uptrend, and -1 when there is an implied downtrend.

  • classify_swings(): to classify the swing levels as higher-highs, lower-lows, lower-highs and higher-lows. The main input to this function is the DataFrame output of find_swings(). The output is another DataFrame with columns "HH", "LL", "LH" and "HL".

  • detect_divergence(): to compare the classified swings of two data sources (for example, price data and RSI). This function requires two "classified swings" DataFrames as inputs, as outputted from the classify_swings() function. It will then return a DataFrame with True/False columns titled 'regularBull', 'regularBear', 'hiddenBull', and 'hiddenBear', corresponding to the regular and hidden divergences detected.

I have also wrapped all of the indicators above into a single indicator, autodetect_divergence. The inputs to this functions are the OHLC data and the indicator data. It will return the same thing as detect_divergence, described above.

Hopefully this post was interesting and maybe gave someone some ideas. Please let me know if you have any suggestions or questions. Thanks for reading!

🌐
GitHub
github.com › topics › rsi
rsi · GitHub Topics · GitHub
Python · Star 106 · RSI divergence detector finds regular and hidden bullish and bearish divergences · trading technical-analysis algorithmic-trading rsi stock-trading relative-strength-index crypto-trading · Updated · Jun 7, 2023 · Python ...
🌐
GitHub
github.com › bitcorr › find-divergence-in-rsi
GitHub - bitcorr/find-divergence-in-rsi: This code helps identify tops and bottoms and find rsi divergence in price action
This code helps identify tops and bottoms and find rsi divergence in price action. The identification of tops & bottoms is made by taking the MA of the price, smoothing it and then the changes in the gradient are the tops & bottoms.
Starred by 15 users
Forked by 3 users
Languages   Python 100.0% | Python 100.0%
🌐
GitHub
github.com › HenrisonTao › Bimex_RSI_Divergence
GitHub - HenrisonTao/Bimex_RSI_Divergence: The program was designed to monitor the RSI divergence event in Bitcoin, then using the Line Notify to alert the user that there has a trading opportunity.
The program was designed to monitor the RSI divergence event in Bitcoin, then using the Line Notify to alert the user that there has a trading opportunity. The program needs to install ccxt and ta-lib. pip install ccxt TA-Lib · The program ...
Starred by 8 users
Forked by 2 users
Languages   Python 100.0% | Python 100.0%
🌐
YouTube
youtube.com › watch
RSI Divergence Automated In Python | Algorithmic Trading - YouTube
The RSI Divergence Detection is programmed in python as an automated indicator for algorithmic trading, the Jupyter notebook file is available from the link:...
Published   November 12, 2021
🌐
GitHub
github.com › AmirRezaFarokhy › RSI-Divergence › blob › master › diverges.py
RSI-Divergence/diverges.py at master · AmirRezaFarokhy/RSI-Divergence
lineـslope = (main_df["RSI"].iloc[point[1]] - main_df["RSI"].iloc[point[0]]) / (point[1] - point[0])
Author   AmirRezaFarokhy
🌐
GitHub
github.com › mtamer › python-rsi
GitHub - mtamer/python-rsi: RSI (Relative Strength Index) written in Python · GitHub
RSI (Relative Strength Index) written in Python. Contribute to mtamer/python-rsi development by creating an account on GitHub.
Starred by 144 users
Forked by 53 users
Languages   Python
🌐
GitHub
github.com › bhargavaparoksham › RSI-Divergence
GitHub - bhargavaparoksham/RSI-Divergence: Detecting RSI Divergence on TradingView charts using Pinescript. · GitHub
Detecting RSI Divergence on TradingView charts using Pinescript. - bhargavaparoksham/RSI-Divergence
Starred by 16 users
Forked by 3 users
🌐
GitHub
github.com › GZotin › RSI_MACD_strategy
GitHub - GZotin/RSI_MACD_strategy: Python script for trading analysis using RSI and MACD indicators.
Create a python script using Binance API and Pandas TA that can indicate buy and sell operations aiming profit in the crypto market. Utilizing Relative Strength Index (RSI) and Moving Average Convergence/Divergence (MACD), both technical indicators ...
Starred by 22 users
Forked by 5 users
Languages   Python 100.0% | Python 100.0%
🌐
GitHub
github.com › topics › relative-strength-index
Build software better, together
python jupyter-notebook pandas financial-analysis exponential-moving-average simple-moving-average datareader relative-strength-index pandas-datareader bollinger-bands colaboratory money-flow-index yfinance quantative-analysis double-exponential moving-average-convergence-divergence 3x-leveraged ... A financial trading algorithm based on limit orders, relying on relative strength index indicator to take an action, optimized via Nelder-Mead algorithm for optimum RSi period, stop loss/take profit, and other parameters.
🌐
GitHub
github.com › lukaszbinden › rsi_tradingview
GitHub - lukaszbinden/rsi_tradingview: Python implementation of RSI indicator as defined in TradingView
Python implementation of RSI indicator as defined in TradingView - lukaszbinden/rsi_tradingview
Starred by 63 users
Forked by 21 users
Languages   Python 100.0% | Python 100.0%
🌐
GitHub
github.com › AmirRezaFarokhy › RSI-Divergence › blob › master › diverge.ipynb
RSI-Divergence/diverge.ipynb at master · AmirRezaFarokhy/RSI-Divergence
" elif df[\"RSI\"].iloc[i]<30 and df[\"RSI\"].iloc[i]<df[\"RSI\"].iloc[i+1] and df[\"RSI\"].iloc[i]<df[\"RSI\"].iloc[i-1] and df[\"RSI\"].iloc[i]<df[\"RSI\"].iloc[i-2] and df[\"RSI\"].iloc[i]<df[\"RSI\"].iloc[i-3]:\n",
Author   AmirRezaFarokhy