Python中RSI背离检测代码在15分钟时间间隔下出现KeyError的解决求助
Python中RSI背离检测代码在15分钟时间间隔下出现KeyError的解决求助
我正在开发一个用于检测RSI背离的程序,遇到了一个奇怪的问题:当时间间隔设为60分钟时,程序能正常运行;但改成15分钟后,程序就会崩溃,抛出KeyError: 11的错误。以下是我的代码和完整报错信息,希望能得到大家的帮助,找出问题所在。
数据获取与RSI计算代码
import pandas as pd import numpy as np import matplotlib.pyplot as plt import yfinance as yf import ta ticker=yf.Ticker("ADA-USD") data=ticker.history(period="10d",interval='15m') data.rename(columns={"Close":"close","High":"high","Low":"low","Open":"open"},inplace=True) data.index=range(1,len(data)+1) rsi_peroid=14 data["rsi"+str(rsi_peroid)]=ta.momentum.RSIIndicator(data["close"],rsi_peroid).rsi() data.dropna(inplace=True)
背离检测逻辑代码
divergence_point=[] rsi_val=data["rsi14"] loop=10 for i in range(loop,len(data)-1): recent_rsi=rsi_val[i-loop:i] recent_price=data["close"].iloc[i-loop:i] if (data["close"].iloc[i] < min(recent_price) and rsi_val[i] >= min((recent_rsi)+2) or data["close"].iloc[i] > max(recent_price) and rsi_val[i]<= max(recent_rsi)-2): divergence_point.append(i) data["Divergence"]=False data.loc[divergence_point,"Divergence"]=True
完整报错信息
KeyError Traceback (most recent call last) File C:\ProgramData\anaconda3\Lib\site-packages\pandas\core\indexes\base.py:3653, in Index.get_loc(self, key) 3652 try: -> 3653 return self._engine.get_loc(casted_key) 3654 except KeyError as err: File C:\ProgramData\anaconda3\Lib\site-packages\pandas\_libs\index.pyx:147, in pandas._libs.index.IndexEngine.get_loc() File C:\ProgramData\anaconda3\Lib\site-packages\pandas\_libs\index.pyx:176, in pandas._libs.index.IndexEngine.get_loc() File pandas\_libs\hashtable_class_helper.pxi:2606, in pandas._libs.hashtable.Int64HashTable.get_item() File pandas\_libs\hashtable_class_helper.pxi:2630, in pandas._libs.hashtable.Int64HashTable.get_item() KeyError: 11 The above exception was the direct cause of the following exception: KeyError Traceback (most recent call last) Cell In[264], line 7 5 recent_rsi=rsi_val[i-loop:i] 6 recent_price=data["close"].iloc[i-loop:i] ----> 7 if (data["close"].iloc[i] < min(recent_price) and rsi_val[i] >= min((recent_rsi)+2) or data["close"].iloc[i] > max(recent_price) and rsi_val[i]<= max(recent_rsi)-2): 8 divergence_point.append(i) 10 data["Divergence"]=False File C:\ProgramData\anaconda3\Lib\site-packages\pandas\core\series.py:1007, in Series.__getitem__(self, key) 1004 return self._values[key] 1006 elif key_is_scalar: -> 1007 return self._get_value(key) 1009 if is_hashable(key): 1010 # Otherwise index.get_value will raise InvalidIndexError 1011 try: 1012 # For labels that don't resolve as scalars like tuples and frozensets File C:\ProgramData\anaconda3\Lib\site-packages\pandas\core\series.py:1116, in Series._get_value(self, label, takeable) 1113 return self._values[label] 1115 # Similar to Index.get_value, but we do not fall back to positional -> 1116 loc = self.index.get_loc(label) 1118 if is_integer(loc): 1119 return self._values[loc] File C:\ProgramData\anaconda3\Lib\site-packages\pandas\core\indexes\base.py:3655, in Index.get_loc(self, key) 3653 return self._engine.get_loc(casted_key) 3654 except KeyError as err: -> 3655 raise KeyError(key) from err 3656 except TypeError: 3657 # If we have a listlike key, _check_indexing_error will raise 3658 # InvalidIndexError. Otherwise we fall through and re-raise 3659 # the TypeError. 3660 self._check_indexing_error(key) KeyError: 11
再次说明下情况:当时间间隔设为60分钟时程序完全正常,但切换到15分钟间隔就会触发这个错误,我实在搞不懂哪里出问题了,求各位大佬指点!
备注:内容来源于stack exchange,提问作者Hossein MEY
相关产品推荐
相关产品推荐

