CMO 策略模仿练习 1

来源:https://uqer.io/community/share/55b4c44ef9f06c91f818c5ea

  1. import numpy as np
  2. start='2010-01-01'
  3. end='2015-06-20'
  4. benchmark='sh50'
  5. universe=set_universe('SH50')
  6. capital_base=1000000
  7. window=35 # 参数,CMO指标计算周期
  8. def initialize(account):
  9. pass
  10. def handle_data(account):
  11. clp=account.get_attribute_history("closeprice",window)
  12. prc=account.get_attribute_history("precloseprice",window)
  13. p=account.referenceprice
  14. # 计算CMO
  15. CMO= {}
  16. for s in account.universe:
  17. diff=clp(s)-prc(s)
  18. u=sum(n for n in diff if n>0)
  19. d=sum(-n for n in diff if n<0)
  20. if u+d==0: continue
  21. CMO[s]=(u-d)/(u+d)*100
  22. # 根据CMO卖出目前持有股票
  23. v=account.cash
  24. for s,a in account.valid_secpos.items():
  25. if cmo.get(s,0)<0 and s in account.universe:
  26. order_to(s,0)
  27. v+=a*p[s]
  28. # 根据CMO确定买入列表
  29. buylist= []
  30. for s in account_universe:
  31. if cmo.get(s,0)<0 and not np.isnan(p[s]) and s not in account.valid_secpos:
  32. buylist.append(s)
  33. if v > account.referencePortfolioValue * 0.33: # 为了避免调仓过于频繁,仅当可用现金超过账户市值1/3时买入
  34. for s in buylist:
  35. order(s, v/len(buylist)/ p[s])
  36. ---------------------------------------------------------------------------
  37. ValueError Traceback (most recent call last)
  38. <mercury-input-8-189e24327e9d> in <module>()
  39. 54 slippage = slippage,
  40. 55 refresh_rate = refresh_rate,
  41. ---> 56 freq = freq)
  42. 57 perf = quartz.perf_parse(bt, quartz_acct)
  43. 58 perf_temp = {}
  44. 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)
  45. 279 sim_params = env.SimulationParameters(start, end, benchmark, universe, capital_base, security_base, csvs)
  46. 280
  47. --> 281 idxmap_all, data_all = data_generator.get_daily_data(sim_params)
  48. 282 data_gen = data_generator.get_daily_data_generator(data_all)
  49. 283 account = env.Account(sim_params, strg, idxmap_all, data_all, commission, slippage)
  50. python2.7/site-packages/quartz/sim_condition/data_generator.py in get_daily_data(sim_params, fq)
  51. 26 trading_days = sim_params.trading_days
  52. 27 idxmap_date = dict(zip(trading_days, range(len(trading_days))))
  53. ---> 28 idxmap_bm, data_bm = load_benchmark_data(sim_params.benchmark, trading_days)
  54. 29
  55. 30 stocks, funds = univ_divide(sim_params.universe)
  56. python2.7/site-packages/quartz/data/benchmarks.py in load_benchmark_data(symbol, trading_days)
  57. 135 data.append(line)
  58. 136 else:
  59. --> 137 raise ValueError("Please verify your benchmark ID!")
  60. 138
  61. 139 # 数据缺失
  62. ValueError: Please verify your benchmark ID!