来源:https://uqer.io/community/share/55b4c44ef9f06c91f818c5ea
import numpy as np
start='2010-01-01'
end='2015-06-20'
benchmark='sh50'
universe=set_universe('SH50')
capital_base=1000000
window=35 # 参数,CMO指标计算周期
def initialize(account):
pass
def handle_data(account):
clp=account.get_attribute_history("closeprice",window)
prc=account.get_attribute_history("precloseprice",window)
p=account.referenceprice
# 计算CMO
CMO= {}
for s in account.universe:
diff=clp(s)-prc(s)
u=sum(n for n in diff if n>0)
d=sum(-n for n in diff if n<0)
if u+d==0: continue
CMO[s]=(u-d)/(u+d)*100
# 根据CMO卖出目前持有股票
v=account.cash
for s,a in account.valid_secpos.items():
if cmo.get(s,0)<0 and s in account.universe:
order_to(s,0)
v+=a*p[s]
# 根据CMO确定买入列表
buylist= []
for s in account_universe:
if cmo.get(s,0)<0 and not np.isnan(p[s]) and s not in account.valid_secpos:
buylist.append(s)
if v > account.referencePortfolioValue * 0.33: # 为了避免调仓过于频繁,仅当可用现金超过账户市值1/3时买入
for s in buylist:
order(s, v/len(buylist)/ p[s])
---------------------------------------------------------------------------
ValueError Traceback (most recent call last)
<mercury-input-8-189e24327e9d> in <module>()
54 slippage = slippage,
55 refresh_rate = refresh_rate,
---> 56 freq = freq)
57 perf = quartz.perf_parse(bt, quartz_acct)
58 perf_temp = {}
python2.7/site-packages/quartz/backtest.py in backtest_generator(start, end, benchmark, universe, capital_base, initialize, handle_data, csvs, security_base, commission, slippage, refresh_rate, freq, *args, **kwargs)
279 sim_params = env.SimulationParameters(start, end, benchmark, universe, capital_base, security_base, csvs)
280
--> 281 idxmap_all, data_all = data_generator.get_daily_data(sim_params)
282 data_gen = data_generator.get_daily_data_generator(data_all)
283 account = env.Account(sim_params, strg, idxmap_all, data_all, commission, slippage)
python2.7/site-packages/quartz/sim_condition/data_generator.py in get_daily_data(sim_params, fq)
26 trading_days = sim_params.trading_days
27 idxmap_date = dict(zip(trading_days, range(len(trading_days))))
---> 28 idxmap_bm, data_bm = load_benchmark_data(sim_params.benchmark, trading_days)
29
30 stocks, funds = univ_divide(sim_params.universe)
python2.7/site-packages/quartz/data/benchmarks.py in load_benchmark_data(symbol, trading_days)
135 data.append(line)
136 else:
--> 137 raise ValueError("Please verify your benchmark ID!")
138
139 # 数据缺失
ValueError: Please verify your benchmark ID!