先上代码:
import pandas as pd
start='2014-11-01'
end='2018-01-01'
benchmark='HS300'
universe=DynamicUniverse('HS300')
refresh_rate=60 #此处未给出频率参数,默认是‘d’(天)
max_history_window=60
accounts={
'fantasy_account':AccountConfig(account_type="security",capital_base=10000000,
commission=Commission(buycost=0.001,sellcost=0.002,unit='perValue'),
slippage=Slippage(value=0.0,unit='perValue'))
}
def initialize(context):
pass
def handle_data(context):
account=context.get_account('fantasy_account')
universe=context.get_universe(exclude_halt=True)
history=context.history(universe,'closePrice',60) #获取K线图等时间序列数据(获取60条历史K线图universe证券列表的前复权收盘价)
momentum={'symbol':[],'c_ret':[]} #字典初始化
for stk in history.keys(): #对每只股票进行循环,用最近的前复权价格除以60个交易日之前的前复权价格,得到累计净值
momentum['symbol'].append(stk)
momentum['c_ret'].append(history[stk]['closePrice'][-1]/history[stk]['closePrice'][0])
#按照过去60日收益率排序,并且选择前60只得股票作为买入候选
momentum&#