KeyError: 'close'
from gm.api import *
import talib
import numpy as np
set_token("9e02621e7fcf850731def2e8a9de1fdbd5b21ad6")
data = history_n(symbol="SZSE.399006",frequency="1d",count=100,end_time="2017-12-31",fields="close",fill_missing="last",adjust=ADJUST_PREV,df=True)
close = np.asarray(data["close"].values)
macd, signal, hist = talib.MACD(close, fastperiod=12, slowperiod=26, signalperiod=9)
macd = np.nan_to_num(macd)
print(macd)
运行后出现如下错误:
D:\anaconda\python.exe "D:\pycharm\PyCharm 2017.1\helpers\pydev\pydevd.py" --multiproc --qt-support --client 127.0.0.1 --port 60853 --file C:/Users/1701/Desktop/Python量化交易实战/源代码/C05/5-4.py
warning: Debugger speedups using cython not found. Run '"D:\anaconda\python.exe" "D:\pycharm\PyCharm 2017.1\helpers\pydev\setup_cython.py" build_ext --inplace' to build.
pydev debugger: process 7436 is connecting
Connected to pydev debugger (build 171.3780.47)
2019-06-29 09:58:06,818 WARNING [history.py:25] error code=1000, error info=错误或无效的token
2019-06-29 09:58:06,879 WARNING [history.py:25] error code=1000, error info=错误或无效的token
2019-06-29 09:58:06,940 WARNING [history.py:25] error code=1000, error info=错误或无效的token
2019-06-29 09:58:06,941 ERROR [query.py:76] 获取不到数据服务地址
Traceback (most recent call last):
File "C:\Users\1701\AppData\Roaming\Python\Python37\site-packages\gm\api\query.py", line 36, in wrapper
return func(*args, **kw)
File "C:\Users\1701\AppData\Roaming\Python\Python37\site-packages\gm\retrying.py", line 37, in wrapped_f
return Retrying(*dargs, **dkw).call(f, *args, **kw)
File "C:\Users\1701\AppData\Roaming\Python\Python37\site-packages\gm\retrying.py", line 201, in call
raise attempt.get()
File "C:\Users\1701\AppData\Roaming\Python\Python37\site-packages\gm\retrying.py", line 239, in get
six.reraise(self.value[0], self.value[1], self.value[2])
File "D:\anaconda\lib\site-packages\six.py", line 693, in reraise
raise value
File "C:\Users\1701\AppData\Roaming\Python\Python37\site-packages\gm\retrying.py", line 189, in call
attempt = Attempt(fn(*args, **kwargs), attempt_number, False)
File "C:\Users\1701\AppData\Roaming\Python\Python37\site-packages\gm\api\query.py", line 384, in history_n
df=df)
File "C:\Users\1701\AppData\Roaming\Python\Python37\site-packages\gm\model\history.py", line 120, in get_history_n_bars
self._init_addr()
File "C:\Users\1701\AppData\Roaming\Python\Python37\site-packages\gm\model\history.py", line 27, in _init_addr
raise EnvironmentError("获取不到数据服务地址")
OSError: 获取不到数据服务地址
Traceback (most recent call last):
File "D:\anaconda\lib\site-packages\pandas\core\indexes\base.py", line 2657, in get_loc
return self._engine.get_loc(key)
File "pandas/_libs/index.pyx", line 108, in pandas._libs.index.IndexEngine.get_loc
File "pandas/_libs/index.pyx", line 132, in pandas._libs.index.IndexEngine.get_loc
File "pandas/_libs/hashtable_class_helper.pxi", line 1601, in pandas._libs.hashtable.PyObjectHashTable.get_item
File "pandas/_libs/hashtable_class_helper.pxi", line 1608, in pandas._libs.hashtable.PyObjectHashTable.get_item
KeyError: 'close'
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "D:\pycharm\PyCharm 2017.1\helpers\pydev\pydevd.py", line 1578, in <module>
globals = debugger.run(setup['file'], None, None, is_module)
File "D:\pycharm\PyCharm 2017.1\helpers\pydev\pydevd.py", line 1015, in run
pydev_imports.execfile(file, globals, locals) # execute the script
File "D:\pycharm\PyCharm 2017.1\helpers\pydev\_pydev_imps\_pydev_execfile.py", line 18, in execfile
exec(compile(contents+"\n", file, 'exec'), glob, loc)
File "C:/Users/1701/Desktop/Python量化交易实战/源代码/C05/5-4.py", line 7, in <module>
close = np.asarray(data["close"].values)
File "D:\anaconda\lib\site-packages\pandas\core\frame.py", line 2927, in __getitem__
indexer = self.columns.get_loc(key)
File "D:\anaconda\lib\site-packages\pandas\core\indexes\base.py", line 2659, in get_loc
return self._engine.get_loc(self._maybe_cast_indexer(key))
File "pandas/_libs/index.pyx", line 108, in pandas._libs.index.IndexEngine.get_loc
File "pandas/_libs/index.pyx", line 132, in pandas._libs.index.IndexEngine.get_loc
File "pandas/_libs/hashtable_class_helper.pxi", line 1601, in pandas._libs.hashtable.PyObjectHashTable.get_item
File "pandas/_libs/hashtable_class_helper.pxi", line 1608, in pandas._libs.hashtable.PyObjectHashTable.get_item
KeyError: 'close'
刚开始出现这种情况的时候以为是Ta-Lib库没有安装好,装了好几次,怎么都运行不出来。最后找到原因如下:
是Set_tokan没有获取到实时数据。