3.2 分析师推荐 • 分析师的金手指?
在我们的观点中,分析师对股票的评级以及EPS的估计,更多的是对该之股票过去一段时间表现的总结,并没有明确的预测未来的能力。鉴于分析师估计的延迟特点,在我们的策略中我们将分析师估计作为反向指标使用。粗略的说,在固定的期限内,我们买入分析师调低预期的股票,卖出分析师调高预期的股票。
本策略的参数如下:
起始日期: 2011年1月1日
结束日期: 2015年3月19日
股票池: 沪深300
业绩基准: 沪深300
起始资金: 100000元
调仓周期: 3个月
本策略使用的主要数据API有:
这里我们使用了来自于第三方朝阳永续的数据API(需要在数据商城中购买)
CGRDReportGGGet获取朝阳永续分析师一致评级CESTReportGGGet获取朝阳永续分析师一致预期
import pandasas pdstart = datetime(2011,1,1)# 回测起始时间end = datetime(2015,3,19)# 回测结束时间benchmark ='HS300'# 策略参考标准universe = set_universe('HS300')# 股票池#universe = ['600000.XSHG', '000001.XSHE']capital_base =100000# 起始资金commission = Commission(0.0,0.0)longest_history =1defCGRDwithBatch(universe, batch, startDate, endDate): res = pd.DataFrame() totalLength = len(universe) count =0while totalLength > batch: tmp = DataAPI.GG.CGRDReportGGGet(secID = universe[count * batch : (count +1) * batch], BeginPubDate = startDate, EndPubDate = endDate) count +=1 totalLength -= batch res = res.append(tmp) tmp = DataAPI.GG.CGRDReportGGGet(secID = universe[(count * batch):], BeginPubDate = startDate, EndPubDate = endDate) res = res.append(tmp)return resdefCESTwithBatch(universe, batch, startDate, endDate): res = pd.DataFrame() totalLength = len(universe) count =0while totalLength > batch: tmp = DataAPI.GG.CESTReportGGGet(secID = universe[count * batch : (count +1) * batch], BeginPubDate = startDate, EndPubDate = endDate) count +=1 totalLength -= batch res = res.append(tmp) tmp = DataAPI.GG.CGRDReportGGGet(secID = universe[(count * batch):], BeginPubDate = startDate, EndPubDate = endDate) res = res.append(tmp)return resdefMktEqudwithBatch(universe, batch, startDate, endDate): res = pd.DataFrame() totalLength = len(universe) count =0while totalLength > batch: tmp = DataAPI.MktEqudGet(secID = universe[count * batch : (count +1) * batch], beginDate = startDate, endDate = endDate) count +=1 totalLength -= batch res = res.append(tmp) tmp = DataAPI.MktEqudGet(secID = universe[count * batch : (count +1) * batch], beginDate = startDate, endDate = endDate) res = res.append(tmp)return resdefregressionTesting(universe, startDate, endDate):import statsmodels.apias sm res1 = CGRDwithBatch(universe,50, startDate, endDate).sort('publishDate') res2 = CESTwithBatch(universe,50, startDate, endDate).sort('publishDate') res1 = res1[res1.RatingType ==1] res2 = res2[res2.PnetprofitType ==1]# got expRating change lastRating = res1.groupby('secID').last() firstRating = res1.groupby('secID').first() lastRating['previousRating'] = firstRating.Rating lastRating['chg_exp'] = lastRating.Rating / firstRating.Rating -1.0 lowerP = lastRating['chg_exp'].quantile(0.05) highP = lastRating['chg_exp'].quantile(0.95) lastRating = lastRating[(lastRating['chg_exp']>lowerP) & (lastRating['chg_exp']<highP)] lastRating['chg_exp'] = (lastRating.chg_exp - lastRating.chg_exp.mean())/lastRating.chg_exp.std() expRating = lastRating[['secShortName','publishDate','Rating','previousRating','chg_exp']]# got expEps change lastEps = res2.groupby('secID').last() firstEps = res2.groupby('secID').first() lastEps['previousEps'] = firstEps.EPS_con lastEps['chg_eps'] = lastEps.EPS_con / firstEps.EPS_con -1.0 lowerP = lastEps['chg_eps'].quantile(0.05) highP = lastEps['chg_eps'].quantile(0.95) lastEps = lastEps[(lastEps['chg_eps']>lowerP) & (lastEps['chg_eps']<highP)] lastEps['chg_eps'] = (lastEps.chg_eps - lastEps.chg_eps.mean())/lastEps.chg_eps.std() expEps = lastEps[['secShortName','publishDate','EPS_con','previousEps','chg_eps']]# Weighted Average Ranking rankRes = expEps.copy() rankRes['chg_exp'] = expRating.chg_exp rankRes['ranking'] = expEps.chg_eps + expRating.chg_exp# Current period return mktDate = MktEqudwithBatch(universe,50, startDate, endDate) group = mktDate.groupby('secID') returnRes = group.last().closePrice / group.first().closePrice -1.0 rankRes['currentReturn'] = (returnRes - returnRes.mean()) / returnRes.std() rankRes.dropna(inplace=True)# Do linear regression for current return x = rankRes[['chg_eps','chg_exp']].values y = rankRes.currentReturn.values x = sm.add_constant(x) model = sm.OLS(y, x) results = model.fit() rankRes['resid'] = results.residreturn rankResdefinitialize(account):# 初始化虚拟账户状态 account.traded =False account.universe = universe account.tradingMonth = set([1,4,7,10]) account.currentTradedMonth =0 account.previousRatingExp =None account.previousEpsExp =None account.holdings = set() account.first =True account.chosen =0.05defhandle_data(account):# 每个交易日的买入卖出指令 today = Date(account.current_date.year, account.current_date.month, account.current_date.day)if today.month()in account.tradingMonthandnot account.traded: hist = account.get_history(1) account.traded =True account.currentTradedMonth = today.month() endDate = today startDate = endDate -'3m' endStr =''.join(endDate.toISO().split('-')) startStr =''.join(startDate.toISO().split('-')) res = regressionTesting(account.universe, startStr, endStr) chosenNumber = int(account.chosen * len(res)) secids = res.sort('resid')[:chosenNumber].index.valuesprint today.toISO() +' ' + str(chosenNumber) +u' 股票被选择:' + str(secids)# clean current position c = account.cashfor sin account.holdings: c += hist[s]['closePrice'][-1] * account.secpos.get(s,0) order_to(s,0) equalAmount = c / chosenNumber# order equal amountfor sin secids: approximationAmount = int(equalAmount / hist[s]['closePrice'][-1]) order(s, approximationAmount) account.holdings = secidsif today.month() != account.currentTradedMonth: account.traded =False!{}(img/20160730104832.jpg)
2011-01-058 股票被选择:['002252.XSHE''000338.XSHE''600031.XSHG''600741.XSHG''002024.XSHE''000869.XSHE''600027.XSHG''600588.XSHG']2011-04-019 股票被选择:['600406.XSHG''300024.XSHE''002081.XSHE''000776.XSHE''002310.XSHE''002375.XSHE''601933.XSHG''600570.XSHG''002065.XSHE']2011-07-019 股票被选择:['600873.XSHG''600415.XSHG''002344.XSHE''002400.XSHE''300133.XSHE''002415.XSHE''601166.XSHG''002422.XSHE''600887.XSHG']2011-10-108 股票被选择:['600085.XSHG''000598.XSHE''002594.XSHE''000157.XSHE''600999.XSHG''600208.XSHG''600252.XSHG''600585.XSHG']2012-01-049 股票被选择:['600516.XSHG''601901.XSHG''600348.XSHG''600395.XSHG''601928.XSHG''600352.XSHG''600827.XSHG''000629.XSHE''600547.XSHG']2012-04-059 股票被选择:['601929.XSHG''300146.XSHE''002450.XSHE''300133.XSHE''002603.XSHE''600050.XSHG''600252.XSHG''601800.XSHG''600267.XSHG']2012-07-029 股票被选择:['002230.XSHE''600143.XSHG''002310.XSHE''000729.XSHE''600157.XSHG''601258.XSHG''600170.XSHG''300133.XSHE''002385.XSHE']2012-10-089 股票被选择:['000869.XSHE''002146.XSHE''000338.XSHE''601169.XSHG''601336.XSHG''000729.XSHE''600031.XSHG''002594.XSHE''600115.XSHG']2013-01-049 股票被选择:['002007.XSHE''002065.XSHE''601928.XSHG''000858.XSHE''600633.XSHG''600519.XSHG''600406.XSHG''002603.XSHE''603000.XSHG']2013-04-019 股票被选择:['600809.XSHG''000568.XSHE''000060.XSHE''000069.XSHE''600549.XSHG''000858.XSHE''601377.XSHG''002653.XSHE''000338.XSHE']2013-07-019 股票被选择:['600157.XSHG''002475.XSHE''000001.XSHE''600886.XSHG''002344.XSHE''600028.XSHG''600535.XSHG''002429.XSHE''600188.XSHG']2013-10-089 股票被选择:['600372.XSHG''600010.XSHG''002146.XSHE''002051.XSHE''000999.XSHE''600519.XSHG''600518.XSHG''000024.XSHE''601117.XSHG']2014-01-028 股票被选择:['300251.XSHE''600880.XSHG''600633.XSHG''601928.XSHG''002416.XSHE''600637.XSHG''600332.XSHG''300058.XSHE']2014-04-018 股票被选择:['002344.XSHE''600880.XSHG''002385.XSHE''002310.XSHE''600597.XSHG''600315.XSHG''600188.XSHG''002415.XSHE']2014-07-018 股票被选择:['300146.XSHE''000413.XSHE''002065.XSHE''002456.XSHE''300058.XSHE''600633.XSHG''000024.XSHE''000400.XSHE']2014-10-087 股票被选择:['600887.XSHG''600863.XSHG''300017.XSHE''002292.XSHE''002594.XSHE''601169.XSHG''000400.XSHE']2015-01-058 股票被选择:['600880.XSHG''002653.XSHE''300017.XSHE''603000.XSHG''002456.XSHE''002292.XSHE''000963.XSHE''300133.XSHE']