FMP
Oct 26, 2023 5:50 PM - Rajnish Katharotiya(Last modified: Dec 17, 2023 1:32 AM)
Image credit: Maxim Hopman
Making an investment decision is as hard as it is important. Using solid investment valuation ratios can help make the decision much easier. In a previous article, we explained what investment valuation is and 5 crucial investment valuation ratios to be familiar with. In this article, we simply explain how to pull these 5 investment valuation ratios from the FMP financial ratio API.
The four ratios we will investigate are:
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:
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
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 investment ratios
The debt 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 debt ratio in the JSON file is as follows:
Investment Valuation Ratio | Key Name |
---|---|
Price to Earnings Ratio | 'priceEarningsRatio' |
Price/earnings to growth ratio | 'priceEarningsToGrowthRatio' |
Price to sales ratio | 'priceToSalesRatio' |
Price to book value ratio | 'priceBookValueRatio' |
Let's say we want to pull out Apple's 2020 Price to Earnings. We would use the following code:
Ratios_APPL[0]['priceEarningsRatio']
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:
To be able to simply loop through all the investment valuation ratios it's useful to make a list with the codified names of each ratio. Here's how:
investmentRatios = [
'priceEarningsRatio',
'priceEarningsToGrowthRatio',
'priceToSalesRatio',
'priceBookValueRatio',
]
Here's a block of code that will give you the investment valuation ratios for a given year using the indexes shown above:
APPL_2020_Ratios = []
#This is an empty list of that the next for loop will fill
for i in range(0,len(investmentRatios)):
APPL_2020_Ratios.append(Ratios_APPL[0][investmentRatios[i]])
#This adds the ratio i to the list of Apple's ratios
print(f"{investmentRatios[i]} = {APPL_2020_Ratios[i]: .2f}")
In this example, we create a function so that you can easily use it to pull the investment valuation ratios for any stock ticker in any year:
def investmentRatio_ticker(ticker , yearindex):
url = f"https://financialmodelingprep.com/api/v3/ratios/{ticker}?limit=40&apikey=demo"
ticker_ratios = get_jsonparsed_data(url)
investmentRatios = [
'priceEarningsRatio',
'priceEarningsToGrowthRatio',
'priceToSalesRatio',
'priceBookValueRatio',
]
Ratios = []
#This is an empty list of that the next for loop will fill
for i in range(0,len(investmentRatios)):
Ratios.append(ticker_ratios[0][investmentRatios[i]])
#This adds the ratio i to the list of Apple's ratios
print(f"{investmentRatios[i]} = {Ratios[i]: .2f}")
As you can see above, in the cell after I created the function I called it to print out the Amazon investment Valuation ratios from 2020.
May 14, 2024 11:41 AM - Sanzhi Kobzhan
A stock's target price, also known as its fair value, is an indication of what a share can cost based on the company’s forecasted financial statements. It is important to know a stock's fair value to find undervalued stocks with great growth potential. Let's consider how investment analysts calculat...
May 24, 2024 9:30 AM - Rajnish Katharotiya
Earnings call transcripts are invaluable resources for investors, analysts, and financial enthusiasts. They provide insights into a company's performance, strategy, and future outlook, making them essential for making informed investment decisions. With Financial Modeling Prep, Earnings Call Transcr...
May 27, 2024 3:30 PM - Rajnish Katharotiya
In the ever-evolving world of technology, certain sectors have consistently demonstrated exceptional growth and innovation. The graphics processing units (GPUs) industry is one such sector, offering investors a golden opportunity for potentially high returns. In this blog, we'll delve into why inves...