是否根据TradingView计算RSI指标? [英] Calculate RSI indicator according to tradingview?
本文介绍了是否根据TradingView计算RSI指标?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我要根据TradingView图表计算RSI 14。
根据There Wiki的说法,这应该是解决方案: https://www.tradingview.com/wiki/Talk:Relative_Strength_Index_(RSI)
我在一个名为RSI的对象中实现了这一点: 在对象RSI内调用: self.df['rsi1'] = self.calculate_RSI_method_1(self.df, period=self.period)
实现代码的计算:
def calculate_RSI_method_1(self, ohlc: pd.DataFrame, period: int = 14) -> pd.Series:
delta = ohlc["close"].diff()
ohlc['up'] = delta.copy()
ohlc['down'] = delta.copy()
ohlc['up'] = pd.to_numeric(ohlc['up'])
ohlc['down'] = pd.to_numeric(ohlc['down'])
ohlc['up'][ohlc['up'] < 0] = 0
ohlc['down'][ohlc['down'] > 0] = 0
# This one below is not correct, but why?
ohlc['_gain'] = ohlc['up'].ewm(com=(period - 1), min_periods=period).mean()
ohlc['_loss'] = ohlc['down'].abs().ewm(com=(period - 1), min_periods=period).mean()
ohlc['RS`'] = ohlc['_gain']/ohlc['_loss']
ohlc['rsi'] = pd.Series(100 - (100 / (1 + ohlc['RS`'])))
self.currentvalue = round(self.df['rsi'].iloc[-1], 8)
print (self.currentvalue)
self.exportspreadsheetfordebugging(ohlc, 'calculate_RSI_method_1', self.symbol)
我测试了其他几种解决方案,例如,但不返回完好值:
https://github.com/peerchemist/finta https://gist.github.com/jmoz/1f93b264650376131ed65875782df386
因此,我创建了一个基于以下条件的单元测试: https://school.stockcharts.com/doku.php?id=technical_indicators:relative_strength_index_rsi 我创建了一个输入文件:(见下图) 和输出文件:(见下面的EXCEL图片)运行单元测试(此处不包括单元测试代码)应该会导致检查最后一个值,但只检查最后一个值。
if result == 37.77295211:
log.info("Unit test 001 - PASSED")
return True
else:
log.error("Unit test 001 - NOT PASSED")
return False
但我还是不能通过考试。 我在EXCEL的帮助下检查了所有数值。
所以现在我有点迷路了。
如果我在回答这个问题: Calculate RSI indicator from pandas DataFrame? 但这不会给收益带来任何价值。
- a)如何计算才能对齐单位测试?
- b)如何计算才能与TradingView保持一致?
推荐答案
以下是TradingView中当前RSI指标版本的一个实现:
https://github.com/lukaszbinden/rsi_tradingview/blob/main/rsi.py
这篇关于是否根据TradingView计算RSI指标?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文