How to Retrieve Key Financial Metrics with Python

Can you imagine to press a button and within seconds retrieve key financial metrics such as price-to-earnings, price-to-debt dividend yields, etc for multiple c

How to Retrieve Key Financial Metrics with Python

Sep 11, 2023 5:30 PM - Rajnish Katharotiya

blog post cover photo

Image credit: Adeolu Eletu

Can you imagine to press a button and within seconds retrieve key financial metrics such as price-to-earnings, price-to-debt dividend yields, etc for multiple companies? In this post, we are going to do exactly that. We will build a Python script that will allow us to compare financial key ratios for a bunch of companies

Photo by bongkarn thanyakij on Pexels

Why Python for Financial Analysis and Financial Ratios?

Financial analysts searching for stock opportunities may spend hours and hours analysing Financial Statements and comparing companies. For each company, there are a few key metrics that may be worth to look at. For example:

  • By looking into price to earnings, we can have an idea if the company is trading at discount comparing to comparable companies.

  • Or we can see how companies are managing their debt levels compared to peer companies.

  • Or instead, we may be interested to identify companies with higher dividend yields.

Just to get all these metrics for a single company may take a few hours of work and research. Imagine to have to do it for a bunch of companies. Luckily for us, we have Python to make the work for us.

Getting key financial ratios with Python

One of the key aspects to keep in mind while performing a financial analysis is the importance of having comparable data. A single number, it is just that a number. It does not give us any insights unless we can compare it to something else. That is why we should always look into financial numbers from a company and compare them with prior periods or comparable companies.

Comparable companies are companies operating in the same sector and having very similar market capitalisation. For our financial ratio analysis, we will work with technological companies having a market capitalisation higher than $100 billions.

We will make a request to the API end point returning the tickers of companies operating in the technological sector. Note in below code, that we pass the name of the sector that we are interested in as url parameter. Then, we parse the response in order to extract each of the company tickers. Finally, we save the ticker of each of the companies into a Python list.

import requests

import pandas as pd

import requests

demo = 'your api key'

companies = requests.get(f'{demo}')

companies = companies.json()

technological_companies = []

for item in companies:



Retrieving Financial Ratios with Python

Now that we have all the tickers in a Python list, we can move to the next step and retrieve key financial metrics. In this post, we will get all of them using the following API endpoint. First, we loop through each of the companies in our list. Then, we extract and store each of the company metrics in a Python dictionary.

metrics = {}

for item in technological_companies:


metrics[item] = {}

keymetrics = requests.get(f'{item}?limit=40&apikey={demo}')

keymetrics = keymetrics.json()


metrics[item]['date'] = keymetrics[0]['date']

metrics[item]['currentratio'] = float(keymetrics[0]['currentRatio'])

metrics[item]['debtToAssets'] = float(keymetrics[0]['debtRatio'])

metrics[item]['debtToEquity'] = float(keymetrics[0]['debtEquityRatio'])

metrics[item]['dividendYield'] = float(keymetrics[0]['dividendYield'])

metrics[item]['interestCoverage'] = float(keymetrics[0]['interestCoverage'])

metrics[item]['Gross_Profit_Margin'] = float(keymetrics[0]['grossProfitMargin'])

metrics[item]['roe'] = float(keymetrics[0]['returnOnEquity'])

metrics[item]['priceToSalesRatio'] = float(keymetrics[0]['priceSalesRatio'])

metrics[item]['price_to_book_Ratio'] = float(keymetrics[0]['priceToBookRatio'])

metrics[item]['priceEarningsRatio'] = float(keymetrics[0]['priceEarningsRatio'])

metrics[item]['return_on_assets'] = float(keymetrics[0]['returnOnAssets'])



Simple right? In a few lines of code we have saved hours and hours of research and data gathering.

Other Blogs

Sep 11, 2023 - Rajnish Katharotiya

P/E Ratios Using Normalized Earnings

Price to Earnings is one of the key metrics use to value companies using multiples. The P/E ratio and other multiples are relative valuation metrics and they cannot be looked at in isolation. One of the problems with the P/E metric is the fact that if we are in the peak of a business cycle, earni...

blog post title

Sep 11, 2023 - Rajnish Katharotiya

What is Price To Earnings Ratio and How to Calculate it using Python

Price-to-Earnings ratio is a relative valuation tool. It is used by investors to find great companies at low prices. In this post, we will build a Python script to calculate Price Earnings Ratio for comparable companies. Photo by Skitterphoto on Pexels Price Earnings Ratio and Comparable Compa...

blog post title

Oct 17, 2023 - Davit Kirakosyan

VMware Stock Drops 12% as China May Hold Up the Broadcom Acquisition

Shares of VMware (NYSE:VMW) witnessed a sharp drop of 12% intra-day today due to rising concerns about China's review of the company's significant sale deal to Broadcom. Consequently, Broadcom's shares also saw a dip of around 4%. Even though there aren’t any apparent problems with the proposed solu...

blog post title


Financial Modeling Prep API provides real time stock price, company financial statements, major index prices, stock historical data, forex real time rate and cryptocurrencies. Financial Modeling Prep stock price API is in real time, the company reports can be found in quarter or annual format, and goes back 30 years in history.
2017-2023 © Financial Modeling Prep