How to Download Historical Cryptocurrency Price Data from Coingecko with Python
Build your blockchain idea with cryptocurrency price data by learning how to download and visualize historical cryptocurrency price data using Python and CoinGecko.
Posted by
Jake MangerBuild your blockchain idea with cryptocurrency price data
Cryptocurrencies have skyrocketed in price since their inception. Bitcoin, the world’s first cryptocurrency, reached an all-time high of $65,000 (November, 2021) coming from as little as $222 in January, 2015, or 0.02% of the price of a pizza in 2010.
Seriously, 0.02% of the price of a pizza. Lazlo Hanyecz bought two pizzas in 2010 for 10,000 BTC, which at near peak prices, is two pizzas for $650 million USD. Other, more recent cryptocurrencies such as Ethereum, Solana and Dogecoin have had similarly ludicrous increases in value.
If you’re like me, you may have thought about how you can use cryptocurrency data to build a new blockchain-related project. Let’s say a product that tracks cryptocurrency prices. As a first step on this journey, I thought it would be fun to see how I could download some historical price data using Python and plot it on my local computer.
Finding an API
For this task, we’ll need an API (an application programming interface). This allows two programs to talk to each other, such as your Python program and the program of a data provider, like a cryptocurrency exchange.
Looking online, Binance is by far the most used cryptocurrency exchange with a 24h trading volume of $17 Billion USD and 1388 trading pairs according to CoinGecko.
However, Binance requires a long identity verification process before you can access their data.
Therefore, it’s probably best we go with a simpler data provider like Coingecko for this process.
Setting up the Python Project
Before we start downloading data from Coingecko, we’ll need to set up our Python environment. I’ve covered this in more detail at Getting Started with Python Virtual Environments. But briefly, to do this, we’ll need to:
- Have Python 3 installed. If you don’t already, visit python.org for installation instructions.
- Open your terminal or command line and create a new project folder:
mkdir crypto_data
- Change directory to that new project folder:
cd crypto_data
- Create a new Python virtual environment:
python3 -m venv venv
Note, `python3` is your installation of python. If the version of python you installed is called `python`, `python3.7` or `python3.9` or anything else, then use that.
- Activate your python virtual environment:
- On Mac or Linux:
source venv/bin/activate
- On Windows:
venv\Scripts\activate
- On Mac or Linux:
If you got stuck anywhere here, check out my past article that should make you a pro at setting up python with virtual environments here.
Installing dependencies
To download and use the Coingecko API, we’ll need to install the pycoingecko package. And, to visualize our data, we’ll download the matplotlib package. We can do this by running the following command in our terminal or command prompt:
pip install pycoingecko matplotlib
Getting and plotting data
Now that we have our environment setup, it’s time to start collecting data. First, we’ll import the packages we installed into our Python script. Then, we’ll use the CoinGecko API to retrieve historical cryptocurrency price data. Finally, we’ll use matplotlib to visualize the data.
Here’s a sample code to retrieve Bitcoin price data from CoinGecko and plot it. Create a file called plot_btc.py
and add this code to it using your favourite text editor (e.g. Visual Studio Code, Vim or Notepad… if you’re desperate).
import pycoingecko
import matplotlib.pyplot as plt
import datetime
# Initialize CoinGecko API client
coinGecko = pycoingecko.CoinGeckoAPI()
# Get historical price data for Bitcoin
btc_data = coinGecko.get_coin_market_chart_by_id('bitcoin', 'usd', '365days')
# Extract the dates and prices from the data
dates = [data[0] for data in btc_data['prices']]
# convert unix timestamp to datetime
dates = [datetime.datetime.fromtimestamp(date/1000) for date in dates]
prices = [data[1] for data in btc_data['prices']]
# Plot the data\nplt.plot(dates, prices)
plt.xlabel('Date')
plt.ylabel('Price (USD)')
plt.title('Historical Bitcoin Price (USD)')
plt.show()
This code will retrieve a year’s worth of daily data for Bitcoin in USD and plot the price over time. With this data, we can try to explore the patterns and trends that drive the price of cryptocurrencies.
Now, let’s run this code with the following command:
python plot_btc.py
and we should see the following beautiful plot!
Congratulations! You just became an amateur cryptocurrency data analyst and developer! You can now use this data for whatever purpose you imagined.
Explore More with Chainspy
If you’re interested in more than just cryptocurrency prices, consider checking out Chainspy. We aim to make blockchain data transparent and accessible through simple, user-friendly charts and APIs. Our site offers information on transaction speeds, usage, and applications of various cryptocurrencies, providing you with a comprehensive view of what’s happening on blockchains.
If you’re interested in exploring more, start spying by clicking the button below.
Start spying