Since we are working with currency, getting the price history is kinda important.
To follow me through this article, make sure that you:
- Understand basic Python code
- Have Python installed on your PC
Ok. Let's start.
First we need to define what exactly we are doing.
# Task 1: Get Top 10 currencies by market cap # Task 2: Get the trading history of the top 10 coins # Task 3: Make a cool chart of the history
These are the tasks.
Next condition is that we can only use CoinMarketCap.COM
This is the link to it's API documentation : https://coinmarketcap.com/api/#endpoint_listings
As you see (if you actually opened the link above), there are endpoints to get the current state of the coins, but not their history.
(At least that's how it is at the moment of writing this article...)
So... The API can't give us anything useful for our second task.
We need to look for another way...
Let's open https://coinmarketcap.com/ and take a look.
If we select Bitcoin, we arrive at this screen:
From this screenshot it is obvious that CoinMarketCap has the trading history and shows it to the visitors. Let's dig around and see how we could get that history...
Open your browser debugger ('Inspect Element' in Firefox) and go to the network resources.
Refresh the page
Do you see something interesting?
Look at this request - the one I marked on the screenshot.
It is a GET request to https://graphs2.coinmarketcap.com/currencies/bitcoin/
This is exactly what we needed for our task. It is perfect.
It provides us with BTC price, usd price, volume and market cap.
All of them are histories since the beginning of the coin (or to be exact - since the beginning of the coin's measurements)
Bitcoin's history starts at ~100 dollars, which is not the real beginning, but is the only one we have data for.
Also - each historical price is accompanied by a number, like:
If you find a epoch converter, you will see that 1435624460000 is "Tuesday, June 30, 2015".
So the price on June 30, 2015 was 256 $
Now let's start writing code!
import requests import json import matplotlib
These are the requirements we will be needing. Make sure to install matplotlib.
# https://coinmarketcap.com/api/#endpoint_listings # https://api.coinmarketcap.com/v2/listings/ # https://api.coinmarketcap.com/v2/ticker/ # Task 1: Get Top 10 currencies by market cap arr = json.loads( requests.get("https://api.coinmarketcap.com/v2/ticker/").content )["data"].values() top = sorted( arr, key=lambdai: i["circulating_supply"] * i["quotes"]["USD"]["price"], reverse=True )[:10]
I this first task we use the API /ticker/ endpoint to get the current price information of ALL cryptocurrencies.
Once that is done, we sort them by their market cap, which we calculate using the circulating_supply.
(The order will be different if we use the total_supply.)
The ordering is reversed, because we want biggest first.
Lastly, there is a [:10] which is just a Python trick, telling it to take the elements with indexes between 0 and 10
Now let's try the next part:
# https://graphs2.coinmarketcap.com/currencies/bitcoin/ # Task 2: Get the trading history of the top 10 coins historyOfTop =  for i in top: history = json.loads(requests.get( "https://graphs2.coinmarketcap.com/currencies/" + i["website_slug"] + "/" ).content) historyOfTop.append(history)
Here we iterate over the top 10 coins.
We make a request for each of them, using the unofficial API we found earlier.
Then we parse the content of the response as JSON and we add it to the collection of trading histories.
Note: This collection of trading histories does not contain any data about which coin it refers to. The only way to know which history is for which coin is to keep the indexes of 'top' and 'historyOfTop' aligned.
top is bitcoin
historyOfTop is history of bitcoin
# Task 3: Make a cool chart of the history traces =  index = 0 for i in historyOfTop: x_axis = map(lambda tpls: tpls, i["price_usd"]) y_axis = map(lambda tpls: tpls, i["price_usd"]) matplotlib.pyplot.plot(x_axis, y_axis, label=top[index]["website_slug"]) index = index + 1 matplotlib.pyplot.title("Prices USD") matplotlib.pyplot.legend() matplotlib.pyplot.show()
In the third part we draw all of the price lines in a single chart.
First we separate the X and Y.
X is the epoch timestamps.
Y is the price information
Then we plot it in matplotlib and use an 'index' to get the name of the currency from the 'top' collection
Once everything is done we show the chart.
I hope you liked this guide.