10 Best Resources to Fetch Cryptocurrency Data in Python

A list of all useful python packages and APIs to pull cryptocurrency data seamlessly in python



Introduction


In recent days, the cryptocurrency industry has not only attracted investors or enterprises but also tons and tons of developers around the world given the response from the public. After the momentum gained by the release of Bitcoin and Ethereum tokens, every aspiring developer around the world started working to create their own cryptocurrencies which then be auctioned off within an Initial Coin Offering (ICO) and traded among stock traders. This new idea of money-minting captivated software developers to build an extensive amount of projects.

The trend even follows today without any disruption but the way to develop crypto-based products with the use of cryptocurrency market data has opened a whole new chapter. There are tons of resources to pull cryptocurrency data but reliability is the most important factor to consider. In this article, we are going to see the top 10 reliable resources from which cryptocurrency data can be pulled off with my buddy python.


Reliable Resources


The factors I considered to find the top 10 resources are reliable data, ease to use, and finally good documentation. While considering these factors, this is the final list I arrived at (Note: I’m not ranking but just listing the top 10 useful resources):



- IEX Cloud
- Alpha Vantage
- Pandas DataReader
- Yfinance
- Cryptocompare
- Fastquant
- Twelve Data
- Polygon.io
- Coinlayer
- Messari


Using these useful resources, we are going to extract the historical market data of various cryptocurrencies in python. These resources include crypto APIs and python packages. Whenever crypto APIs are used, make sure that you have a developer account in the respective data provider, only then, you will be able to access your API key which is crucial to pull data from an API.


1. IEX Cloud


IEX Cloud provides an extensive amount of API endpoints which include market data of stocks, commodities, forex, options, and cryptocurrency. The data is reliable and it is easy to use their API in any programming languages. The API comes in with highly customizable parameters which can help attain the desired information. One thing I truly appreciate about IEX Cloud is the amount of work and time they invested in creating wonderful documentation. Let’s use the crypto API provided by IEX Cloud to pull the historical data of Bitcoin in python.


Unfortunately, the API to pull historical data comes under the premium category. So, I decided to use the ‘Price’ endpoint provided by IEX Cloud to pull the latest price of a given cryptocurrency.


Python Implementation:



# 1. IEX Cloud

import requests
import pandas as pd

def get_crypto_price(symbol):
    api_key = 'pk_32ef64b2003542b6a829b8f94831c789'
    api_url = f'https://cloud.iexapis.com/stable/crypto/{symbol}/price?token={api_key}'
    raw = requests.get(api_url).json()
    price = raw['price']
    return float(price)

btc = get_crypto_price('btcusd')
print('Price of 1 Bitcoin: {} USD'.format(btc))


Output:



Price of 1 Bitcoin: 54700.07 USD 


As you can see, it just requires a few lines of code to extract the latest crypto information. If you are a paid user of IEX Cloud, you can try using the ‘Quote’ endpoint to extract the historical market data of a given cryptocurrency. Let’s jump on to the next useful resource.


2. Alpha Vantage


When Yahoo Finance API stopped functioning, a new player with the name of Alpha Vantage gained momentum. It is one of the earliest data providers and acted as a wonderful alternative to Yahoo Finance API in the early days. The usage of Alpha Vantage has not reduced up to this day. They provide thousands of API endpoints for all kinds of data and the best part is most of the data are free to use up to a certain limit. Even though the documentation is not as fancy as IEX Cloud, they have a great one with lots and lots of examples. Let’s pull some data!


Python Implementation:



# 2. Alpha Vantage

import requests
import pandas as pd

def get_crypto_price(symbol, exchange, start_date = None):
    api_key = '3J9RETSY6U1F1DCG'
    api_url = f'https://www.alphavantage.co/query?function=DIGITAL_CURRENCY_DAILY&symbol={symbol}&market={exchange}&apikey={api_key}'
    raw_df = requests.get(api_url).json()
    df = pd.DataFrame(raw_df['Time Series (Digital Currency Daily)']).T
    df = df.rename(columns = {'1a. open (USD)': 'open', '2a. high (USD)': 'high', '3a. low (USD)': 'low', '4a. close (USD)': 'close', '5. volume': 'volume'})
    for i in df.columns:
        df[i] = df[i].astype(float)
    df.index = pd.to_datetime(df.index)
    df = df.iloc[::-1].drop(['1b. open (USD)', '2b. high (USD)', '3b. low (USD)', '4b. close (USD)', '6. market cap (USD)'], axis = 1)
    if start_date:
        df = df[df.index >= start_date]
    return df

btc = get_crypto_price(symbol = 'BTC', exchange = 'USD', start_date = '2020-01-01')
btc


Output:



The response of the API is way cleaner which helps in reducing the process of doing data cleaning and manipulations. Also, the data being provided is very reliable. Let’s explore the next useful resource.


3. Pandas DataReader


Pandas DataReader is a sub-package of Pandas in python. This package extracts historical from the Yahoo Finance API for the given stock or cryptocurrency symbol. Let’s pull some data


Python Implementation:



# 3. Pandas Datareader

#!pip install pandas_datareader
import pandas_datareader as web
import datetime as dt

start = dt.datetime(2020,1,1)
end = dt.datetime.now()

ltc = web.DataReader('LTC-USD', 'yahoo', start, end)
ltc


Output:



This resource is best for beginners as it doesn’t need any API key to pull data or to perform any data processing or data manipulations. The data extracted is also almost similar to the original market data. The only thing that bothers me about Pandas DataReader is their documentation. It looks neat but the way things are ordered seems confusing and it is also necessary to search manually for the functions they provide whereas the documentation of IEX Cloud or Alpha Vantage lists the functions so that new programmers can get an idea of what they are actually offering. But, this problem doesn't affect the resource in any way given the number of tutorials available on the internet.


4. YFinance


Like Pandas DataReader, YFinance is a python package used to extract all kinds of market data. Using YFinance, one can not only extract historical data but other related data like a given symbol’s profile, and so on. Now let’s extract the historical data of Ethereum (ETH).


Python Implementation:



# 4. YFinance

#!pip install yfinance
import yfinance as yf
import datetime as dt

start = dt.datetime(2020,1,1)
end = dt.datetime.now()

eth = yf.download('ETH', start, end)
eth


Output:



From the code, we can observe that YFinance not only works seamlessly but also helps beginners to extract reliable data in just a few lines of code. Like how Pandas DataReader doesn’t have such good documentation, YFinance is no exception. In fact, much worse. They just have one tiny documentation about the primary functions on PyPi which is also confusing. With the help of great tutorials on YouTube, this problem isn't a hectic one to tackle with. Let’s move on to the next resource.

5. CrytpoCompare


CryptoCompare allows users to extract various kinds of cryptocurrency data using its highly customizable APIs. Users have access to tons and tons of API endpoints including different types of blockchain data. Most of the users fall in love with CryptoCompare due to its user-friendly website and documentation where the desired API URL can be generated and customized. It’s time to extract some market data using CryptoCompare’s API in python.

Python Implementation:



# 5. Cryptocompare

import requests
import pandas as pd

def get_crypto_price(symbol, exchange, days):
    api_key = '0d615dafef20e86cc7dc1579f6e31340ca344b0926ac7b71edade3f0b534c3c1'
    api_url = f'https://min-api.cryptocompare.com/data/v2/histoday?fsym={symbol}&tsym={exchange}&limit={days}&api_key={api_key}'
    raw = requests.get(api_url).json()
    df = pd.DataFrame(raw['Data']['Data'])[['time', 'high', 'low', 'open']].set_index('time')
    df.index = pd.to_datetime(df.index, unit = 's')
    return df

ada = get_crypto_price('ADA', 'USD', 365)
ada


Output:



Beginners can also try their hands on experimenting with CryptoCompare’s APIs even though it contains tasks like API calls as the documentation is so user-friendly and the response from the API is so clean that only a small amount of data processing is required. Speaking about data reliability, CryptoCompare’s data is highly reliable which can be used to conduct extensive technical analysis and research.


6. Fastquant


Fastquant is a powerful python package that mainly focuses on the area of backtesting trading strategies but also provides reliable cryptocurrency data with its 'get_crypto_data()' function. This function will return the given cryptocurrency’s historical market data for a specified period of time. Using Fastquant’s powerful function, let’s get some historical data with python.


Python Implementation:



# 6. Fastquant

#!pip install fastquant
import fastquant as fq

start = '2020-01-01'
end = '2021-03-27'

dot = fq.get_crypto_data('DOT/USDT', start, end)
dot


Output:



There are two drawbacks to Fastquant. First is the lack of customizable features. While using crypto APIs like CryptoCompare, we have access to a wide range of cryptocurrency data whereas, we could only pull historical data of cryptocurrencies using Fastquant. This problem is not only associated with Fastquant but also in Pandas DataReader. Secondly, Fastquant’s documentation is poor when compared to the rest. We could only find brief documentation about its functions in its GitHub repository and PyPi but nothing else. As I said before, with the increasing number of video tutorials on youtube, the second problem is foregone.


7. Twelve Data


At present, Twelve Data is one of the leading market data providers having an enormous amount of API endpoints for all types of market data. It is very easy to interact with the APIs provided by Twelve Data and has one of the best documentation ever. It is a great resource to start with if you want to learn to extract crypto data with APIs. Time to extract some data with python!


Python Implementation:



# 7. Twelve data

import requests
import pandas as pd

def get_crypto_price(symbol, interval, days):
    api_key = 'ace6a59ff46446659af4e691fab88e22'
    api_url = f'https://api.twelvedata.com/time_series?symbol={symbol}&exchange=Binance&interval={interval}&outputsize={days}&apikey={api_key}'
    raw = requests.get(api_url).json()
    df = pd.DataFrame(raw['values']).set_index('datetime')
    df = df.iloc[::-1]
    return df

xrp = get_crypto_price('XRP/USD', '1day', 450)
xrp


Output:



From all the resources I tested, Twelve Data API’s response (raw JSON data) is one of the cleanest when compared to the rest. This ultimately helped me from performing an extensive amount of work to clean and process the data. One of the greatest things about Twelve Data’s documentation is the number of examples that come along with each and every API endpoint.


8. Polygon.io


One of the best things about Polygon.io is the frequent updation of market data. The data gets updated on a real-time basis and provides one of the most accurate market data. Like Twelve Data, this platform also has an endless amount of APIs with wonderful documentation (comprising tons and tons of examples). Let’s do some data extraction with python!


Python Implementation:



# 8. Polygon.io

import requests
import pandas as pd

def get_crypto_prices(symbol, start, end):
    api_key = 'R3z00248bKFa02l1pomndRMylXeLVPkw'
    api_url = f'https://api.polygon.io/v2/aggs/ticker/X:{symbol}USD/range/1/day/{start}/{end}?unadjusted=true&sort=asc&apiKey={api_key}'
    raw = requests.get(api_url).json()
    df = pd.DataFrame(raw['results']).set_index('t')[['o', 'h', 'l', 'c', 'v']]
    df = df.rename(columns = {'o':'open', 'h':'high', 'l':'low', 'c':'close', 'v':'volume'})
    df.index = pd.to_datetime(df.index, unit = 'ms')
    return df

bch = get_crypto_prices('BCH', '2020-01-01', '2021-04-26')
bch


The limitations of Polygon.io are the lack of exchanges and the non-existence of technical indicators. Firstly, lack of exchanges. While most of the resources support international markets, Polygon.io supports market data for securities present only in the United States. Next is the non-existence of technical indicators. Technical indicators are an important aspect of technical analysis and research but, it is not provided by Polygon.io. These limitations have nothing to do with cryptocurrency but are facts that need to be considered.


9. Coinlayer