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!
- 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
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.
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?
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.
# 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)
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)
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’.
|time||high||low||open||volume from||volume to||close|
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
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. Eligibility to trade in digital asset products may vary based on jurisdiction.