Introduction
Recently, more and more people have been interested in stock investing. However, these new investors wonder how to figure out if a company’s stock is overvalued or undervalued and should put in their portfolio. The P/E (Price-to-Earnings) ratio is a easy way to estimate a company’s value. When a company’s P/E ratio is higher than other companies in same sector or peer group, it means the company’s stock is overvalued. Also, P/S (Price-to-Sales) ratio, P/B (Price-to-Book) ratio, and PEG (Price / Earnings-to-Growth) ration can be a way to check whether a stock is overvalued or undervalued. In this post I will explore how to get these ratios, and find out a company’s stock value is overvalued or undervalued by extracting fundamentals data from Yahoo Finance by using the yahoo_fin package.
What is P/E, P/S, P/B and PEG Ratios
P/E Ratio
The price-to-earnings (P/E ) ratio is an investment valuation ratio that measures its current stock price relative to its earnings per share (EPS).
P/S Ratio
The price-to-sales (P/S) ratio is an investment valuation ratio that compares a company’s stock price to its revenues. It is an indicator of the value that financial markets have placed on each dollar of a company’s sales or revenues.
P/B Ratio
The price-to-book (P/B) is an investment valuation ratio ratio that compares a company’s market value to its book value. The market value of a company is its share price multiplied by the number of outstanding shares. The book value is the net assets of a company.
PEG Ratio
The price/earnings to growth (PEG) ratio is an investment valuation ratio ratio that a stock’s price-to-earnings (P/E) ratio divided by the growth rate of its earnings for a specified time period. The PEG ratio is used to determine a stock’s value while also factoring in the company’s expected earnings growth.
Import stock_info Module from yahoo_fin Package
Install yahoo_fin package by using pip
pip install yahoo_fin
If you already have a previous version of yahoo_fin package, then you can upgrade package
pip install yahoo_fin --upgrade
Import the stock_info module from yahoo_fin to get fundamentals data from Yahoo Finance
import yahoo_fin.stock_info as si
import pandas as pd
import matplotlib.pyplot as plt
import numpy as np
Get P/E, P/S, P/B, and PEG ratios of a Company
Use the get_quote_table method to extract the data of a stock from Yahoo Finance
quote = si.get_quote_table("nvda")
P/E ratio
PE_ratio = quote["PE Ratio (TTM)"] #80.07
P/S ratio
val = si.get_stats_valuation("nvda")
val = val.iloc[:,:2]
val.columns = ["Attribute", "Recent"]PS_ratio = float(val[val.Attribute.str.contains("Price/Sales")].iloc[0,1]) #19.51
P/B ratio
PB_ratio = float(val[val.Attribute.str.contains("Price/Book")].iloc[0,1]) #19.01
PEG ratio
PEG_ratio = float(val[val.Attribute.str.contains("PEG Ratio")].iloc[0,1]) #3.32
Get Competitors Ratio in S&P 500 List
Get list of companies in S&P 500 from Wikipedia page
payload=pd.read_html("https://en.wikipedia.org/wiki/List_of_S%26P_500_companies")
df = payload[0]
Find Nvidia’s GICS Sector, GICS Sub-Industry
df[df["Symbol"] == "NVDA"]
Find companies’ symbol list which GICS Sector is information technology, and GICS Sub-Industry is semiconductors
# get list of competitors tickers
competitors = list(df.loc[(df["GICS Sector"] == "Information Technology") & (df["GICS Sub-Industry"] == "Semiconductors")]["Symbol"].values)
# ['AMD', 'ADI', 'AVGO', 'INTC', 'MXIM', 'MCHP', 'MU', 'MPWR', 'NVDA', 'NXPI', 'QRVO', 'QCOM', 'SWKS', 'TXN', 'XLNX']
Get fundamentals stats of competitors
# Get data in the current column for each stock's valuation table
competitors_stats = {}
for ticker in competitors:
temp = si.get_stats_valuation(ticker)
temp = temp.iloc[:,:2]
temp.columns = ["Attribute", "Recent"]
competitors_stats[ticker] = temp
# combine all the stats valuation tables into a single data frame
combined_stats = pd.concat(competitors_stats)
combined_stats = combined_stats.reset_index()
del combined_stats["level_1"]
# update column names
combined_stats.columns = ["Ticker", "Attribute", "Recent"]
Drop Nvidia’s stats and NXP Semiconductors NV stats because NXPI’s P/E ratio is extremely high compare other competitors
# drop Nvidia's stats
combined_stats= combined_stats[combined_stats['Ticker'] != 'NVDA']# drop NXPI's stats - because the value is extremly high
combined_stats= combined_stats[combined_stats['Ticker'] != 'NXPI']
Get the lists of P/E, P/S, P/B, and PEG ratios of competitors
# get the list of P/E ratio of competitors
com_PE = combined_stats[combined_stats.Attribute.str.contains("Trailing P/E")].Recent.values.astype(float)
# array([ 39.36, 40.81, 52.74, 13.06, 35.62, 119.13, 34.87,
100.92, 42.9 , 22.48, 28.24, 32.17, 51.94])# get the list of P/S ratio of competitors
com_PS = combined_stats[combined_stats.Attribute.str.contains("Price/Sales")].Recent.values.astype(float)
# array([10.03, 10.19, 7.82, 3.51, 10.96, 7.5 , 4.74, 19.66,
5.6 , 5.65, 7.62, 12.39, 10.56])# get the list of P/B ratio of competitors
com_PB = combined_stats[combined_stats.Attribute.str.contains("Price/Book")].Recent.values.astype(float)
# array([16.87, 4.7 , 7.77, 3.23, 13.28, 7.63, 2.59, 16.67,
4.5 , 20.21, 6.71, 19.3 , 12.28])# get the list of PEG ratio of competitors
com_PEG = combined_stats[combined_stats.Attribute.str.contains("PEG")].Recent.values.astype(float)
# array([1.51, 2.26, 1.25, 2.84, 2.7 , 1.42, 0.9 , 3.61, 1.31, 0.75,
1.24, 2.92, 3.61])
Result
Create box plots using Matplotlib
# create plot
fig, axs = plt.subplots(nrows=2, ncols=2, figsize=(10,10))axs[0,0].boxplot(com_PE)
axs[0,0].set_xticklabels(['P/E ratio'])
axs[0,0].plot(1, PE_ratio, marker='o', markersize=10)
axs[0,0].set_title('P/E ratio')axs[0,1].boxplot(com_PS)
axs[0,1].set_xticklabels(['P/S ratio'])
axs[0,1].plot(1, PS_ratio, marker='o', markersize=10)
axs[0,1].set_title('P/S ratio')axs[1,0].boxplot(com_PB)
axs[1,0].set_xticklabels(['P/B ratio'])
axs[1,0].plot(1, PB_ratio, marker='o', markersize=10)
axs[1,0].set_title('P/B ratio')axs[1,1].boxplot(com_PEG)
axs[1,1].set_xticklabels(['PEG ratio'])
axs[1,1].plot(1, PEG_ratio, marker='o', markersize=10)
axs[1,1].set_title('PEG ratio')# show plot
plt.show()
Conclusion
In this post, I wanted to check Nvidia’s stock price is overvalued or undervalued. According to the result, I can say Nvidia’s stock is overvalued because its P/E ratio and P/S ratio are considerably higher than its competitors’. In addition, P/B ratio and PEG ratio of Nvidia also in the range between 75 percent and 100 percent of other competitors’ distribution. Checking competitors’ ratios is a easy way to figure out a company’s stock values is overvalued or undervalued. I used these codes to check Nvidia’s stock value but these codes also can use for any companies to check their stock value.
Source
- http://theautomatic.net/2018/07/31/how-to-get-live-stock-prices-with-python/
- http://theautomatic.net/2020/05/05/how-to-download-fundamentals-data-with-python/
- https://www.thebalance.com/how-to-tell-when-a-stock-is-overvalued-357147
- https://www.investopedia.com/terms/p/price-earningsratio.asp
- https://www.investopedia.com/terms/p/price-to-salesratio.asp
- https://www.investopedia.com/terms/p/price-to-bookratio.asp
- https://www.investopedia.com/terms/p/pegratio.asp