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:
- Column 6: Data[:, 5] will be for the bullish divergences and will have values of 0 or 1 (initiate buy).
- 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 OverflowCreating a complex (maybe basic but strong ) strategy
algorithm - How to implement RSI Divergence in Python - Stack Overflow
Coding price-RSI divergence?
Stochastic and RSI Strategy
Videos
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:
- Column 6: Data[:, 5] will be for the bullish divergences and will have values of 0 or 1 (initiate buy).
- 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
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
Dear all, I would like to use Python to detect bearish and bullish divergences on the price/RSI. In my thought progress the way this could be coded is as follows:
-
Calculate relative maxima and minima.
-
Calculate RSI at those points.
-
For each pair of lows and highs, compare the change in price with the difference in RSI.
Has anyone ever tried coding this? Thanks
Quick disclaimer before some of you insult me in the comments, I'm not looking for an easy way to make money. I'm just a programmer that thinks that cryptocurrencies and blockchain in general are the future and wants to learn from trading experts.
Having said so, I recently discovered Freqtrade and I fell in love with how beautiful its code is. I know something about trading but I'm certainly no expert and I wanted to get some information from people who definitely have more knowledge then me on what is the best trading strategy for the current market state and for other states in general.