How to calculate Price Book ratio with Python

Price to Book ratio (PB) is a very useful metric to analyse the relative value of a company. PB helps to determine if a company is currently undervalued or overvalued in comparison to similar firms. In this article, we are going to learn how to calculate Price Book value (PB) using Python.

How to calculate Price to Book ratio
Photo by Isaac Smith on Unsplash

What is Price to Book Ratio?

Before moving to Python, it is always good to have clear what we want to do. In this case, we need to know what is the Price Book ratio and how it is calculated.

How to calculate Price Book Ratio
Price Book Ratio Definition

Price Book value is a financial ratio which can give a good indication of a firm price compare to other similar firms. It is calculated as the company market capitalisation divided by the book value of equity.

An alternative method to calculate PB ratio is to divide the stock price by the book value of equity on a per share basis. We will use the first approach for our example.

There are only two elements required to calculate the Price Book Ratio:

  • Company Market Capitalisation: It can be easily obtained from any exchange or financial website. It is the quote price per stock multiply by the number of shares outstanding.
  • Book Value of Equity: It can be extracted from the financial reports published by public companies. It is found under the Equity section of the Balance Sheet under the label of total shareholders equity. The book value is simply the net assets of a company and it is a past looking measure. I.e. it takes into account historic transactions such as profit and losses from previous periods, dividends, etc.

How to interpret Price Book Ratio?

Price Book ratio is a good indicator to know if a firm is overvalued or undervalued compared to a peer company. A high Price Book ratio may indicate that the firm is expensive or maybe that the market is very optimistic about a firm future prospects. Growing firms tends to have a very high Price to Book ratio.

On the other hand, companies with low Price Book ratios are known as value stocks. It may indicate that a company is undervalued since the book value is higher than the market capitalisation. However, we need to be cautious with companies with low Price to Book ratios since it may also be a sign of financial distress or expected future earnings drop.

Firms in the technology industry are a clear example of growth companies with very high Price to Book ratio.

Let’s check the PB ratio with Python by calculating the ratio for companies in the technological sector.

Calculating Price Book Ratio with Python

In order to get the required financial numbers for the calculation, I will use a free API containing stock and financial data. I recommend you to have a look at the documentation.

We will create a function, getpricetobook that will take as argument the ticker of the company for which we want to extract data. In our example, we will pass AAPL to get financials for Apple.

We make a request to the end point of our financial API to retrieve Balance Sheet statement data. Then, we store the response in a variable call BS.

Note that you need to sign up to financialmodelingprep in order to get an API key. If you opt to sign up for a paid subscription using my link, you will get a 25% discount. Alternatively, you can get up 250 requests a month for free.

import requests
api_key = 'your api key'
def getpricetobook(stock):
    BS = requests.get(f"https://financialmodelingprep.com/api/v3/balance-sheet-statement/AAPL?period=quarter&limit=400&apikey={api_key}")
    BS = BS.json()
    print(BS)
    
getpricetobook('AAPL')

If we print our BS variable, we will see that we have a json object that we need to parse. This is an easy task to do with Python. BS contains a dictionary with two keys. The second key, financials, contains a list of quarterly Balance Sheet statements:

How to retrieve Balance Sheet Python
Balance Sheet from Apple

First, we need to extract the Total shareholders equity value since that will represent our Book value of equity (i.e. denominator of the Price Book ratio).

We can extract it by parsing BS and getting the total shareholder equity included in the first element of the list:

book_value_equity_now = float(BS[0]['totalStockholdersEquity'])  
print(book_value_equity_now)

#returned numer: 89531000000.0

Note that we have included float in front of the API returned string in order to convert it to a float.

Next, we have to retrieve Apple’s market capitalisation. We can easily do that by making a request to the respective API end point:

company_info = requests.get(f"https://financialmodelingprep.com/api/v3/profile/AAPL?apikey={api_key}")

company_info = company_info.json()
market_cap =  float(company_info[0]['mktCap'])
print(market_cap)

#market_cap value is 1423019080000.0
Retrieve Apple price with Python
Apple Profile

As we did previously, we extract the mktCap value within the profile dictionary. Note that this value is based on the most current stock price while our equity book value is based on the latest available Balance Sheet Statement.

Finally, we can calculate our Price to Book ratio:

price_to_book = market_cap/book_value_equity_now
return (price_to_book)

Putting the Python script together

Now, we have our Python script ready. We can pass any company ticker and Python will calculate the Price Book ratio for us. See below the complete code, and the calculation of the PB ratio for Apple, Microsoft, Ford and Tesla:

import requests
api_key = 'your api key'

def getpricetobook(stock):
    BS = requests.get(f"https://financialmodelingprep.com/api/v3/balance-sheet-statement/AAPL?period=quarter&limit=10&apikey={api_key}")
    BS = BS.json()
    book_value_equity_now = float(BS[0]['totalStockholdersEquity'])  

    company_info = requests.get(f"https://financialmodelingprep.com/api/v3/profile/AAPL?apikey={api_key}")
    company_info = company_info.json()
    market_cap =  float(company_info[0]['mktCap'])
    price_to_book = market_cap/book_value_equity_now
    return (price_to_book)
     
                                
print('AAPL:', getpricetobook('AAPL') )
print('MSFT:', getpricetobook('MSFT') )

print('Ford:', getpricetobook('F') )
print('TSLA:', getpricetobook('TSLA') )

#Return:
AAPL: 15.89414928907306
MSFT: 12.47125039733355
Ford: 0.9288292681546861
TSLA: 22.989353311258277

Wrapping up and Other Relative Valuation Measures

Great, we have now a script to calculate for us the Price to Book ratio. It was quite easy to build and may be useful to know if certain companies may be overvalued/ undervalued compared to a peer or industry group.

As next steps, I encourage you to calculate some other relative valuation measures such as Price Sales ratio and Price Earnings ratio by your own.

If you have liked the article, I recommend you to read my latest article on how to calculate Return on Equity (ROE) with Python since it can be used in combination with the Price Book value. High PB and low ROE may be a clear sign of overprice company. Low P/B and high ROE may be a good sign in terms of a cheap company with great prospects.

In addition, you can also have a look at my post on price earnings ratio with Python for comparable companies. To have a better understanding on how to calculate key financial metrics with Python, you can visit the following article.

If you like my content on Python for Finance, I would be more than happy if you support us by sharing in social media.