Automating Financial Ratio Calculations Using FMP’s Financial Ratios API | by Pranjal Saxena | Oct, 2024
This request fetches Nvidia’s key financial ratios. If everything is working, you’ll see a JSON output with data on ratios like P/E, debt-to-equity, and more.You can then convert it to pandas dataframe for further analysis.
Handling API Responses
The FMP API returns data in JSON format, which is easy to handle in Python. You can parse, filter, and structure this data to fit your analysis needs. For instance, to extract the latest P/E ratio, you can navigate through the JSON and retrieve specific values.
With this setup complete, you’re ready to explore how FMP’s API can automate financial ratios in your analysis. In the following section, we’ll look at how to use these API calls in a streamlined, automated process for real-time insights.
With the initial setup complete, it’s time to put FMP’s API to work by automating the retrieval and analysis of key financial ratios. Automating this process ensures timely insights without manual effort, letting you scale up your analysis quickly and accurately.
Example: Analyzing Profitability Ratios for a Tech Company
For this use case, let’s automate the retrieval of profitability ratios (like Gross Profit Margin and Return on Assets) for a selected tech company, Apple Inc. (AAPL), over the past few quarters. This will allow us to track its recent performance trends with minimal manual data handling.
Step 1: Fetch Ratio Data
Using the ratios endpoint, we can retrieve recent quarterly data for Apple:
import requestsapi_key = "YOUR_API_KEY"
symbol = "AAPL"
url = f"https://financialmodelingprep.com/api/v3/ratios/symbol?period=quarter&apikey=api_key"
response = requests.get(url)
data = response.json()
# Display a snapshot of the data
for quarter in data[:4]: # Fetching the most recent 4 quarters
print(f"Date: quarter['date']")
print(f"Gross Profit Margin: quarter['grossProfitMargin']")
print(f"Return on Assets: quarter['returnOnAssets']")
print("-" * 20)
This code snippet fetches quarterly data and displays two essential profitability ratios. You can adjust the data parsing based on the specific ratios you need.
Step 2: Automate with Scheduled Scripts
To keep your data up to date, you can automate this script to run periodically using a scheduler like cron (for Linux) or Task Scheduler (for Windows). This setup allows you to pull fresh data as it becomes available, so your analysis always reflects the latest trends.
Step 3: Visualize the Results
Once data is retrieved and stored, visualize it to make insights more accessible. Here’s an example of how you can plot the Gross Profit Margin and Return on Assets over time:
import matplotlib.pyplot as plt# Extract dates and profitability ratios
dates = [quarter['date'] for quarter in data[:4]]
gross_profit_margins = [quarter['grossProfitMargin'] for quarter in data[:4]]
roa = [quarter['returnOnAssets'] for quarter in data[:4]]
# Plot the data
plt.plot(dates, gross_profit_margins, label='Gross Profit Margin')
plt.plot(dates, roa, label='Return on Assets')
plt.xlabel("Date")
plt.ylabel("Ratio Value")
plt.title("Profitability Ratios for AAPL")
plt.legend()
plt.xticks(rotation=45)
plt.show()
link
