Using Twitter to forecast cryptocurrency returns #2 – Mining Cryptocurrency with CoinGecko API

Gearing up for returns forecasting with VAR

Caption: Screen grab from CoinGecko

After going through Tweets mining as explained in my first article, gathering cryptocurrency returns wasn’t difficult at all. I had decided to use pycoingecko, which is a Python wrapper around the CoinGecko API

The key 2 things to note are:

  • You need the id of the cryptocurrency in order to download the market data
  • the granularity of the data is automatically determined by the number of days you are downloading for

Getting id of cryptocurrency

Go to the CoinGecko API and execute the “/coin/list”. 

Get the ids of the coins required. In my case, I have the list below:

gecko_list = [
    "ripple",  # xrp

Downloading historical market data

I used get_coin_market_chart_by_id, the wrapper around /coins/{id}/market_chart to get my historical market data. The granularity of the data returned depends on the number of days we are getting:

  • minutely data for duration within 1 day
  • hourly data will be used for duration between 1 day and 90 days
  • daily data will be used for duration above 90 days

Since I do not have the luxury to collect hourly data beyond 90 days, I can only stick to daily market data. The code snippet below returns 300 days worth of historical market data against USD. I’m only going to store the prices returned for the given timestamp.

cg = CoinGeckoAPI()
timePeriod = 300

data = {}
for coin in gecko_list:
        nested_lists = cg.get_coin_market_chart_by_id(
            id=coin, vs_currency="usd", days=timePeriod
        data[coin] = {}
        data[coin]["timestamps"], data[coin]["values"] = zip(*nested_lists)

    except Exception as e:
        print("coin: " + coin)

frame_list = [
    pd.DataFrame(data[coin]["values"], index=data[coin]["timestamps"], columns=[coin])
    for coin in gecko_list
    if coin in data

Let’s convert the timestamp into a user friendly format:

df_cyptocurrency["datetime"] = pd.to_datetime(df_cyptocurrency.index, unit="ms")
df_cyptocurrency["date"] = df_cyptocurrency["datetime"]
df_cyptocurrency["hour"] = df_cyptocurrency["datetime"].dt.hour

Let’s align the data into a format that is more generic:

df_cyptocurrency = df_cyptocurrency.melt(
    id_vars=["datetime", "date", "hour"], var_name="currency_name", ignore_index=True

Now, that was not too bad right? Gathering cryptocurrency returns was way more simple than mining tweets. The only confusing part during data collection was the granularity of the data as I overlooked the API method definitions. My notebook is available in the atoti notebook gallery for your reference.

Now, let’s take a breather before I start performing my time-series analysis.

Latest posts

Constraints straining your portfolio optimization?
See how we expanded our atoti and external optimizer integration use case...
Contributing to atoti notebook gallery
Share your atoti use case with our community If you haven’t heard...
atoti: Guide to implementing lookup in a multidimensional cube
A simple example of converting your analytics into any referenced currency Today...

Join our Community

    Like this post ? Please share

    Follow Us

    atoti Free Community Edition is developed and brought to you by ActiveViam. Learn more about ActiveViam at