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

量化交易吧 /  量化平台 帖子:3365837 新帖:12

选股——实时观察市盈率

外汇交易达人发表于:5 月 10 日 07:20回复(1)

本文分享代码用于实时计算各个行业及个股的市盈率,以便及时发现价值洼地,用于选股决策。
计算完后发现有的行业确实是投资的绝佳机会。

市盈率的概念对于接触量化的小伙伴来说再熟悉不过了。下面借用百度百科的解释:
市盈率(Price earnings ratio,即P/E ratio)也称“本益比”、“股价收益比率”或“市价盈利比率(简称市盈率)”。
市盈率是最常用来评估股价水平是否合理的指标之一,由股价除以年度每股盈余(EPS)得出(以公司市值除以年度股东应占溢利亦可得出相同结果)。计算时,股价通常取最新收盘价,而EPS方面,若按已公布的上年度EPS计算,称为历史市盈率(historical P/E);计算预估市盈率所用的EPS预估值,一般采用市场平均预估(consensus estimates),即追踪公司业绩的机构收集多位分析师的预测所得到的预估平均值或中值。何谓合理的市盈率没有一定的准则。
市盈率是某种股票每股市价与每股盈利的比率。市场广泛谈及市盈率通常指的是静态市盈率,通常用来作为比较不同价格的股票是否被高估或者低估的指标。用市盈率衡量一家公司股票的质地时,并非总是准确的。一般认为,如果一家公司股票的市盈率过高,那么该股票的价格具有泡沫,价值被高估。当一家公司增长迅速以及未来的业绩增长非常看好时,利用市盈率比较不同股票的投资价值时,这些股票必须属于同一个行业,因为此时公司的每股收益比较接近,相互比较才有效。

在对企业进行估值时,常用的办法有两种,分别是绝对估值法和相对估值法。绝对估值法是依据企业盈利增速和折现率计算,企业增速这个指标不好预测,我感觉绝对估值法难度较大。而相对估值法则计算起来相对容易,这就需要用到市盈率了。

市盈率简单来讲就是回本要用的时间,假设你花78元买了五粮液的股票,2017年,每一股分配2块8毛5的利润,那么这个利润水平下,回本时间是27.6,也就是说要差不多27.6年能够回本。

市盈率越低,回本时间越短。作为投资来讲,这个就越划算。

关于市盈率的更详细介绍可以看下面这篇文章:
市盈率指标详解及相关文献概述

import pandas as pd
import numpy as np
#证监会行业
HY_ZJH = ['A01','A02','A03','A04','A05','B06','B07','B08','B09','B11','C13','C14','C15',
          'C17','C18','C19','C20','C21','C22','C23','C24','C25','C26','C27','C28','C29',
          'C30','C31','C32','C33','C34','C35','C36','C37','C38','C39','C40','C41','C42',
          'D44','D45','D46','E47','E48','E50','F51','F52','G53','G54','G55','G56','G58',
          'G59','H61','H62','I63','I64','I65','J66','J67','J68','J69','K70','L71','L72',
          'M73','M74','N77','N78','P82','Q83','R85','R86','R87','S90']
#聚宽一级行业
HY_JK1 = ['HY001','HY002','HY003','HY004','HY005','HY006','HY007','HY008','HY009','HY010','HY011']
#聚宽二级行业
HY_JK2 = ['HY401','HY402','HY403','HY404','HY405','HY406','HY407','HY408','HY409','HY410',
           'HY411','HY412','HY413','HY414','HY415','HY416','HY417','HY418','HY419','HY420',
           'HY421','HY422','HY423','HY424','HY425','HY426','HY427','HY428','HY429','HY432',
           'HY433','HY434','HY435','HY436','HY437','HY438','HY439','HY440','HY441','HY442',
           'HY443','HY444','HY445','HY446','HY447','HY448','HY449','HY450','HY451','HY452',
           'HY453','HY454','HY455','HY457','HY458','HY459','HY460','HY461','HY462','HY463',
           'HY464','HY465','HY466','HY467','HY468','HY469','HY470','HY471','HY472','HY473',
           'HY474','HY476','HY477','HY478','HY479','HY480','HY481','HY483','HY484','HY485',
           'HY486','HY487','HY488','HY489','HY491','HY492','HY493','HY494','HY496','HY497',
           'HY500','HY501','HY504','HY505','HY506','HY509','HY510','HY511','HY512','HY513',
           'HY514','HY515','HY516','HY517','HY518','HY519','HY520','HY521','HY523','HY524',
           'HY525','HY526','HY527','HY528','HY529','HY530','HY531','HY570','HY571','HY572',
           'HY573','HY574','HY576','HY578','HY579','HY587','HY588','HY591','HY593','HY595',
           'HY596','HY597','HY598','HY601']
#申万一级行业
HY_SW1 = ['801010','801020','801030','801040','801050','801080','801110','801120','801130',
          '801140','801150','801160','801170','801180','801200','801210','801230','801710',
          '801720','801730','801740','801750','801760','801770','801780','801790','801880',
          '801890']
#申万二级行业
HY_SW2 = ['801011','801012','801013','801014','801015','801016','801017','801018','801021',
       '801022','801023','801024','801032','801033','801034','801035','801036','801037',
       '801041','801051','801053','801054','801055','801072','801073','801074','801075',
       '801076','801081','801082','801083','801084','801085','801092','801093','801094',
       '801101','801102','801111','801112','801123','801131','801132','801141','801142',
       '801143','801144','801151','801152','801153','801154','801155','801156','801161',
       '801162','801163','801164','801171','801172','801173','801174','801175','801176',
       '801177','801178','801181','801182','801191','801192','801193','801194','801202',
       '801203','801204','801205','801211','801212','801213','801214','801215','801222',
       '801223','801231','801711','801712','801713','801721','801722','801723','801724',
       '801725','801731','801732','801733','801734','801741','801742','801743','801744',
       '801751','801752','801761','801881']
#申万三级行业
HY_SW3 = ['850111','850112','850113','850121','850122','850131','850141','850151','850152','850154','850161',
       '850171','850181','850211','850221','850222','850231','850241','850242','850311','850313','850321',
       '850322','850323','850324','850331','850332','850333','850334','850335','850336','850337','850338',
       '850339','850341','850342','850343','850344','850345','850351','850352','850353','850361','850362',
       '850363','850372','850373','850381','850382','850383','850411','850412','850521','850522','850523',
       '850531','850541','850542','850543','850544','850551','850552','850553','850611','850612','850614',
       '850615','850616','850623','850711','850712','850713','850714','850715','850716','850721','850722',
       '850723','850724','850725','850726','850727','850728','850729','850731','850741','850751','850811',
       '850812','850813','850822','850823','850831','850832','850833','850841','850851','850852','850911',
       '850912','850913','850921','850935','850936','850941','851012','851013','851014','851021','851111',
       '851112','851113','851114','851115','851121','851122','851231','851232','851233','851234','851235',
       '851236','851241','851242','851243','851244','851311','851312','851313','851314','851315','851316',
       '851322','851323','851324','851325','851326','851327','851411','851421','851432','851433','851434',
       '851435','851441','851511','851512','851521','851531','851541','851551','851561','851611','851612',
       '851613','851614','851615','851621','851631','851641','851711','851721','851731','851741','851751',
       '851761','851771','851781','851811','851821','851911','851921','851931','851941','852021','852031',
       '852032','852033','852041','852051','852052','852111','852112','852121','852131','852141','852151',
       '852211','852221','852222','852223','852224','852225','852226','852241','852242','852243','852244',
       '852311','857221','857231','857232','857233','857234','857235','857241','857242','857243','857244',
       '857251','857321','857322','857323','857331','857332','857333','857334','857335','857336','857341',
       '857342','857343','857344','857411','857421','857431','858811']
#概念
HY_GN = ['GN001','GN028','GN030','GN031','GN032','GN034','GN035','GN036','GN039','GN040','GN041','GN045','GN046',
      'GN050','GN057','GN062','GN069','GN074','GN075','GN076','GN077','GN079','GN080','GN081','GN086','GN087',
      'GN088','GN089','GN090','GN091','GN092','GN093','GN096','GN098','GN099','GN100','GN101','GN103','GN104',
      'GN106','GN107','GN109','GN110','GN111','GN112','GN113','GN114','GN115','GN116','GN119','GN121','GN123',
      'GN124','GN125','GN126','GN127','GN128','GN129','GN130','GN131','GN132','GN133','GN134','GN135','GN136',
      'GN137','GN138','GN139','GN140','GN141','GN142','GN144','GN145','GN146','GN148','GN149','GN151','GN152',
      'GN153','GN154','GN155','GN156','GN157','GN158','GN159','GN160','GN161','GN162','GN163','GN164','GN165',
      'GN166','GN167','GN168','GN169','GN170','GN171','GN172','GN173','GN174','GN175','GN176','GN177','GN178',
      'GN179','GN180','GN181','GN182','GN183','GN184','GN185','GN186','GN187','GN188','GN189','GN190','GN191',
      'GN192','GN193','GN194','GN195','GN196','GN197','GN198','GN199','GN200','GN201','GN202','GN203','GN204',
      'GN205','GN206','GN207','GN208','GN209','GN210','GN211','GN212','GN213','GN214','GN215','GN216','GN217',
      'GN218','GN219','GN220','GN221','GN222','GN223','GN224','GN225','GN226','GN227','GN228','GN229','GN230']
qj_count = 10 #从当天向前取几天的数据平均
past_q = ['2018q1','2017q4','2017q3','2017q2']
stocks_for_pe = get_index_stocks('000300.XSHG')
def get_industry_pe(industry_codes):
    list_industry_code = []
    list_pe_mean = []
    list_pe1_mean = []
    for i in industry_codes:
        stocks = get_industry_stocks(i)
        q = query( valuation.code,valuation.day, valuation.market_cap, valuation.pe_ratio, valuation.capitalization,
                  income.np_parent_company_owners,indicator.eps,income.net_profit
                 ).filter(valuation.code.in_(stocks))
        df = get_fundamentals(q)
        df.index = df['code']
        close_price = history(count = qj_count, field='close',security_list=stocks).mean()
        close_price = pd.DataFrame(close_price,columns = ['close_price'])
        
        #计算过期四个季度的财务数据
        for sd in past_q:
            df_past = get_fundamentals(q,statDate=sd)
            df_past.index = df_past['code']
            df[sd] = df_past['np_parent_company_owners']
    
        
        df = pd.merge(close_price,df, left_index=True,right_index=True)
        df['new_market_cap'] = (df['close_price']*df['capitalization'])/(10000)
        df['pe1'] = df['close_price']/(df['eps']*4)
        df = df.dropna(axis=0)
        df_profit = df.loc[:,past_q]
        df_profit.fillna(method='ffill',axis=1)
        df['profit_sum'] = df_profit.sum(axis=1)
        df['new_pe'] = (df['new_market_cap']*(1e8))/df['profit_sum']
        #df['pe_change'] = df['pe_ratio']- df['new_pe']

        
        #df = df.sort_values(by = 'new_pe', ascending = True)
        index = df.index
        
        #去除PE为负和过大的股票,负值会影响平均PE的结果
        for j in index:            
            if ((df['new_pe'].loc[j] < 0)|(df['new_pe'].loc[j] > 200)|(df['pe1'].loc[j] > 200)):
                df.drop(j,axis=0,inplace = True)
        
        
        #查看单个行业
        if i == '801192':
            print('行业代码:%s'%i)
            print('行业股票代码:%s'%stocks)
            pe = df['new_pe'].mean()
            print('行业PE:%.2f'%pe)
            print(df)
            print(df_profit)
        
        pe_mean = df['new_pe'].mean()
        pe1_mean = df['pe1'].mean()
        list_industry_code.append(i)
        list_pe_mean.append(pe_mean)
        list_pe1_mean.append(pe1_mean)
        #print('%s pe mean: %.2f' %(i, pe_mean))
        #print(df)
    #print(list_industry_code)
    industry_pe = pd.DataFrame(index = list_industry_code, data=list_pe_mean,columns=['pe'])
    industry_pe_sort = industry_pe.sort_values(by='pe',ascending=True)
    industry_pe1 = pd.DataFrame(index = list_industry_code, data=list_pe1_mean,columns=['pe1'])
    industry_pe1_sort = industry_pe1.sort_values(by='pe1',ascending=True)
    industry_pe = pd.concat([industry_pe,industry_pe1],axis = 1)
    print(industry_pe.sort_values(by='pe',ascending=True))
    
'''
个股市盈率计算
'''
def get_stock_pe(stocks):
    q = query( valuation.code,valuation.day, valuation.market_cap, valuation.pe_ratio, valuation.capitalization,
                  income.np_parent_company_owners,indicator.eps,income.net_profit
                 ).filter(valuation.code.in_(stocks))
    df = get_fundamentals(q)
    df.index = df['code']
    close_price = history(count = qj_count, field='close',security_list=stocks).mean()
    close_price = pd.DataFrame(close_price,columns = ['close_price'])
    
    
    #计算过期四个季度的财务数据
    for sd in past_q:
        df_past = get_fundamentals(q,statDate=sd)
        df_past.index = df_past['code']
        df[sd] = df_past['np_parent_company_owners']
    
    df = pd.merge(close_price,df, left_index=True,right_index=True)
    df['new_market_cap'] = (df['close_price']*df['capitalization'])/(10000)
    df['pe1'] = df['close_price']/(df['eps']*4)
    df = df.dropna(axis=0)
    df_profit = df.loc[:,past_q]
    df_profit.fillna(method='ffill',axis=1)
    df['profit_sum'] = df_profit.sum(axis=1)
    df['new_pe'] = (df['new_market_cap']*(1e8))/df['profit_sum']
    
    index = df.index
        
    #去除PE为负和过大的股票,负值会影响平均PE的结果
    for j in index: 
        if ((df['new_pe'].loc[j] < 0)|(df['new_pe'].loc[j] > 200)|(df['pe1'].loc[j] > 200)):
            df.drop(j,axis=0,inplace = True)
    df_pe = df.loc[:,['new_pe','pe1','pe_ratio']].sort_values(by='new_pe')
    print(df_pe)
get_stock_pe(stocks_for_pe)
#get_industry_pe(HY_JK2)
                 new_pe         pe1  pe_ratio
600015.XSHG    4.719528    5.152903    4.7692
600016.XSHG    4.929376    3.474969    5.0662
601166.XSHG    5.073355    4.168726    5.1765
600000.XSHG    5.104780    4.826118    5.2084
002146.XSHE    5.794492   14.241492    5.6126
600919.XSHG    5.835985    5.393170    6.0833
601818.XSHG    5.906820    5.224768    6.0331
000069.XSHE    5.965094   11.985725    5.7838
601328.XSHG    5.995122    5.296673    6.2242
601988.XSHG    6.044115    5.387387    6.3170
601288.XSHG    6.060792    4.693031    6.2858
600383.XSHG    6.232594   10.057732    6.1393
601009.XSHG    6.375755    5.579071    6.4749
000898.XSHE    6.428185    6.190855    6.4601
601668.XSHG    6.454049    4.813076    6.3590
000001.XSHE    6.457299    5.769331    6.5782
601398.XSHG    6.495245    5.956581    6.8183
601939.XSHG    6.584878    5.488652    6.9192
601998.XSHG    6.587789    5.867860    6.6725
601169.XSHG    6.605728    5.444993    6.6733
000625.XSHE    6.623577    7.290373    6.5758
600068.XSHG    6.797976   10.533435    6.6401
600376.XSHG    6.813262   13.101489    6.6533
601186.XSHG    6.855928    8.314484    6.8142
600153.XSHG    7.079494   10.626829    6.9625
601225.XSHG    7.097087    6.630588    7.4597
000402.XSHE    7.520015   10.349154    7.1553
601229.XSHG    7.573691    6.823869    7.6274
600516.XSHG    7.726706    5.548107    7.9193
600340.XSHG    7.791812    7.932872    7.9006
...                 ...         ...       ...
601857.XSHG   50.571940   33.909910   51.1228
600038.XSHG   51.111688   82.264387   52.2983
600893.XSHG   51.636881 -222.451172   54.1780
002500.XSHE   52.557552   53.387097   53.5103
603288.XSHG   52.759486   41.200314   55.2139
002797.XSHE   54.021650   82.842640   52.9624
600118.XSHG   54.043207   92.554134   55.4024
600867.XSHG   54.178246   37.103125   56.1723
600339.XSHG   56.719236 -202.264151   55.5552
300024.XSHE   60.776645  108.698718   61.2892
300003.XSHE   64.494262   51.592244   64.6338
600570.XSHG   65.281073  138.437500   63.2510
002153.XSHE   72.168671  114.142743   73.6384
000839.XSHE   73.487588  102.500000   73.7392
600436.XSHG   74.077287   51.496762   75.7766
600583.XSHG   75.675894  -15.806748   76.0724
300059.XSHE   79.051281   45.616097   81.4026
600276.XSHG   80.477397   54.672584   79.3631
002065.XSHE   84.068263   51.857477   88.5378
600489.XSHG   90.690284  102.068452   91.4968
600111.XSHG   92.566003   92.550000   90.0156
300015.XSHE   93.206089   57.617388   94.2654
600588.XSHG  100.178719  -80.172999  100.8781
600050.XSHG  102.690357   28.636905  103.5228
601989.XSHG  109.509694   83.250000  110.1674
002044.XSHE  111.583998 -146.878307  109.2275
300122.XSHE  115.609112   70.295749  123.2951
601958.XSHG  117.811361   76.620603  117.8307
002230.XSHE  148.191654  133.483051  150.2991
600100.XSHG  173.800728  -28.849206  178.3031

[279 rows x 3 columns]
 

全部回复

0/140

量化课程

    移动端课程