Importing Altcoin Data with Python and The CoinMarketCap API

With a new handful of ICO’s happening every week, the cryptospace has become awash with altcoins, making it increasingly hard to keep up with hundreds of cryptocurrencies. The magnitude of cryptocurrencies makes investing in altcoins fairly difficult, forcing investors to base their assumptions on speculation. As an attempt to alleviate this, I’m going to write a series of guides on how to analyze altcoins with Python and CoinMarketCap’s API.

Please Note: I am assuming users are using macOS, have pip (or preferably Anaconda) installed, and are using python 3.X. If you need help with pip, I suggest reading this article. Any IDE should work just fine!

Installing The CoinMarketCap Package

Luckily, pip makes it easy to install the tool we will be using for this project. Open a terminal window and execute the following:

pip install coinmarketcap

You may also need to install json and pandas if you don’t have a package manager that does this for you (I recommend Anaconda) or haven’t already done so manually.

Connecting with CoinMarketCap

The package we just installed is fairly simple to use. Start by importing it. We will also import json and pandas for later on.

import coinmarketcap
import json
import pandas as pd
import time

Now we connect to the API, using the Market class. We also choose a coin to grab data about by placing the coin’s name in the .ticker() function. I’m using “ethereum” for this example. If you leave .ticker() without an argument, it will grab data from all coins.

market = coinmarketcap.Market()
coin = market.ticker("ethereum")

At this point, we have data coming in. If we print our variable coin we get the following:

b'[\n    {\n        "id": "ethereum", \n        "name": "Ethereum",
 \n        "symbol": "ETH", \n        "rank": "2", \n        "price_usd": "187.533",
 \n        "price_btc": "0.0821642", \n        "24h_volume_usd": "511438000.0",
 \n        "market_cap_usd": "17259673824.0", \n        "available_supply": "92035395.0",
 \n        "total_supply": "92035395.0", \n        "percent_change_1h": "1.43", \n
        "percent_change_24h": "13.67", \n        "percent_change_7d": "1.59", \n        
"last_updated": "1496082265"\n    }\n]'

Making the JSON Readable

We can tell there’s information in there, but not easy to read with the human eye. To beautify this code we can do the following:

parsedCoin = json.loads(coin)
print(json.dumps(parsedCoin, indent=4, sort_keys=True))

And we get this well-formatted JSON data.

[
    {
        "24h_volume_usd": "515936000.0",
        "available_supply": "92035395.0",
        "id": "ethereum",
        "last_updated": "1496082864",
        "market_cap_usd": "17317103911.0",
        "name": "Ethereum",
        "percent_change_1h": "1.77",
        "percent_change_24h": "14.33",
        "percent_change_7d": "2.08",
        "price_btc": "0.0823298",
        "price_usd": "188.157",
        "rank": "2",
        "symbol": "ETH",
        "total_supply": "92035395.0"
    }
]

Aggregating Multiple Altcoins

Now it’s time to get the data into a usable data structure. I’m going to use a DataFrame from the pandas package. To save lines of code we can start pulling in data and parsing it at the same time. Each coin you do this with should look something like the last line:

#market.ticker("ethereum") -> connects to the API to get Ethereum information
#json.loads()[0] -> parses this information
#pd.Series() -> converts the dictionary into a pandas Series

ether = pd.Series(json.loads(market.ticker("ethereum"))[0])

You can set several variables equal to different coins that you’re interested in analyzing. We then place all the Series into a pandas DateFrame. For example, if I want to analyze bitcoin, ether, and wowcoin:

ether = pd.Series(json.loads(market.ticker("ethereum"))[0])
wowcoin = pd.Series(json.loads(market.ticker("wowcoin"))[0])
bitcoin = pd.Series(json.loads(market.ticker("bitcoin"))[0])

coinArray = pd.DataFrame([ether,bitcoin,wowcoin]).set_index("id")

Now the coins are aggregated into a single data type for your analysis! A printout of the information looks something like this:

Why not grab ’em all?

As mentioned above, if you leave the .ticker() method without an argument, the API defaults to retrieving information for all of the cryptocurrencies. Since many of the lower market cap coins have missing information, I would suggest selecting just the top 100 or so. Moreover, we can begin storing snapshots of information in csv files with timestamps on them on a timer. We can leave this script running in the background to collect data.

#this grabs all of the coin data available
coins = json.loads(market.ticker())

for i in range(96):
    #this creates a dataframe with the top 100 coins
    coinArray = pd.DataFrame([pd.Series(coins[i]) for i in range(100)]).set_index('id')

    #timestamps and stores the csv file
    location = 'Data/'+str(time.time())+'.csv'
    coinArray.to_csv(location)

    #waits an hour until collecting data again
    time.sleep(3600)

This code grabs the top 100 coins’ information every hour for 4 days and stores the information in a csv file, in a folder labeled ‘Data.’

Next Steps

Once we have the data, we can begin our analysis. The only issue is waiting for the data to pour in. If you want immediate access to altcoin data, you can visit alt19 – but I find CoinMarketCap to be the most reliable data source. Maybe one day they’ll enhance their API.

Until then, feel free to play around with your new data! If you have any questions, please comment below. And be on the lookout for Part II!

5 comments:

  1. Thank you for this guide.
    I have few issues when I try to implement this:
    I think they all stem from the same problem, that the api returns a list which contains a dictionary.
    1.When I try to make the json readable, it tells me json objects need to be a string, not a list.
    2. When I visualise the data with pandas, It does not recognise the input as a dictionary with multiple key : value objects. It sees this as a list with one collumn.
    Could you help me implement this?

    1. Hi Glen, i had the same issue but managed to get it working:
      import coinmarketcap
      import json
      import pandas as pd
      import time
      from pandas import Series, DataFrame

      market = coinmarketcap.Market()
      coin = market.ticker(“ethereum”)
      ###This will output human readable code
      print(coin)

      ### This will output the pandas dataframe

      ether = pd.Series(market.ticker(“ethereum”)[0])
      wowcoin = pd.Series(market.ticker(“wowcoin”)[0])
      bitcoin = pd.Series(market.ticker(“bitcoin”)[0])
      coinArray = pd.DataFrame([ether,bitcoin,wowcoin]).set_index(“id”)

      you can find me on https://www.fiverr.com/sampsonakinyelu

      Thumbs up to blockxchain.org for such a wonderful article. Your work is very useful and easy to grasp.
      Thanks alot,
      Sampson.

  2. Hi Sampson,

    Thanks for the reply this was really useful.
    Im new to python and struggling with converting your code to accommodate say the top 100 tokens rather than only the 3 specified.
    Any suggestions would really useful!

Leave a Reply

Your email address will not be published. Required fields are marked *