CMO策略模仿练习2

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

  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])

CMO策略模仿练习2 - 图1