从克隆自聚宽文章:https://www.joinquant.com/post/20635帖子里面看的思路,选出一个小列表,读取最近两条股东户数信息,用最近1条除倒数第二条,按照从小到大排序;感觉错了很多地方,上年纪了0基础,调参调够了,尝试自己写个小函数到处是错……
函数:
def sort_by_holder_reduce(context,stocklists):
stocklists=[]
g.shareholder_table=[]
for stock in stocklists:
df=finance.run_query(query(finance.STK_HOLDER_NUM).filter(finance.STK_HOLDER_NUM.code==code).limit(2))
shareholder_ratio=df.ix[1,'a_share_holders']/df.ix[0,'a_share_holders']
g.shareholder_table.append({'code':stock,'shareholder_ratio':shareholder_ratio})
shareholder=pd.DataFrame(g.shareholder_table)
#对shareholder_ratio列进行降序排序,
shareholder=shareholder.sort_values('shareholder_ratio',ascending=False)
#选取股东户数减少最多的30名
buylist=shareholder['code'].head(30)
#以列表的形式返回结果
return list(buylist)
报错:
Traceback (most recent call last):
File "/tmp/jqcore/jqboson/jqboson/core/entry.py", line 368, in _run
engine.start()
File "/tmp/jqcore/jqboson/jqboson/core/engine.py", line 232, in start
self._dispatcher.start()
File "/tmp/jqcore/jqboson/jqboson/core/dispatcher.py", line 273, in start
self._run_loop()
File "/tmp/jqcore/jqboson/jqboson/core/dispatcher.py", line 240, in _run_loop
self._loop.run()
File "/tmp/jqcore/jqboson/jqboson/core/loop/loop.py", line 84, in run
self._handle_queue()
File "/tmp/jqcore/jqboson/jqboson/core/loop/loop.py", line 124, in _handle_queue
message.callback(message.callback_data)
File "/tmp/jqcore/jqboson/jqboson/core/dispatcher.py", line 110, in callback
self._event_bus.emit(evt)
File "/tmp/jqcore/jqboson/jqboson/core/bus.py", line 47, in emit
ret.append(call(event))
File "/tmp/jqcore/jqboson/jqboson/core/strategy.py", line 376, in _wrapper
return cb(self._context.strategy_environment.strategy_context, cb_kwargs)
File "/tmp/strategy/user_code.py", line 92, in before_market_open
g.buylist=sort_by_holder_reduce(context,filter1)
File "/tmp/strategy/user_code.py", line 116, in sort_by_holder_reduce
shareholder=shareholder.sort_values('shareholder_ratio',ascending=False)
File "pandas/core/frame.py", line 4421, in sort_values
stacklevel=stacklevel)
File "pandas/core/generic.py", line 1382, in _get_label_or_level_values
raise KeyError(key)
KeyError: 'shareholder_ratio'