如何在 Python 中实现 RSI Divergence [英] How to implement RSI Divergence in Python
问题描述
我想知道是否有任何涵盖 RSI-Divergence
(快速和慢速 RSI
之间的区别)的 Python 库或有关如何实现其算法的任何指导在 Python 中.
已提出问题:以编程方式检测 RSI 背离.答案之一建议 quantconnect forum 用于 Python 版本,但它不包括任何内容.
我找不到它的数学公式,但我找到了 RSI- pine-script 的分歧,如下所示,但我无法将其转换为 Python,因为它无法使用 tradingview.
<块引用>study(title="RSI Divergence", shorttitle="RSI Divergence")src_fast = close, len_fast = input(5, minval=1, title=长度快速 RSI")src_slow = 关闭,len_slow = 输入(14,minval=1,title=长度慢 RSI")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))背离 = rsi_fast - rsi_slowplotdiv = plot(divergence, color = divergence > 0 ?lime:red, linewidth = 2)带 = hline(0)
我在下一个链接中找到了这个:反向测试RSI分歧外汇策略
该帖子的作者使用指数移动平均线进行 RSI 计算,使用这段代码:
'''假设您有一个从 Metatrader 5 历史数据下载的 Pandas OHLC 数据框.'''# 获取与上一步的价格差异数据 = pd.DataFrame(Data)delta = Data.iloc[:, 3].diff()delta = delta[1:]# 使正收益(向上)和负收益(向下)系列向上,向下 = delta.copy(), delta.copy()向上[向上<0] = 0向下[向下>0] = 0roll_up = pd.stats.moments.ewma(向上,回顾)roll_down = pd.stats.moments.ewma(down.abs(), 回顾)# 计算SMAroll_up = roll_up[回顾:]roll_down = roll_down[回顾:]Data = Data.iloc[lookback + 1:,].values# 根据SMA计算RSIRS = roll_up/roll_downRSI = (100.0 - (100.0/(1.0 + RS)))RSI = np.array(RSI)RSI = np.reshape(RSI, (-1, 1))数据 = np.concatenate((Data, RSI), 轴 = 1)
此时我们有一个包含 OHLC 数据的数组和一个包含 RSI 的第五列.然后添加了接下来的两列:
- 第 6 列:数据[:, 5] 将用于看涨背离,值为 0 或 1(开始买入).
- 第 7 列:数据 [:, 6] 将用于看跌背离,值为 0 或 -1(开始做空).
使用这个变量:
lower_barrier = 30上屏障 = 70宽度 = 10
代码如下:
# 看涨背离对于范围内的 i(len(Data)):尝试:如果数据 [i, 4] <下屏障:对于范围内(i + 1,i + 宽度):如果数据 [a, 4] >下屏障:对于范围内的 r(a + 1, a + 宽度):如果数据 [r, 4] 下屏障:数据[s + 1, 5] = 1休息别的:继续别的:继续别的:继续别的:继续除了索引错误:经过# 看跌背离对于范围内的 i(len(Data)):尝试:如果数据[i, 4] >上屏障:对于范围内(i + 1,i + 宽度):如果数据 [a, 4] <上屏障:对于范围内的 r(a + 1, a + 宽度):如果数据[r,4]>upper_barrier 和 \数据[r,4]<数据[i,4]和数据[r,3]>数据[i, 3]:对于范围内的 s(r + 1, r + 宽度):如果数据 [s, 4] <上屏障:数据[s + 1, 6] = -1休息别的:继续别的:继续别的:继续别的:继续除了索引错误:经过
I was wondering is there any Python library that covers RSI-Divergence
(difference between a fast and a slow RSI
) or any guidence about how can I implement its algorithm in Python.
Already asked question: Programmatically detect RSI divergence. One of the answer suggests quantconnect forum for the Python version but it does not cover anything.
I was not able to find its mathematical formula but I was able to find the RSI-Divergence in pine-script, as below, but I was not able to convert it into Python since its not possible to debug pine-script
using tradingview.
study(title="RSI Divergence", shorttitle="RSI Divergence") src_fast = close, len_fast = input(5, minval=1, title="Length Fast RSI") src_slow = close, len_slow = input(14,minval=1, title="Length Slow RSI") 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 > 0 ? lime:red, linewidth = 2) band = hline(0)
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
这篇关于如何在 Python 中实现 RSI Divergence的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!