ROIC&cashROIC

来源:https://uqer.io/community/share/564d30eff9f06c4446b483db

  1. from CAL.PyCAL import *
  2. import numpy as np
  3. from pandas import DataFrame , Series
  4. start = '2015-01-01' # 回测起始时间
  5. end = '2015-11-01' # 回测结束时间
  6. benchmark = 'HS300' # 策略参考标准
  7. universe = set_universe('HS300') # 证券池,支持股票和基金
  8. capital_base = 100000 # 起始资金
  9. freq = 'd' # 策略类型,'d'表示日间策略使用日线回测,'m'表示日内策略使用分钟线回测
  10. refresh_rate = 30 # 调仓频率,表示执行handle_data的时间间隔,若freq = 'd'时间间隔的单位为交易日,若freq = 'm'时间间隔为分钟
  11. cal = Calendar('China.SSE')
  12. def initialize(account): # 初始化虚拟账户状态
  13. pass
  14. def handle_data(account): # 每个交易日的买入卖出指令
  15. today = account.current_date.strftime('%Y%m%d')
  16. yesterday = cal.advanceDate(account.current_date, '-1B', BizDayConvention.Following).strftime('%Y%m%d')
  17. ROIC = {}
  18. cashROIC = {}
  19. # EBITToTOR
  20. EBITToTOR = DataAPI.MktStockFactorsOneDayGet(tradeDate=yesterday,secID=account.universe,field=u"secID,EBITToTOR",pandas="1")
  21. EBITToTOR.drop_duplicates('secID' , inplace= True)
  22. EBITToTOR.set_index('secID' , inplace = True)
  23. # tRevenue : 营业收入
  24. tRevenue = DataAPI.FdmtISAllLatestGet(secID=account.universe,endDate=u"20151101",beginDate=u"20150101",field=u"secID,tRevenue",pandas="1")
  25. tRevenue.drop_duplicates('secID' , inplace= True)
  26. tRevenue.set_index('secID' , inplace = True)
  27. # 自由现金流
  28. freeCF = DataAPI.FdmtCFAllLatestGet(secID=account.universe,endDate=u"20151101",beginDate=u"20150101",field=u"secID,NCFOperateA,purFixAssetsOth,dispFixAssetsOth",pandas="1")
  29. freeCF.drop_duplicates('secID' , inplace= True)
  30. freeCF.set_index('secID' , inplace= True)
  31. # IC : 投入资本
  32. IC = DataAPI.FdmtBSAllLatestGet(secID=account.universe,endDate=u"20151101",beginDate=u"20150101",field=u"secID,TShEquity,LTBorr",pandas="1")
  33. IC.drop_duplicates('secID' , inplace= True)
  34. IC.set_index('secID' , inplace = True)
  35. for s in account.universe:
  36. ROIC[s] = (EBITToTOR['EBITToTOR'][s] * tRevenue['tRevenue'][s] * (1-0.25)) / (IC['TShEquity'][s] + IC['LTBorr'][s])
  37. cashROIC[s] = (freeCF['NCFOperateA'][s] - (freeCF['purFixAssetsOth'][s] - freeCF['dispFixAssetsOth'][s])) / (IC['TShEquity'][s] + IC['LTBorr'][s])
  38. # ROIC
  39. ROIC = Series(ROIC)
  40. ROIC.sort(ascending = False)
  41. ROIC.dropna(inplace = True)
  42. ROIC = ROIC[0:60]
  43. buylist1 = list(ROIC.index)
  44. # cashROIC
  45. cashROIC = Series(cashROIC)
  46. cashROIC.sort(ascending = False)
  47. cashROIC.dropna(inplace = True)
  48. cashROIC = cashROIC[0:60]
  49. buylist2 = list(cashROIC.index)
  50. # buylist为buylist1与buylist2的交集
  51. buylist = list(set(buylist1)&set(buylist2))
  52. print 'buylist', len(buylist)
  53. print buylist
  54. sell_list = [x for x in account.valid_secpos if x not in buylist]
  55. for s in sell_list :
  56. order_to(s,0)
  57. total_money = account.referencePortfolioValue
  58. for s in buylist:
  59. if s in account.valid_secpos:
  60. pass
  61. else:
  62. order(s,total_money/(len(buylist)-len(set(buylist)&set(account.valid_secpos)))/account.referencePrice[s])

ROIC&cashROIC - 图1

  1. buylist 24
  2. ['601601.XSHG', '601899.XSHG', '000895.XSHE', '000539.XSHE', '600276.XSHG', '600011.XSHG', '600887.XSHG', '000712.XSHE', '600863.XSHG', '600018.XSHG', '000538.XSHE', '600900.XSHG', '002470.XSHE', '600600.XSHG', '600153.XSHG', '002304.XSHE', '600104.XSHG', '000333.XSHE', '600873.XSHG', '600578.XSHG', '000999.XSHE', '002294.XSHE', '601006.XSHG', '600177.XSHG']
  3. buylist 24
  4. ['600027.XSHG', '000559.XSHE', '601899.XSHG', '000895.XSHE', '000876.XSHE', '000539.XSHE', '600276.XSHG', '600011.XSHG', '601006.XSHG', '000538.XSHE', '600900.XSHG', '002470.XSHE', '600600.XSHG', '600153.XSHG', '600332.XSHG', '002304.XSHE', '600104.XSHG', '000333.XSHE', '600873.XSHG', '600578.XSHG', '000999.XSHE', '002294.XSHE', '000712.XSHE', '600177.XSHG']
  5. buylist 21
  6. ['600276.XSHG', '600900.XSHG', '600104.XSHG', '600011.XSHG', '600887.XSHG', '600873.XSHG', '600027.XSHG', '601006.XSHG', '600663.XSHG', '600018.XSHG', '000538.XSHE', '601899.XSHG', '600863.XSHG', '002470.XSHE', '002304.XSHE', '000999.XSHE', '000895.XSHE', '000876.XSHE', '600153.XSHG', '600177.XSHG', '000712.XSHE']
  7. buylist 22
  8. ['601601.XSHG', '600027.XSHG', '000876.XSHE', '600276.XSHG', '600011.XSHG', '600887.XSHG', '600863.XSHG', '000538.XSHE', '002470.XSHE', '600332.XSHG', '002304.XSHE', '601377.XSHG', '600104.XSHG', '000333.XSHE', '600873.XSHG', '601628.XSHG', '002294.XSHE', '600663.XSHG', '000712.XSHE', '600886.XSHG', '601318.XSHG', '600177.XSHG']
  9. buylist 22
  10. ['601216.XSHG', '600027.XSHG', '000895.XSHE', '000876.XSHE', '600011.XSHG', '600887.XSHG', '000538.XSHE', '002470.XSHE', '600649.XSHG', '600332.XSHG', '002304.XSHE', '601377.XSHG', '600104.XSHG', '000333.XSHE', '600873.XSHG', '600578.XSHG', '600741.XSHG', '601628.XSHG', '000999.XSHE', '600795.XSHG', '600663.XSHG', '601318.XSHG']
  11. buylist 26
  12. ['601216.XSHG', '600340.XSHG', '000876.XSHE', '600276.XSHG', '600011.XSHG', '600887.XSHG', '601006.XSHG', '600018.XSHG', '000538.XSHE', '002470.XSHE', '000069.XSHE', '600332.XSHG', '002304.XSHE', '601377.XSHG', '600104.XSHG', '000333.XSHE', '600578.XSHG', '601628.XSHG', '000999.XSHE', '002294.XSHE', '600795.XSHG', '600663.XSHG', '600863.XSHG', '600060.XSHG', '601318.XSHG', '600177.XSHG']
  13. buylist 28
  14. ['002304.XSHE', '601216.XSHG', '600027.XSHG', '601111.XSHG', '601899.XSHG', '000878.XSHE', '600018.XSHG', '000895.XSHE', '600276.XSHG', '600887.XSHG', '601006.XSHG', '601601.XSHG', '601088.XSHG', '002470.XSHE', '000069.XSHE', '600332.XSHG', '600597.XSHG', '601377.XSHG', '600873.XSHG', '600578.XSHG', '600649.XSHG', '601628.XSHG', '002294.XSHE', '600795.XSHG', '600663.XSHG', '600863.XSHG', '601318.XSHG', '600177.XSHG']