top of page
  • Nikhil Adithyan

Extracting Crypto Historical & Intraday Data using Python

An easy way of extracting data using FinancialModelingPrep’s APIs



Introduction

Acquiring crypto or any kind of financial data using APIs can be a little tricky and exhausting in some cases. The process can sound a little overwhelming, from registering and accessing API keys to using the API endpoints. So in this article, I’m going to break down and simplify the entire process of using APIs to easily extract the market data of cryptocurrencies in Python.


But before actually extracting the data, it’s crucial to ensure the reliability and accuracy of the data. In this API era, it’s seriously tough to pinpoint the right API provider given the plethora of providers available in the market. Fortunately, I recently stumbled upon a great API provider with the name of FinancialModelingPrep (FMP). They offer a huge library of essential as well as rare-to-find API endpoints and more importantly, they ensure data reliability and accuracy.


Today, we’ll be using the API endpoints provided by FMP to extract the historical and intraday data of cryptocurrencies and use different methods to visualize the data. Without further ado, let’s dive into the article.


Importing Packages

The first and foremost step of setting up the coding environment is to import the required packages. In this article, we are going to use five different packages which are pandas for data manipulation, and requests for making API calls, and, mplfinance , altair, matplotlib for financial visualization. The following code will import all the mentioned packages into our Python environment:


# IMPORTING PACKAGES

import pandas as pd
import requests
import mplfinance as mf
import matplotlib.pyplot as plt
import altair as alt

plt.style.use('fivethirtyeight')
plt.rcParams['figure.figsize'] = (10,6)

If you haven’t installed any of the imported packages, make sure to do so using the pip command in your terminal.


Before moving further, for extracting the data, we’ll be using the APIs of FMP throughout the article. So for the smooth execution of the upcoming code blocks, you need to have an FMP developer account which you can easily create using the link here.


Extracting Historical Data

In this section, we will use the crypto historical data endpoint provided by FMP to attain Bitcoin’s price history. This can be easily done by using the following code:


# EXTRACTING HISTORICAL DATA

api_key = 'YOUR API KEY'
json = requests.get(f'https://financialmodelingprep.com/api/v3/historical-price-full/BTCUSD?apikey={api_key}').json()

hist_df = pd.DataFrame(json['historical'])
hist_df = hist_df.set_index('date').drop('label', axis = 1)
hist_df.index = pd.to_datetime(hist_df.index)
hist_df = hist_df.iloc[::-1]

hist_df.tail()

In the above code, we are first storing the secret API key in a variable (remember to replace YOUR API KEY with your actual secret key), and then, using the get provided by the Requests package, we are extracting the historical data of Bitcoin. We are then converting the JSON response into a Pandas dataframe, and after performing some data manipulation, this is the final output:



One thing that I truly love about this API is the variety of data that comes with it like the VWAP, change in price, change in percentage, and the change over time. Now that we are having a good amount of data to work with, we can visualize it to get some insights.


Visualizing Historical Data

Currently, there are so many options and packages existing for efficient visualization of financial data. To start, we will use the most traditional package, which is, Matplotlib.


plt.title('BTC Historical Prices 2018-2023')
plt.plot(hist_df.close, label = 'Close', alpha = 0.6)
plt.plot(hist_df.close.rolling(window = 50).mean(), label = 'SMA 50', linewidth = 2)
plt.plot(hist_df.close.rolling(window = 200).mean(), label = 'SMA 200', linewidth = 2)
plt.plot(hist_df.close.ewm(span = 50).mean(), label = 'EMA 50', linewidth = 2)
plt.legend()

This code will generate a basic plot with details about the historical closing price data, along with Simple Moving Averages with lookback periods of 50 and 200, and Exponential Moving Average with 50 as the lookback period. Here’s the generated graph:



The reason for plotting additional details like SMA and EMA is to visualize the trend of the prices. Though this plot does a pretty good job of visualizing the data, we can still improve it to look tremendously better. The following code uses the Altair package to create a beautiful graph.


alt.Chart(hist_df.reset_index(), title = 'BTC Historical Prices 2018-2023').mark_area(
    line={'color':'darkgreen'},
    color=alt.Gradient(
        gradient='linear',
        stops=[alt.GradientStop(color='white', offset=0),
               alt.GradientStop(color='darkgreen', offset=1)],
        x1=1,
        x2=1,
        y1=1,
        y2=0
    )
).encode(
    alt.X('date:T'),
    alt.Y('close:Q')
).properties(
    width = 800,
    height = 450
)
    

This code can be a little extensive when compared to that of Matplotlib’s and that’s because of the way Altair’s syntax is structured. But the result is a much more professional and beautiful graph that looks like this:



Extracting Intraday Data

One of the great things about FMP Intraday API is that it follows the exact method we used to extract the historical data. Here’s the code to extract the intraday data of Ethereum:


# EXTRACTING INTRADAY DATA

api_key = 'YOUR API KEY'
json = requests.get(f'https://financialmodelingprep.com/api/v3/historical-chart/5min/ETHUSD?apikey={api_key}').json()

intra_df = pd.DataFrame(json)
intra_df = intra_df.set_index('date')
intra_df.index = pd.to_datetime(intra_df.index)
intra_df = intra_df.iloc[::-1]

intra_df.tail()

As I said, the code is very similar to that we used to extract historical data but the only change happening here is in the API endpoint URL where we’re specifying the time interval between the data points (5 minutes in our case). But this number can be changed to 1 min, 15 mins, 30 mins, and so on based on your needs. This is the final output:



Just like how we visualized the historical data, we are going to try our hands on different methods to plot this extracted intraday data for deeper insights.


Visualizing Intraday Data

In this case, using Matplotlib is not the most preferred choice as its scope of visualizing intraday data is limited. That’s when mplfinance comes to the rescue. Basically, mplfinance is a sibling of Matplotlib focusing on financial visualizations. It helps in creating stunning visuals at lightning speed. Here’s an example of using the library to plot the Ethereum intraday data:


mf.plot(intra_df[:50], type = 'candle', style = 'yahoo')

When compared to other packages like Altair, the code required by mplfinance is very minimal and it’s straightforward. This one line of code generates this beautiful candlestick chart:



Pretty dope, right?! But we can take this even further. This library provides features for creating several different types of plots and a lot of customization options to craft good-looking visuals. Moreover, it also provides options to add more details to the chart like volume, moving averages, and custom indicators and statistics.


mf.plot(intra_df[:50], type = 'ohlc', volume = True, style = 'yahoo')
mf.plot(intra_df[:50], type = 'candle', volume = True, style = 'binance')
mf.plot(intra_df, type = 'renko', volume = True, style = 'charles')
mf.plot(intra_df, type = 'pnf', volume = True, style = 'default')

The above code creates four different types of visualizations, each utilizing different style themes. Here are the final results:



This is awesome! In seconds, we were able to create four different types of charts including OHLC, Candlestick, Renko, and Point-and-Figure. There are still a lot of features to be explored but that’s a topic for another day.


Closing Notes

In this article, we’ve dived deep into exploring how to use FMP’s API endpoints and extract historical as well as intraday data of cryptocurrencies. We also extended our process to visualize the attained data for insights using different libraries like Matplotlib, Altair, and mplfinance.


My personal experience of using these API endpoints by FMP was absolutely smooth and great. And more importantly, I loved the user experience offered by FMP. The APIs demanded less effort to process the JSON response and their documentation was extremely precise and on-point. So a huge shout out to FMP for creating a wonderful API platform that is easily accessible to everyone.


With that being, you’ve reached the end of the article. If you have any queries or suggestions for improvement, let me know in the comments. Thanks for reading!

0 comments

Comments


bottom of page