请 [注册] 或 [登录]  | 返回主站

量化交易吧 /  数理科学 帖子:3365785 新帖:17

全市场估值-等权PE PB

到此一游发表于:5 月 9 日 18:59回复(1)


目前市场大部分还在高位,只有大蓝筹便宜。

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上证5011.3117.999.7910.5911.4912.4913.9615.0316.0617.8224.8632.6547.33135
PB上证501.428.441.301.441.581.701.801.932.102.302.723.756.57135
PE沪深30020.2246.2812.1115.8916.7017.4618.7521.0822.8524.5032.0041.0554.96132
PB沪深3002.1144.441.331.531.721.862.012.242.602.813.173.926.80132
PE中证流通52.8976.559.3922.4527.8330.6632.1335.6840.3746.4656.2864.3677.09130
PB中证流通3.5668.251.451.842.122.262.442.953.313.623.974.405.76130
PE中证50039.6260.0717.3625.0927.0028.3231.0835.5239.5746.4154.1962.2672.21111
PB中证5002.8550.841.321.972.082.212.532.823.133.393.594.005.03111
PE深证综指63.9786.9117.4728.1831.3234.1637.6740.7147.0451.8758.7566.3192.41135
PB深证综指4.1875.351.431.762.222.402.563.003.503.964.364.946.38135
PE巨潮小盘46.3064.7818.6430.2531.6932.9235.1540.7043.7549.0956.7471.0986.67134
PB巨潮小盘3.1866.191.341.642.032.232.382.622.943.333.664.055.32134
PE上证消费32.5135.3923.7727.3929.3331.2533.5935.4939.0851.5957.6172.2378.0387
PB上证消费3.3039.092.452.732.953.103.323.473.754.214.645.025.7987
PE上证医药39.0186.3922.4527.5730.1331.9032.6333.5834.3335.0236.7540.2859.9487
PB上证医药3.8858.832.923.193.413.493.613.773.904.424.795.366.2287
PE上证金融9.0430.127.307.718.479.0210.0310.7211.8414.0015.0122.9132.2387
PB上证金融1.3928.231.081.181.271.411.511.611.752.022.332.934.0587
PE上证信息59.3165.3026.6533.7736.4743.2848.8854.0257.5060.9068.3580.31101.2387
PB上证信息5.4290.371.892.292.683.123.273.714.024.404.755.308.5487
PE上证环保32.2925.0227.0330.3131.4433.1333.5134.3535.3736.8240.1944.6854.8543
PB上证环保3.1170.971.902.172.352.412.462.622.913.083.373.844.9743
 

全部回复

0/140

量化课程

    移动端课程