FMP

FMP

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 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'https://financialmodelingprep.com/api/v3/stock-screener?sector=technology&marketCapMoreThan=100000000000&limit=100&apikey={demo}')

companies = companies.json()

technological_companies = []

for item in companies:

technological_companies.append(item['symbol'])

print(technological_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:

try:

metrics[item] = {}

keymetrics = requests.get(f'https://financialmodelingprep.com/api/v3/key-metrics/{item}?limit=40&apikey={demo}')

keymetrics = keymetrics.json()

keymetrics[0]

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'])

except:

pass

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