掘金社区

为何同一个程序,2种不同的股票代码,一个通过,一个报错,是代码错误还是数据?Pinned highlighted

小路上 发表在问题反馈 2020-01-15 00:49:21

问题反馈
436
1
0

见下面代码,两组context.pool90数据分别调试,为何有1组通过,1组显示:KeyError: 'high'。
请问是数据原因吗?某个股票代码有问题?


coding=utf-8

from future import print_function, absolute_import#, unicode_literals
from gm.api import *
import talib as talib
import pandas as pd
import numpy as np

def init(context):
#context.pool90=['SZSE.000002', 'SZSE.000004', 'SZSE.000005', 'SZSE.000007', 'SZSE.000008', 'SZSE.000009', 'SZSE.000012', 'SZSE.000021', 'SZSE.001872', 'SZSE.000023', 'SZSE.000025', 'SZSE.000026', 'SZSE.000027', 'SZSE.000028', 'SZSE.000035', 'SZSE.000037', 'SZSE.000038', 'SZSE.000039', 'SZSE.000040', 'SZSE.000042', 'SZSE.001914', 'SZSE.000045', 'SZSE.000048', 'SZSE.000049', 'SZSE.000050', 'SZSE.000056', 'SZSE.000058', 'SZSE.000060', 'SZSE.000062', 'SZSE.000063', 'SZSE.000065', 'SZSE.000066', 'SZSE.000068', 'SZSE.000070', 'SZSE.000078', 'SZSE.000088', 'SZSE.000089', 'SZSE.000090', 'SZSE.000096', 'SZSE.000099', 'SZSE.000100', 'SZSE.000150', 'SZSE.000151', 'SZSE.000153', 'SZSE.000155', 'SZSE.000156', 'SZSE.000157', 'SZSE.000158', 'SZSE.000301', 'SZSE.000338', 'SZSE.000400',]
context.pool90=['SHSE.600000', 'SHSE.600004', 'SHSE.600009', 'SHSE.600011', 'SHSE.600015', 'SHSE.600016', 'SHSE.600018', 'SHSE.600020', 'SHSE.600021', 'SHSE.600027', 'SHSE.600028', 'SHSE.600029', 'SHSE.600030', 'SHSE.600033', 'SHSE.600035', 'SHSE.600036', 'SHSE.600039', 'SHSE.600048', 'SHSE.600060', 'SHSE.600061', 'SHSE.600062', 'SHSE.600064', 'SHSE.600066', 'SHSE.600068', 'SHSE.600089', 'SHSE.600098', 'SHSE.600104', 'SHSE.600109', 'SHSE.600120', 'SHSE.600153', 'SHSE.600168', 'SHSE.600170', 'SHSE.600177', 'SHSE.600185', 'SHSE.600196', 'SHSE.600221', 'SHSE.600236', 'SHSE.600239', 'SHSE.600240', 'SHSE.600266', 'SHSE.600269', 'SHSE.600270', 'SHSE.600273', 'SHSE.600284', 'SHSE.600310', 'SHSE.600315', 'SHSE.600325', 'SHSE.600340', 'SHSE.600350', 'SHSE.600352', 'SHSE.600369']
context.pool60=[]
schedule(schedule_func=algo, date_rule='1d', time_rule='09:20:00')

def algo(context):
today1 = context.now
# 获取上一个交易日
last_day = get_previous_trading_date(exchange='SHSE', date=today1)
context.pool60=[]
pe1=get_fundamentals(table='trading_derivative_indicator', symbols=context.pool90,start_date=last_day, end_date=last_day,fields='PELFY,PETTM',filter='PELFY > 10 and PELFY < 200',df=True)
pe2=pe1['symbol'].tolist()
print('pe2=%s'%pe2)

for symbol in pe2:#
    history1=history_n(symbol=symbol, frequency='1d', count=30, fields='volume,open,symbol,close,high,low,eob',fill_missing='Last', adjust=ADJUST_PREV, end_time=last_day, df=True)             
    hi_1=history1['high'].tolist()[-1]

'''
backtest_start_time='2019-01-31 13:00:00',
backtest_end_time='2019-2-12 11:00:00',
backtest_adjust=ADJUST_PREV,
backtest_initial_cash=10000000,
backtest_commission_ratio=0.0001,
backtest_slippage_ratio=0.0001)
'''
错误显示:
Traceback (most recent call last):
File "C:\Users\Administrator\AppData\Roaming\Python\Python36\site-packages\pandas\core\indexes\base.py", line 2897, in get_loc
return self._engine.get_loc(key)
File "pandas_libs\index.pyx", line 107, in pandas._libs.index.IndexEngine.get_loc
File "pandas_libs\index.pyx", line 131, in pandas._libs.index.IndexEngine.get_loc
File "pandas_libs\hashtable_class_helper.pxi", line 1607, in pandas._libs.hashtable.PyObjectHashTable.get_item
File "pandas_libs\hashtable_class_helper.pxi", line 1614, in pandas._libs.hashtable.PyObjectHashTable.get_item
KeyError: 'high'

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
File "C:\Users\Administrator\AppData\Roaming\Python\Python36\site-packages\gm\callback.py", line 505, in callback_controller
return schedule_callback(data)
File "C:\Users\Administrator\AppData\Roaming\Python\Python36\site-packages\gm\callback.py", line 229, in schedule_callback
schedule_func(context)
File "c:\Users\Administrator.goldminer3\projects\d0759a36-3545-11ea-a7a2-bcee7b25bfb0\main.py", line 37, in algo
hi_1=history1['high'].tolist()[-1]
File "C:\Users\Administrator\AppData\Roaming\Python\Python36\site-packages\pandas\core\frame.py", line 2980, in getitem
indexer = self.columns.get_loc(key)
File "C:\Users\Administrator\AppData\Roaming\Python\Python36\site-packages\pandas\core\indexes\base.py", line 2899, in get_loc
return self._engine.get_loc(self._maybe_cast_indexer(key))
File "pandas_libs\index.pyx", line 107, in pandas._libs.index.IndexEngine.get_loc
File "pandas_libs\index.pyx", line 131, in pandas._libs.index.IndexEngine.get_loc
File "pandas_libs\hashtable_class_helper.pxi", line 1607, in pandas._libs.hashtable.PyObjectHashTable.get_item
File "pandas_libs\hashtable_class_helper.pxi", line 1614, in pandas._libs.hashtable.PyObjectHashTable.get_item
KeyError: 'high'

评论: 1

Looks like your connection to 掘金量化社区 - 量化交易者的策略交流学习社区 was lost, please wait while we try to reconnect.