def initialize(context):
stocks =get_industry_stocks('000300.XSHG')
set_universe(stocks)
def handle_data(context, data):
# 获取取得最近日收盘价,命名为last_price
last_price = data[i].close
# 获取近二十日股票收盘价的平均价,命名为average_price
average_price = data[i].mavg(20, 'close')
# 获取当前现金数量,命名为cash
cash = context.portfolio.cash
# cash除以stocks中的股票数,得到per_cash
per_cash = cash/len(stocks)
# 如果昨日收盘价高出二十日平均价, 则买入,否则卖出。
if last_price > average_price:
order_value(i, per_cash)# 用资金买入股票
elif last_price < average_price:
order_target(i, 0)# 将股票仓位调整到0,即全卖出
----------------------------------------------------------------------
想用沪深300的股票作为股票池,然后带入这个程序,显示错误了。
结果是:行业板块 '000300.XSHG' 不存在
请问大神,这是什么错误。@JoinQuant-TWist
2018-01-27