Blogs

Getting Data From Financial Model Prep API

Here's some useful function to quickly get historical data from Financial Model Prep API. This api provides 5 years of historical prices for free. Many endpoints are paid but only free ones used here. Access to api requires key which can be obtained from the site for free. 

Import basic libraries:

import requests 
import pandas as pd

Here's the functions:

def get_stock_data(symbol,api_key):
    src = f"https://financialmodelingprep.com/api/v3/historical-price-full/{symbol}?apikey={api_key}"
    stockprices = requests.get(src).json()

    stockprices = stockprices['historical'][0:1200]

    stockprices = pd.DataFrame.from_dict(stockprices)
    stockprices = stockprices.set_index('date')
    
    return stockprices

def get_marketcap(symbol, api_key):
    link = f'https://financialmodelingprep.com/api/v3/market-capitalization/{symbol}?apikey={api_key}'
    marketcap = requests.get(link).json()
    return marketcap


def get_stocklist(api_key):
    link = f'https://financialmodelingprep.com/api/v3/stock/list?apikey={api_key}'
    stocklist = requests.get(link).json()
    stocklist = pd.DataFrame.from_dict(stocklist)
    return stocklist


def get_nasdaq100_components(api_key):
    link = f'https://financialmodelingprep.com/api/v3/nasdaq_constituent?apikey={api_key}'
    current_qqq_list = requests.get(link).json()
    current_qqq_list = pd.DataFrame.from_dict(current_qqq_list)
    return current_qqq_list

def get_historical_nasdaq100_changes(api_key):
    link = f'https://financialmodelingprep.com/api/v3/historical/nasdaq_constituent?apikey={api_key}'
    hist_qqq_changes = requests.get(link).json()
    hist_qqq_changes = pd.DataFrame.from_dict(hist_qqq_changes)
    return hist_qqq_changes