# CMO 策略模仿练习 1

``````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确定买入列表
for s in account_universe:
if cmo.get(s,0)<0 and not np.isnan(p[s]) and s not in account.valid_secpos:
if v > account.referencePortfolioValue * 0.33: # 为了避免调仓过于频繁，仅当可用现金超过账户市值1/3时买入

---------------------------------------------------------------------------
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)
29
30     stocks, funds = univ_divide(sim_params.universe)