Download Cryptocurrency Data in Python by Using Crypto Compare API

How to use Cryptocompare API to download cryptocurrency data in Python? Cryptocompare is a cryptocurrency market data provider. And this blog explains how you can go about it with super easy simple steps and readies you to apply it whenever you want!

If you are taking your first step toward Algorithmic Trading or within the cryptocurrency world, then this article is for you!

We cover:

  • Why CryptoCompare?
  • Import Libraries
  • Set API Key
  • Fetch All Cryptocurrency Tickers
  • Download Historical Data With a Specific Frequency
  • Download Bitcoin Hourly Historical Data
  • Plot the Data

Why CryptoCompare?

CryptoCompare allows users to get live prices, graph them and make market research analysis from most of the important crypto exchanges. It also allows us to get limited free data for downloading. That’s what we’re going to do in this article.


Import Libraries

First, let’s install the cryptocompare library in our Jupyter notebook:

Now, we are going to import the following libraries: cryptocompare, pandas, datetime (only its datetime method) and matplotlib.

Are you familiar with the last two code lines of the ‘matplotlib’ library? Don’t worry at all! Please check our great article on the ‘matplotlib’ library to know everything about it.


Set API Key

The API Key is used to have a valid authentication for the user. It serves to identify your account and access an automated program.

To download the data, you will need to authenticate yourself on cryptocompare.com. First, you write your ‘cryptocompare_API_key’ and then you set the API key to the market provider. As the following:


Fetch All Cryptocurrency Tickers

You can do this easily. First, you get the ticker list, which is a dictionary. Then you transform the dictionary into a dataframe. Super easy, right?

tickerIdUrlImageUrlContentCreatedOnName
LIBERA949640/coins/libera/overview/media/40484629/libera.png1656515116LIBERA
MINIFOOTBALL949643/coins/minifootball/overview/media/40484630/minifootball.png1656515517MINIFOOTBALL
CPO949646/coins/cpo/overview/media/40484632/cpo.png1656516143CPO
CPAD949648/coins/cpad/overview/media/40484634/cpad.png1656516578CPAD
SCO949651/coins/sco/overview/media/40484635/sco.png1656517570SCO

Download Historical Data With a Specific Frequency

Contrary to the yahoo finance library, in cryptocompare.com you have for each frequency a specific method to download data. Let’s explain to you this:

You can fetch the historical data for the ticker symbol you want. The historical data in daily, hourly and minute frequency levels are available.

Syntax:

# For daily data
cryptocompare.get_historical_price_day(ticker_symbol, currency, limit=limit_value, exchange=exchange_name, toTs=data_before_timestamp)

# For hourly data
cryptocompare.get_historical_price_hour(ticker_symbol, currency, limit=limit_value, exchange=exchange_name, toTs=data_before_timestamp)

# For minute data
cryptocompare.get_historical_price_minute(ticker_symbol, currency, limit=limit_value, exchange=exchange_name, toTs=data_before_timestamp)

Where:
ticker_symbol: Ticker symbol whose data is required
currency: Currency in which the price is quoted
limit_value: The maximum number of bars to fetch (max. value is 2000)
exchange_name: The exchange to use when fetching the data
data_before_timestamp: Return the data before this timestamp (UNIX epoch time or a datetime object)

Returns:
A dictionary containing the historical data.


Download Bitcoin Hourly Historical Data

For illustration purposes, let’s extract the hourly historical data for the ‘BTC’ before 01-May-2021.

First, you define the ticker symbol, and then you specify that the asset price will be in dollars. Besides, since Cryptocompare establishes a limit to download data, we set the limit number of data points to return as ‘limit_value’.

We choose the ‘CCCAGG’ as the crypto exchange, which means we are going to download the aggregate prices that Cryptocompare has.

Next, we are going to use the ‘get_historical_hour’ function to download hourly Bitcoin prices.

Then, since the download information comes as a dictionary type, we transform it into a dataframe. In addition, we set the ‘time’ column as our index. Finally, the method provides the ‘time’ column as seconds, that’s why we not only change its string type to a datetime type, but also specify the ‘unit’ argument as ‘s’.

timehighlowopenvolume fromvolume toclose
06/02/2021 21:004029627400924740141361264945.08675E+134009247
06/02/2021 22:004050397399919640092471688236.79449E+134004380
06/02/2021 23:004004419391321740043804298181.69581E+143926701
07/02/2021 0:003965158388027139267013982991.5625E+143960394
07/02/2021 1:003963317385573839603942625881.02679E+143870644

Visit QuantInsti Blog to learn how to plot the data: https://blog.quantinsti.com/download-cryptocurrency-data-python-cryptocompare-api/.

Disclosure: Interactive Brokers

Information posted on IBKR Traders’ Insight that is provided by third-parties and not by Interactive Brokers does NOT constitute a recommendation by Interactive Brokers that you should contract for the services of that third party. Third-party participants who contribute to IBKR Traders’ Insight are independent of Interactive Brokers and Interactive Brokers does not make any representations or warranties concerning the services offered, their past or future performance, or the accuracy of the information provided by the third party. Past performance is no guarantee of future results.

This material is from QuantInsti and is being posted with permission from QuantInsti. The views expressed in this material are solely those of the author and/or QuantInsti and IBKR is not endorsing or recommending any investment or trading discussed in the material. This material is not and should not be construed as an offer to sell or the solicitation of an offer to buy any security. To the extent that this material discusses general market activity, industry or sector trends or other broad based economic or political conditions, it should not be construed as research or investment advice. To the extent that it includes references to specific securities, commodities, currencies, or other instruments, those references do not constitute a recommendation to buy, sell or hold such security. This material does not and is not intended to take into account the particular financial conditions, investment objectives or requirements of individual customers. Before acting on this material, you should consider whether it is suitable for your particular circumstances and, as necessary, seek professional advice.

In accordance with EU regulation: The statements in this document shall not be considered as an objective or independent explanation of the matters. Please note that this document (a) has not been prepared in accordance with legal requirements designed to promote the independence of investment research, and (b) is not subject to any prohibition on dealing ahead of the dissemination or publication of investment research.

Any trading symbols displayed are for illustrative purposes only and are not intended to portray recommendations.

Disclosure: Digital Assets

INTERACTIVE BROKERS LLC IS A MEMBER OF NFA AND IS SUBJECT TO NFA’S REGULATORY OVERSIGHT AND EXAMINATIONS. HOWEVER, YOU SHOULD BE AWARE THAT NFA DOES NOT HAVE REGULATORY OVERSIGHT AUTHORITY OVER UNDERLYING OR SPOT VIRTUAL CURRENCY PRODUCTS OR TRANSACTIONS OR VIRTUAL CURRENCY EXCHANGES, CUSTODIANS OR MARKETS.

Trading in digital assets, including cryptocurrencies, is especially risky and is only for individuals with a high risk tolerance and the financial ability to sustain losses. For more information about the risks surrounding the trading of Digital Assets please see the “Disclosure of Risks of Trading Digital Assets“.