目前市场大部分还在高位,只有大蓝筹便宜。
import numpy as npimport pandas as pdimport matplotlib.pyplot as pltimport bisect#指定日期的指数PE(等权重)def get_index_pe_date(index_code,date):stocks = get_index_stocks(index_code, date)q = query(valuation).filter(valuation.code.in_(stocks))df = get_fundamentals(q, date)if len(df)>0:pe = len(df)/sum([1/p if p>0 else 0 for p in df.pe_ratio])return peelse:return float('NaN')#指定日期的指数PB(等权重)def get_index_pb_date(index_code,date):stocks = get_index_stocks(index_code, date)q = query(valuation).filter(valuation.code.in_(stocks))df = get_fundamentals(q, date)if len(df)>0:pb = len(df)/sum([1/p if p>0 else 0 for p in df.pb_ratio])return pbelse:return float('NaN')#指数历史PEPBdef get_index_pe_pb(index_code):start='2005-1-1'#start='2015-1-1'end = pd.datetime.today();dates=[]pes=[]pbs=[]for d in pd.date_range(start,end,freq='M'): #频率为月dates.append(d)pes.append(get_index_pe_date(index_code,d))pbs.append(get_index_pb_date(index_code,d))d = {'PE' : pd.Series(pes, index=dates),'PB' : pd.Series(pbs, index=dates)}PB_PE = pd.DataFrame(d)return PB_PEall_index = get_all_securities(['index'])index_choose =['000016.XSHG',
'000300.XSHG', '000902.XSHG', '000905.XSHG', '399106.XSHE',
'399316.XSHE', '000036.XSHG','000037.XSHG','000038.XSHG','000039.XSHG','000158.XSHG']df_pe_pb = pd.DataFrame()frames=pd.DataFrame()today= pd.datetime.today()for code in index_choose:index_name = all_index.ix[code].display_name print u'正在处理: ',index_name df_pe_pb=get_index_pe_pb(code)
results=[]pe = get_index_pe_date(code,today)q_pes = [df_pe_pb['PE'].quantile(i/10.0) for i in range(11)] idx = bisect.bisect(q_pes,pe)quantile = idx-(q_pes[idx]-pe)/(q_pes[idx]-q_pes[idx-1]) #index_name = all_index.ix[code].display_nameresults.append([index_name,'%.2f'% pe,'%.2f'% (quantile*10)]+['%.2f'%q for q in q_pes]+[df_pe_pb['PE'].count()]) pb = get_index_pb_date(code,today)q_pbs = [df_pe_pb['PB'].quantile(i/10.0) for i in range(11)] idx = bisect.bisect(q_pbs,pb)quantile = idx-(q_pbs[idx]-pb)/(q_pbs[idx]-q_pbs[idx-1]) #index_name = all_index.ix[code].display_nameresults.append([index_name,'%.2f'% pb,'%.2f'% (quantile*10)]+['%.2f'%q for q in q_pbs]+[df_pe_pb['PB'].count()])
df_pe_pb['10% PE']=q_pes[1]df_pe_pb['50% PE']=q_pes[5]df_pe_pb['90% PE']=q_pes[9]df_pe_pb['10% PB']=q_pbs[1]df_pe_pb['50% PB']=q_pbs[5]df_pe_pb['90% PB']=q_pbs[9]df_pe_pb.plot(secondary_y=['PB','10% PB','50% PB','90% PB'],figsize=(14,8),title=index_name,style=['k-.', 'k', 'g', 'y', 'r', 'g-.', 'y-.', 'r-.']) columns=[u'名称',u'当前估值',u'分位点%',u'最小估值']+['%d%%'% (i*10) for i in range(1,10)]+[u'最大估值' , u"数据个数"]df= pd.DataFrame(data=results,index=['PE','PB'],columns=columns)frames = pd.concat([frames, df])frames
正在处理: 上证50
正在处理: 沪深300
正在处理: 中证流通
正在处理: 中证500
正在处理: 深证综指
正在处理: 巨潮小盘
正在处理: 上证消费
正在处理: 上证医药
正在处理: 上证金融
正在处理: 上证信息
正在处理: 上证环保
| 名称 | 当前估值 | 分位点% | 最小估值 | 10% | 20% | 30% | 40% | 50% | 60% | 70% | 80% | 90% | 最大估值 | 数据个数 |
---|
PE | 上证50 | 11.31 | 17.99 | 9.79 | 10.59 | 11.49 | 12.49 | 13.96 | 15.03 | 16.06 | 17.82 | 24.86 | 32.65 | 47.33 | 135 |
---|
PB | 上证50 | 1.42 | 8.44 | 1.30 | 1.44 | 1.58 | 1.70 | 1.80 | 1.93 | 2.10 | 2.30 | 2.72 | 3.75 | 6.57 | 135 |
---|
PE | 沪深300 | 20.22 | 46.28 | 12.11 | 15.89 | 16.70 | 17.46 | 18.75 | 21.08 | 22.85 | 24.50 | 32.00 | 41.05 | 54.96 | 132 |
---|
PB | 沪深300 | 2.11 | 44.44 | 1.33 | 1.53 | 1.72 | 1.86 | 2.01 | 2.24 | 2.60 | 2.81 | 3.17 | 3.92 | 6.80 | 132 |
---|
PE | 中证流通 | 52.89 | 76.55 | 9.39 | 22.45 | 27.83 | 30.66 | 32.13 | 35.68 | 40.37 | 46.46 | 56.28 | 64.36 | 77.09 | 130 |
---|
PB | 中证流通 | 3.56 | 68.25 | 1.45 | 1.84 | 2.12 | 2.26 | 2.44 | 2.95 | 3.31 | 3.62 | 3.97 | 4.40 | 5.76 | 130 |
---|
PE | 中证500 | 39.62 | 60.07 | 17.36 | 25.09 | 27.00 | 28.32 | 31.08 | 35.52 | 39.57 | 46.41 | 54.19 | 62.26 | 72.21 | 111 |
---|
PB | 中证500 | 2.85 | 50.84 | 1.32 | 1.97 | 2.08 | 2.21 | 2.53 | 2.82 | 3.13 | 3.39 | 3.59 | 4.00 | 5.03 | 111 |
---|
PE | 深证综指 | 63.97 | 86.91 | 17.47 | 28.18 | 31.32 | 34.16 | 37.67 | 40.71 | 47.04 | 51.87 | 58.75 | 66.31 | 92.41 | 135 |
---|
PB | 深证综指 | 4.18 | 75.35 | 1.43 | 1.76 | 2.22 | 2.40 | 2.56 | 3.00 | 3.50 | 3.96 | 4.36 | 4.94 | 6.38 | 135 |
---|
PE | 巨潮小盘 | 46.30 | 64.78 | 18.64 | 30.25 | 31.69 | 32.92 | 35.15 | 40.70 | 43.75 | 49.09 | 56.74 | 71.09 | 86.67 | 134 |
---|
PB | 巨潮小盘 | 3.18 | 66.19 | 1.34 | 1.64 | 2.03 | 2.23 | 2.38 | 2.62 | 2.94 | 3.33 | 3.66 | 4.05 | 5.32 | 134 |
---|
PE | 上证消费 | 32.51 | 35.39 | 23.77 | 27.39 | 29.33 | 31.25 | 33.59 | 35.49 | 39.08 | 51.59 | 57.61 | 72.23 | 78.03 | 87 |
---|
PB | 上证消费 | 3.30 | 39.09 | 2.45 | 2.73 | 2.95 | 3.10 | 3.32 | 3.47 | 3.75 | 4.21 | 4.64 | 5.02 | 5.79 | 87 |
---|
PE | 上证医药 | 39.01 | 86.39 | 22.45 | 27.57 | 30.13 | 31.90 | 32.63 | 33.58 | 34.33 | 35.02 | 36.75 | 40.28 | 59.94 | 87 |
---|
PB | 上证医药 | 3.88 | 58.83 | 2.92 | 3.19 | 3.41 | 3.49 | 3.61 | 3.77 | 3.90 | 4.42 | 4.79 | 5.36 | 6.22 | 87 |
---|
PE | 上证金融 | 9.04 | 30.12 | 7.30 | 7.71 | 8.47 | 9.02 | 10.03 | 10.72 | 11.84 | 14.00 | 15.01 | 22.91 | 32.23 | 87 |
---|
PB | 上证金融 | 1.39 | 28.23 | 1.08 | 1.18 | 1.27 | 1.41 | 1.51 | 1.61 | 1.75 | 2.02 | 2.33 | 2.93 | 4.05 | 87 |
---|
PE | 上证信息 | 59.31 | 65.30 | 26.65 | 33.77 | 36.47 | 43.28 | 48.88 | 54.02 | 57.50 | 60.90 | 68.35 | 80.31 | 101.23 | 87 |
---|
PB | 上证信息 | 5.42 | 90.37 | 1.89 | 2.29 | 2.68 | 3.12 | 3.27 | 3.71 | 4.02 | 4.40 | 4.75 | 5.30 | 8.54 | 87 |
---|
PE | 上证环保 | 32.29 | 25.02 | 27.03 | 30.31 | 31.44 | 33.13 | 33.51 | 34.35 | 35.37 | 36.82 | 40.19 | 44.68 | 54.85 | 43 |
---|
PB | 上证环保 | 3.11 | 70.97 | 1.90 | 2.17 | 2.35 | 2.41 | 2.46 | 2.62 | 2.91 | 3.08 | 3.37 | 3.84 | 4.97 | 43 |
---|