3.2 分析师推荐 • 分析师的金手指?

在我们的观点中,分析师对股票的评级以及EPS的估计,更多的是对该之股票过去一段时间表现的总结,并没有明确的预测未来的能力。鉴于分析师估计的延迟特点,在我们的策略中我们将分析师估计作为反向指标使用。粗略的说,在固定的期限内,我们买入分析师调低预期的股票,卖出分析师调高预期的股票。

本策略的参数如下:

  • 起始日期: 2011年1月1日

  • 结束日期: 2015年3月19日

  • 股票池: 沪深300

  • 业绩基准: 沪深300

  • 起始资金: 100000元

  • 调仓周期: 3个月

本策略使用的主要数据API有:

这里我们使用了来自于第三方朝阳永续的数据API(需要在数据商城中购买)

  • CGRDReportGGGet 获取朝阳永续分析师一致评级

  • CESTReportGGGet 获取朝阳永续分析师一致预期

朝阳永续分析师分析数据相关链接

  1. import pandas as pd
  2. start = datetime(2011,1, 1) # 回测起始时间
  3. end = datetime(2015, 3, 19) # 回测结束时间
  4. benchmark = 'HS300' # 策略参考标准
  5. universe = set_universe('HS300') # 股票池
  6. #universe = ['600000.XSHG', '000001.XSHE']
  7. capital_base = 100000 # 起始资金
  8. commission = Commission(0.0,0.0)
  9. longest_history = 1
  10. def CGRDwithBatch(universe, batch, startDate, endDate):
  11. res = pd.DataFrame()
  12. totalLength = len(universe)
  13. count = 0
  14. while totalLength > batch:
  15. tmp = DataAPI.GG.CGRDReportGGGet(secID = universe[count * batch : (count + 1) * batch], BeginPubDate = startDate, EndPubDate = endDate)
  16. count += 1
  17. totalLength -= batch
  18. res = res.append(tmp)
  19. tmp = DataAPI.GG.CGRDReportGGGet(secID = universe[(count * batch):], BeginPubDate = startDate, EndPubDate = endDate)
  20. res = res.append(tmp)
  21. return res
  22. def CESTwithBatch(universe, batch, startDate, endDate):
  23. res = pd.DataFrame()
  24. totalLength = len(universe)
  25. count = 0
  26. while totalLength > batch:
  27. tmp = DataAPI.GG.CESTReportGGGet(secID = universe[count * batch : (count + 1) * batch], BeginPubDate = startDate, EndPubDate = endDate)
  28. count += 1
  29. totalLength -= batch
  30. res = res.append(tmp)
  31. tmp = DataAPI.GG.CGRDReportGGGet(secID = universe[(count * batch):], BeginPubDate = startDate, EndPubDate = endDate)
  32. res = res.append(tmp)
  33. return res
  34. def MktEqudwithBatch(universe, batch, startDate, endDate):
  35. res = pd.DataFrame()
  36. totalLength = len(universe)
  37. count = 0
  38. while totalLength > batch:
  39. tmp = DataAPI.MktEqudGet(secID = universe[count * batch : (count + 1) * batch], beginDate = startDate, endDate = endDate)
  40. count += 1
  41. totalLength -= batch
  42. res = res.append(tmp)
  43. tmp = DataAPI.MktEqudGet(secID = universe[count * batch : (count + 1) * batch], beginDate = startDate, endDate = endDate)
  44. res = res.append(tmp)
  45. return res
  46. def regressionTesting(universe, startDate, endDate):
  47. import statsmodels.api as sm
  48. res1 = CGRDwithBatch(universe, 50, startDate, endDate).sort('publishDate')
  49. res2 = CESTwithBatch(universe, 50, startDate, endDate).sort('publishDate')
  50. res1 = res1[res1.RatingType == 1]
  51. res2 = res2[res2.PnetprofitType == 1]
  52. # got expRating change
  53. lastRating = res1.groupby('secID').last()
  54. firstRating = res1.groupby('secID').first()
  55. lastRating['previousRating'] = firstRating.Rating
  56. lastRating['chg_exp'] = lastRating.Rating / firstRating.Rating - 1.0
  57. lowerP = lastRating['chg_exp'].quantile(0.05)
  58. highP = lastRating['chg_exp'].quantile(0.95)
  59. lastRating = lastRating[(lastRating['chg_exp']>lowerP) & (lastRating['chg_exp']<highP)]
  60. lastRating['chg_exp'] = (lastRating.chg_exp - lastRating.chg_exp.mean())/lastRating.chg_exp.std()
  61. expRating = lastRating[['secShortName', 'publishDate', 'Rating', 'previousRating', 'chg_exp']]
  62. # got expEps change
  63. lastEps = res2.groupby('secID').last()
  64. firstEps = res2.groupby('secID').first()
  65. lastEps['previousEps'] = firstEps.EPS_con
  66. lastEps['chg_eps'] = lastEps.EPS_con / firstEps.EPS_con - 1.0
  67. lowerP = lastEps['chg_eps'].quantile(0.05)
  68. highP = lastEps['chg_eps'].quantile(0.95)
  69. lastEps = lastEps[(lastEps['chg_eps']>lowerP) & (lastEps['chg_eps']<highP)]
  70. lastEps['chg_eps'] = (lastEps.chg_eps - lastEps.chg_eps.mean())/lastEps.chg_eps.std()
  71. expEps = lastEps[['secShortName', 'publishDate', 'EPS_con', 'previousEps', 'chg_eps']]
  72. # Weighted Average Ranking
  73. rankRes = expEps.copy()
  74. rankRes['chg_exp'] = expRating.chg_exp
  75. rankRes['ranking'] = expEps.chg_eps + expRating.chg_exp
  76. # Current period return
  77. mktDate = MktEqudwithBatch(universe, 50, startDate, endDate)
  78. group = mktDate.groupby('secID')
  79. returnRes = group.last().closePrice / group.first().closePrice - 1.0
  80. rankRes['currentReturn'] = (returnRes - returnRes.mean()) / returnRes.std()
  81. rankRes.dropna(inplace=True)
  82. # Do linear regression for current return
  83. x = rankRes[['chg_eps','chg_exp']].values
  84. y = rankRes.currentReturn.values
  85. x = sm.add_constant(x)
  86. model = sm.OLS(y, x)
  87. results = model.fit()
  88. rankRes['resid'] = results.resid
  89. return rankRes
  90. def initialize(account): # 初始化虚拟账户状态
  91. account.traded = False
  92. account.universe = universe
  93. account.tradingMonth = set([1,4,7,10])
  94. account.currentTradedMonth = 0
  95. account.previousRatingExp = None
  96. account.previousEpsExp = None
  97. account.holdings = set()
  98. account.first = True
  99. account.chosen = 0.05
  100. def handle_data(account): # 每个交易日的买入卖出指令
  101. today = Date(account.current_date.year, account.current_date.month, account.current_date.day)
  102. if today.month() in account.tradingMonth and not account.traded:
  103. hist = account.get_history(1)
  104. account.traded = True
  105. account.currentTradedMonth = today.month()
  106. endDate = today
  107. startDate = endDate - '3m'
  108. endStr = ''.join(endDate.toISO().split('-'))
  109. startStr = ''.join(startDate.toISO().split('-'))
  110. res = regressionTesting(account.universe, startStr, endStr)
  111. chosenNumber = int(account.chosen * len(res))
  112. secids = res.sort('resid')[:chosenNumber].index.values
  113. print today.toISO() + ' ' + str(chosenNumber) + u' 股票被选择:' + str(secids)
  114. # clean current position
  115. c = account.cash
  116. for s in account.holdings:
  117. c += hist[s]['closePrice'][-1] * account.secpos.get(s, 0)
  118. order_to(s, 0)
  119. equalAmount = c / chosenNumber
  120. # order equal amount
  121. for s in secids:
  122. approximationAmount = int(equalAmount / hist[s]['closePrice'][-1])
  123. order(s, approximationAmount)
  124. account.holdings = secids
  125. if today.month() != account.currentTradedMonth:
  126. account.traded = False

!{}(img/20160730104832.jpg)

  1. 2011-01-05 8 股票被选择:['002252.XSHE' '000338.XSHE' '600031.XSHG' '600741.XSHG' '002024.XSHE'
  2. '000869.XSHE' '600027.XSHG' '600588.XSHG']
  3. 2011-04-01 9 股票被选择:['600406.XSHG' '300024.XSHE' '002081.XSHE' '000776.XSHE' '002310.XSHE'
  4. '002375.XSHE' '601933.XSHG' '600570.XSHG' '002065.XSHE']
  5. 2011-07-01 9 股票被选择:['600873.XSHG' '600415.XSHG' '002344.XSHE' '002400.XSHE' '300133.XSHE'
  6. '002415.XSHE' '601166.XSHG' '002422.XSHE' '600887.XSHG']
  7. 2011-10-10 8 股票被选择:['600085.XSHG' '000598.XSHE' '002594.XSHE' '000157.XSHE' '600999.XSHG'
  8. '600208.XSHG' '600252.XSHG' '600585.XSHG']
  9. 2012-01-04 9 股票被选择:['600516.XSHG' '601901.XSHG' '600348.XSHG' '600395.XSHG' '601928.XSHG'
  10. '600352.XSHG' '600827.XSHG' '000629.XSHE' '600547.XSHG']
  11. 2012-04-05 9 股票被选择:['601929.XSHG' '300146.XSHE' '002450.XSHE' '300133.XSHE' '002603.XSHE'
  12. '600050.XSHG' '600252.XSHG' '601800.XSHG' '600267.XSHG']
  13. 2012-07-02 9 股票被选择:['002230.XSHE' '600143.XSHG' '002310.XSHE' '000729.XSHE' '600157.XSHG'
  14. '601258.XSHG' '600170.XSHG' '300133.XSHE' '002385.XSHE']
  15. 2012-10-08 9 股票被选择:['000869.XSHE' '002146.XSHE' '000338.XSHE' '601169.XSHG' '601336.XSHG'
  16. '000729.XSHE' '600031.XSHG' '002594.XSHE' '600115.XSHG']
  17. 2013-01-04 9 股票被选择:['002007.XSHE' '002065.XSHE' '601928.XSHG' '000858.XSHE' '600633.XSHG'
  18. '600519.XSHG' '600406.XSHG' '002603.XSHE' '603000.XSHG']
  19. 2013-04-01 9 股票被选择:['600809.XSHG' '000568.XSHE' '000060.XSHE' '000069.XSHE' '600549.XSHG'
  20. '000858.XSHE' '601377.XSHG' '002653.XSHE' '000338.XSHE']
  21. 2013-07-01 9 股票被选择:['600157.XSHG' '002475.XSHE' '000001.XSHE' '600886.XSHG' '002344.XSHE'
  22. '600028.XSHG' '600535.XSHG' '002429.XSHE' '600188.XSHG']
  23. 2013-10-08 9 股票被选择:['600372.XSHG' '600010.XSHG' '002146.XSHE' '002051.XSHE' '000999.XSHE'
  24. '600519.XSHG' '600518.XSHG' '000024.XSHE' '601117.XSHG']
  25. 2014-01-02 8 股票被选择:['300251.XSHE' '600880.XSHG' '600633.XSHG' '601928.XSHG' '002416.XSHE'
  26. '600637.XSHG' '600332.XSHG' '300058.XSHE']
  27. 2014-04-01 8 股票被选择:['002344.XSHE' '600880.XSHG' '002385.XSHE' '002310.XSHE' '600597.XSHG'
  28. '600315.XSHG' '600188.XSHG' '002415.XSHE']
  29. 2014-07-01 8 股票被选择:['300146.XSHE' '000413.XSHE' '002065.XSHE' '002456.XSHE' '300058.XSHE'
  30. '600633.XSHG' '000024.XSHE' '000400.XSHE']
  31. 2014-10-08 7 股票被选择:['600887.XSHG' '600863.XSHG' '300017.XSHE' '002292.XSHE' '002594.XSHE'
  32. '601169.XSHG' '000400.XSHE']
  33. 2015-01-05 8 股票被选择:['600880.XSHG' '002653.XSHE' '300017.XSHE' '603000.XSHG' '002456.XSHE'
  34. '002292.XSHE' '000963.XSHE' '300133.XSHE']