FMP

FMP

Enter

Profitability is a measure of relative profit; how much profit is made compared to total revenue. In our education section, we have two articles about profitabi

How to pull Financial Profitability Ratios using Python

-

twitterlinkedinfacebook
blog post cover photo

Image credit: Christopher Gower

Profitability is a measure of relative profit; how much profit is made compared to total revenue. In our education section, we have two articles about profitability. The first explains what profitability is and its importance. The second explains the ratios that measure profitability, how to calculate them, and where to find the right information. In this article, we will teach how to pull these ratios using the Financial Modeling Prep financial ratio API.

The 7 ratios that measure profitability are:

  1. Gross Profit Margin
  2. Operating Profit Margin
  3. Pretax Profit Margin
  4. Net Profit Margin
  5. Return on Assets
  6. Return on Equity
  7. Return on Capital Employed

How to get Started Pulling Data from the Financial Ratio API

Step 1: Load FMP Developer API Docs Website

From this article go to the top right of the page, click “Developers” and then click "API Docs" from the list below. Make sure you are logged into your account. You should see this in the top right corner:

image

Otherwise, click login and enter your account credentials.

A key piece of information that'll want to write down in a safely secured location is your API Key. This is located in the second section of the API docs page which is called "Your Details". See in the example below, it should be a long series of letters and numbers. As with any key, this unlocks something and in this case it ensures that you have permission to access the API you are requesting. Make sure to keep your key private and safely secured if you are storing it somewhere on your computer. It is against our terms and conditions to share your API key with anyone - your account will be blocked and investigated if we suspect you are doing this.

Step 2: Copy the URL for the API you want to access

For this example, we are going to pull from the company profile API. So scroll down to the section that says Company Financial Ratios (You can also use the navigation bar on the left hand side of the screen) and you will see the following

image

Then right click on the box that contains something that looks like a condensed URL. In this case: api/v3/ratios/APPL?limit=40

Step 3: Open Your Python Environment and copy URL

Now you'll need to open up your Python environment to start writing code. To start with, create a variable called “URL” and give the string version of the URL you copied in step 2. You can simply use Ctrl + V (or cmd + V for mac users) to paste the url you copied.

ERROR ALERT: Make sure to add inverted commas (otherwise called quotation marks) around the copied URL to make the URL variable a string.

url = “https://financialmodelingprep.com/api/v3/ratios/AAPL?limit=40&apikey=demo”

If you're copying directly from this page you'll need to make sure that you replace the ‘demo' with your API key

Step 4: Write the Python code to access the Financial Statement

To learn more about exactly what all the following code means and why it works, visit out guide - How to call a Financial Modeling Prep API

try: # For Python 3.0 and later from urllib.request import urlopen except ImportError: # Fall back to Python 2's urllib2 from urllib2 import urlopen import json def get_jsonparsed_data(url): """ Receive the content of ``url``, parse it as JSON and return the object. Parameters ---------- url : str Returns ------- dict """ response = urlopen(url) data = response.read().decode("utf-8") return json.loads(data) url = ("https://financialmodelingprep.com/api/v3/ratios/AAPL?limit=40&apikey=demo") Ratios_APPL = get_jsonparsed_data(url)

The variable that we have created called Ratios_APPL will have all the financial ratios from the last 40 years of Apple Inc's financial statements in the JSON format.

Step 5: Pull out the individual profitability ratios

The profitability ratios listed at the beginning of this article aren't named exactly the same way in the JSON because we want to keep the code clean and we can't use spaces in python when naming individual variables and objects. The JSON format stores the data as a list of dictionaries for each year. Each dictionary is made up of keys and values. In this case, each key is a financial ratio and the value is the figure for that ratio. The key name of each profitability ratio in the JSON file is as follows:

Profitability Ratio Key Name
Gross Profit Margin 'grossProfitMargin'
Operating Profit Margin 'operatingProfitMargin'
Pretax Profit Margin 'pretaxProfitMargin'
Net Profit Margin 'netProfitMargin'
Return on Assets 'returnOnAssets'
Return on Equity 'returnOnEquity'
Return on Capital Employed 'returnOnCapitalEmployed'

Let's say we want to pull out Apple's 2020 Net Profit Margin. We would use the following code:

Ratios_APPL[0]['netProfitMargin']

It's important to note that the format of this data means that as you index higher, you'll go further back in time. Here's a little bit of code that will help you understand how to index for a specific year:

for i in range(0,len(Ratios_APPL)): print(i, Ratios_APPL[i]['date'])

This will return a print out of each index and its corresponding date taken:

image

So using the index of 10 (Ratios_APPL[10]) will give you the ratios from 2010.

Here's one last block of code that will print out all the values of the ratios for a given year:

ProfitabilityRatioNames = [ 'grossProfitMargin', 'operatingProfitMargin', 'pretaxProfitMargin', 'netProfitMargin', 'returnOnAssets', 'returnOnEquity', 'returnOnCapitalEmployed', ] #This is a list of all the profitability ratios APPL_2020_Ratios = [] #This is an empty list of that the next for loop will fill for i in range(0,len(ProfitabilityRatioNames)): APPL_2020_Ratios.append(Ratios_APPL[0][ProfitabilityRatioNames[i]]) #This adds the ratio i to the list of Apple's ratios print(ProfitabilityRatioNames[i] , APPL_2020_Ratios[i] )

The output for this code is the following:

image

Other Blogs

Oct 31, 2023 8:03 AM - Parth Sanghvi

FCFF vs FCFE: What's the Difference?

Free cash flow to the firm (FCFF) and free cash flow to equity (FCFE) are two of the most important metrics used in financial modeling. Both metrics measure the amount of cash that is available to a company's shareholders and creditors, but there is a key difference between the two. FCFF measures...

blog post title

Nov 25, 2023 6:39 AM - Parth Sanghvi

DCF Valuation vs. Comparable Companies Analysis: Choosing the Right Valuation Method

Choosing the Right Valuation Method: DCF vs. Comparable Companies Analysis Introduction: Valuation methods play a pivotal role in determining the fair value of a company, aiding investors in making informed investment decisions. Two commonly used methods, DCF Valuation and Comparable Companies A...

blog post title

Dec 23, 2023 2:19 AM - Parth Sanghvi

Understanding the Limitations of DCF Analysis: A Guide to Overcoming Challenges

Introduction: Discounted Cash Flow (DCF) analysis stands as a cornerstone in valuing investments, yet its efficacy is contingent upon various assumptions and methodologies. While a powerful tool, DCF analysis comes with inherent limitations and challenges that investors must acknowledge to make i...

blog post title
FMP

FMP

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.
twitterlinkedinfacebookinstagram
2017-2024 © Financial Modeling Prep