Tag: api-valuation

Calculate PEs for all Indices

index PE

Some investors like to use the PE ratio of an index to time their entry and exit. This is especially true about the widely tracked Nifty 50. However, there are advantages to rolling your own.

Annual vs. Quarterly EPS

The most popular approach of calculating PE is by dividing the price by the last reported annual EPS. This is probably fine most of the time. However, we prefer to add up the last 4 diluted quarterly EPS so that we have the latest numbers.

Closing vs. Live Prices

Once again, a matter of preference. Since most data points present the PE based on the last close, it may be useful to calculate it based on the live price, just to see the difference.

Other indices

Whereas the Nifty index PE can be obtained from a number sources, what about the other indices? And sector-wide PE estimates? By having a script handy, you can always have a ready reckoner of where things stand.

The Code

The StockViz API exposes the QuarterlyEps endpoint that returns the quarterly basic and diluted EPS in descending order of periods. You can use this to calculate the annualized number, grab the live price and adjust it by the stock’s weight within the index to arrive at the index EPS.

for cc in constituents:
	ticker = cc["SYMBOL"]
	#print ticker,

	price = common.getLivePrice(ticker)
	#print price,

	epss = common.getEps(ticker)

	annualEps = 0
	for i in range(0,4):
		annualEps += epss[i]["EPS_DILUTED_AEI"]

	#print annualEps, price/annualEps

	if annualEps != 0:
		indexPe += price/annualEps * cc["WEIGHTAGE"]

	indexPe /= 100

You can download the entire code on GitHub.

Previously