avatarBenjamin Ja

Summary

The article provides a beginner's guide to using the Financial Modeling Prep (FMP) API in Python to access financial data such as historical stock prices and financial statements of public companies.

Abstract

The Financial Modeling Prep (FMP) API is a powerful tool for accessing a wide range of financial data, including historical stock and currency data, as well as financial statements from public companies. This article serves as a simple tutorial for Python beginners, detailing how to set up and use the FMP API to retrieve and manipulate this data. It outlines the process of obtaining an API key, setting up a Python function to access JSON-formatted data, and provides examples of how to pull historical stock data and financial statements. The tutorial emphasizes the ease of use of the API and its potential utility for those working in finance, economics, or statistics.

Opinions

  • The author considers FMP's API to be the most accurate financial data API available.
  • The article positions the FMP API as an essential resource for individuals working with financial data.
  • The author suggests that the tutorial will be particularly useful for those who are new to Python and want to quickly leverage the FMP API.
  • The author highlights the affordability of the API, with a basic version available for free and advanced plans ranging from 19 to 99 per month.
  • The article implies that the FMP API's JSON-formatted data is straightforward to work with in Python, especially when using additional libraries for data manipulation.
  • The author encourages readers to engage with the content by suggesting they use the provided code examples and invites feedback and questions in the comments section.

A simple guide how to use the Financial Modeling Prep API in Python

Financial Modeling Prep (FMP) is a website providing stock markets information and provide a powerful API that can be easily used in Python. To make the use of FMP’s API easier, this article provides an easy step-by-step tutorial for beginners.

Good data is crucial if you work in finance, economics or statistics. Finacial Modeling Prep or FMP provides an excellent API that is (according to their website) “the most accurate financial data API out there”. With this API, you can access historical stock and currency data, financial statements of many public companies and much more. The basic API is free and pricing for advanced plans range from $19 to $99 per month.

When you sign up, you will receive your personal API key that you can use for any project. If you have just started with Python, this article will be very useful to quickly use the FMP API.

#1: Set Up

The data of the FMP API is stored in JSON format that can be easily read with Python. In a first step, you have to set up a function that lets you access the data from your API. The function lets you insert the URL and returns data you can work with in Python.

Please note that you also have to import other libraries to work with the data further.

import urllib, json
import requests
from urllib.request import urlopen

# Function to get the Data

def get_jsonparsed_data(url):
    res = urlopen(url)
    data = res.read().decode("utf-8")
    return json.loads(data)

#2: Use it — Historical Stock data

In the following I want to show how to use this function and how to get the data you need. With this Link you can look for the specific links you need for your project. First, I want to show how to get historical stock data you can work with in Python.

df = get_jsonparsed_data(f"https://financialmodelingprep.com/api/v3/historical-chart/5min/AAPL?from=2023-10-10&to=2023-08-10&apikey=YOURKEY")
df1 = pd.DataFrame(df)
df1

If you enter your personal key at the end of the link you will get 5-min historical stock data between 8 and 10 October 2023 from Apple. You can also change the company, by changing the stock ticker in the URL (for instance KO for Coca-Cola). Also, you can have 1min or 15min stock price intervals (you just have to use “1min” instead of “5min” and by changeing the dates in the URL, you can change the period.

Df1 would be a dataframe with the dates in the first column besides the open, low, high and close price and the volume. It will be useful to set the first column as the index. Here is a quick guide how to do so.

#3: Financial Statements

With FMP you can also get the data from the financial statements of public companies. Here is an example how to get a public company’s net income via the FMP API. With the link below you can get the income statement of OMV (traded in Vienna, therefore the ticker OMV.VI).

inst = get_jsonparsed_data(f"https://financialmodelingprep.com/api/v3/income-statement/{'OMV.VI'}?period=quarter&limit=50&apikey=YOURKEY")

With this URL you can get the financial statement of OMV for the last 50 quarters. “period=quarter” gives you the the quarterly income statements.

You can copy the link into your browser to see all the information given in the income statement. It is also possible to type “inst” + press enter to see what data is stored in this URL.

If you want to retrieve the company’s historical net income data, you can use a code similar to this one:

df1 = pd.DataFrame(inst)

df1 = df1.set_index(['date'])

df1['netIncome']

The first line of code converts the data into a data frame. In the second line, the date column is defined as an index, and in the third line you receive the data for the net income.

This article gives you a brief introduction to using the FMP API for Python. FMP also has a documentation website that explains how to access the information via the API.

I hope this article was helpful. If you have any questions or find any errors, please let me know in the comments.

Financial Modeling
API
Fmp
Python
Tutorial
Recommended from ReadMedium