# Examining Index and Sectoral P/E Ratios With Python (and R)

Given today’s frothy market environment, it is relatively hard to find value stocks within a thick mire of growth opportunities. As investors, we can a examine a representative market index in order to gauge where the overall market is at in terms of valuation. We can apply various fundamental measures to the index and its constituents in order to gauge if the market is overvalued or not. One such metric we can use is the P/E ratio, or price to earnings ratio.

How to calculate a trailing twelve months (TTM) P/E ratio for a company:

For an individual asset, the P/E ratio or trailing twelve months P/E ratio is calculated as such.

1.) Take the last four quarters of reported earnings per share figures and sum them together.

2.) Divide the current price of the asset by the sum of the last four EPS figures (hence trailing twelve months or TTM).

What we get is a metric that demonstrates how much investors are willing to pay for one dollar of earnings. For instance, if a P/E ratio is 100, that means investors are willing to pay 100 dollars for 1 dollar of the companies earnings.

One thing to note: various vendors calculate metrics differently. We will calculate the TTM P/E ratio for Apple Inc. within R using the quantmod package. We will download quarterly financial statements and extract the last four EPS figures (diluted) to “hand calculate” our P/E ratio. At a very high level, diluted EPS accounts for the conversions of different share classes, while basic does not. We don’t need to worry about the semantics of diluted and basic EPS right now. However, diluted is predominantly used in P/E calculations.

Let’s examine the calculation.

And the result……..

How to calculate the P/E ratio of a stock market index:

When applying calculating the P/E ratio for an index, it is a bit different. We can’t take the current value of the S&P and divide it by the last four EPS figures. To calculate an index or sector P/E ratio, we will need to sum the market caps of the constituent stocks and divide it by the sum of the total earnings of the constituent stocks.

*Disclaimers:

Reliable data:

This script web scrapes all approximately 504 company market caps and earnings from finviz, because it’s very easy to parse the sites HTML.  Note that each site’s fundamental data will likely be different. This may influence the index P/E calculation. For instance, one vendor may not have figures on the stocks class b shares net income. In terms of choosing a vendor, you could use Yahoo Finance or whichever vendor you want where the data is somewhat reliable.

Resort to web scraping last:

You could also use an API service such as Quandl’s for fundamental stock data. I tried using the Yahoo Finance API in order to calculate the S&P 500’s P/E ratio, but the service both in multiple packages for Python and R seems to be defunct, thus I resorted to web scraping. Please note using an API is much more efficient and desired in most circumstances (take into consideration API call limits).

I am going to use Python in order to calculate this, given that obtaining fundamental stock data in R is a bit more difficult. The script I wrote in order to do this is displayed below.

And the result for both the index and sectors…….

We can compare the index P/E result with a popular website called Multipl. Multiple is a website that tracks the S&P 500 P/E ratio as well as the Shiller P/E ratio (CAPE ratio, P/E / 10 ratio, etc.) as well as other indicators.

As you can see, the values are fairly close.

*Some notes when examining an individual P/E ratio (or any fundamental metric) for a company online.*

Make Sure to Use a Benchmark:

In general terms, the average P/E for a company (loosely defined by investors) should be somewhere in the realm of 12 to 17. However, this is extremely general and does not take into consideration industry and sector-specific P/E ratios. By the way, when examining aggregated P/E ratios, we would typically use the median as there could be a small number of outliers that may drastically alter the mean calculation.

For instance, we can examine the median and median P/E of the technology sector and various companies within it. Historically, it can average from 30 to 40 in respect to different time frames. The point here is that when you see the P/E ratio of a company from any sort of vendor online, you should compare it with its sector and industry to fairly examine the stock on a fundamental basis.

#### programmingforfinance

Hi, I'm Frank. I have a passion for coding and extend it primarily within the realm of Finance.

View all posts

## 1 Comment

• J B says:

Hi Frank,
Thanks for this. I seem to run into issues with the ‘tickers’ specification. I get a Name Error –> name ‘tickers’ is not defined.
I reckon that perhaps I should use actual tickers in the code, do you mind demonstrating how one should go about doing that explicitly?

This is the specific part of the code that I’m referring to.

ticker = tag.text
#Adjust ticker notations for site specifications (e.g. BRK.B, BF.B, etc..)
ticker = ticker.replace(‘.’, ‘-‘)
tickers.append(ticker)

Thank you very much,
J.