Top N Crypto-Assets by MarketCap for Crypto-Portfolio Backtesting Purposes in Python

A quantitative research over the construction of a perfect crypto-portfolio can be based on a number of crypto-assets. The selection of them is of paramount importance. If you are able to build the right portfolio, stick to it, or successfully manage its composition in time (e.g. through the method of rebalancing), the final PnL coming from your management can be worth every dollar invested in all underlings.

In this short research note, we will develop a Python code which enables fetching the most updated list of Top N crypto-assets sorted by market capitalisation (MarketCap, in short). The list can be generated every day and saved for further investigation (e.g. the change of Top N components over the past 3 years).

1. Crypto-Data Provider

The data we are interested in are, luckily, provided by CryptoCompare.com API (see documentation page here). The publicly available default URL address is

https://min-api.cryptocompare.com/data/top/mktcapfull?limit=10&tsym=USD

which allows to get the full suite of information on the top 10 crypto-assets (by MarketCap) with the coins’ prices converted to USD fiat currency as a reference. Let’s see how one can turn this URL address into meaningful data stream extracted from the data provider down in Python.

2. Top N Crypto-Assets Sorted by MarketCap

Let’s say we are interested in getting information on the current Top N=75 cryptocurrencies. We can achieve it by executing the following code:

# Top N Crypto-Assets by MarketCap for Crypto-Portfolio Backtesting Purposes in Python
#
# (c) 2022 by QuantAtRisk.com
 
import requests
import json
from bs4 import BeautifulSoup
from datetime import datetime
 
 
def timestamp2date_hms(timestamp):
    # function converts a Unix timestamp into Gregorian date
    return datetime.fromtimestamp(int(timestamp)).strftime('%Y-%m-%d %H:%M')
 
 
top_n = 75
url = 'https://min-api.cryptocompare.com/data/top/mktcapfull?limit=' + str(top_n) 
      + '&tsym=USD'
 
# fetch the raw data
response = requests.get(url)
soup = BeautifulSoup(response.content, "html.parser")
dic = json.loads(soup.prettify())  # convert from json to dictionary
 
data = dic['Data']
n_records = len(data)
print(n_records)  # 75
 
# first record
d = data[0]

The key is the response. For the first record, corresponding to Bitcoin/BTC (of course!), we find:

{'CoinInfo': {'Id': '1182',
  'Name': 'BTC',
  'FullName': 'Bitcoin',
  'Internal': 'BTC',
  'ImageUrl': '/media/37746251/btc.png',
  'Url': '/coins/btc/overview',
  'Algorithm': 'SHA-256',
  'ProofType': 'PoW',
  'Rating': {'Weiss': {'Rating': 'B+',
    'TechnologyAdoptionRating': 'A-',
    'MarketPerformanceRating': 'C+'}},
  'NetHashesPerSecond': 193928423919356840000,
  'BlockNumber': 729289,
  'BlockTime': 605,
  'BlockReward': 6.25,
  'AssetLaunchDate': '2009-01-03',
  'MaxSupply': 20999999.9769,
  'Type': 1,
  'DocumentType': 'Webpagecoinp'},
 'RAW': {'USD': {'TYPE': '5',
   'MARKET': 'CCCAGG',
   'FROMSYMBOL': 'BTC',
   'TOSYMBOL': 'USD',
   'FLAGS': '1026',
   'PRICE': 44865.64,
   'LASTUPDATE': 1648409699,
   'MEDIAN': 44871.5,
   'LASTVOLUME': 0.00221708,
   'LASTVOLUMETO': 99.482818388,
   'LASTTRADEID': '304077243',
   'VOLUMEDAY': 8836.248462356232,
   'VOLUMEDAYTO': 395276930.3160927,
   'VOLUME24HOUR': 11549.325863369999,
   'VOLUME24HOURTO': 516131733.2285419,
   'OPENDAY': 44542.49,
   'HIGHDAY': 45007.6,
   'LOWDAY': 44449.41,
   'OPEN24HOUR': 44387.58,
   'HIGH24HOUR': 45031.99,
   'LOW24HOUR': 44274.89,
   'LASTMARKET': 'Coinbase',
   'VOLUMEHOUR': 220.82893112000076,
   'VOLUMEHOURTO': 9911823.879511757,
   'OPENHOUR': 44865.22,
   'HIGHHOUR': 44907.6,
   'LOWHOUR': 44840.53,
   'TOPTIERVOLUME24HOUR': 11549.305531369999,
   'TOPTIERVOLUME24HOURTO': 516130828.48256934,
   'CHANGE24HOUR': 478.0599999999977,
   'CHANGEPCT24HOUR': 1.0770129842627096,
   'CHANGEDAY': 323.15000000000146,
   'CHANGEPCTDAY': 0.7254870574141712,
   'CHANGEHOUR': 0.41999999999825377,
   'CHANGEPCTHOUR': 0.0009361371681633429,
   'CONVERSIONTYPE': 'direct',
   'CONVERSIONSYMBOL': '',
   'SUPPLY': 18995562,
   'MKTCAP': 852248046289.68,
   'MKTCAPPENALTY': 0,
   'CIRCULATINGSUPPLY': 18995562,
   'CIRCULATINGSUPPLYMKTCAP': 852248046289.6799,
   'TOTALVOLUME24H': 91014.50072698834,
   'TOTALVOLUME24HTO': 4081387661.196691,
   'TOTALTOPTIERVOLUME24H': 90847.52942525677,
   'TOTALTOPTIERVOLUME24HTO': 4073896394.345091,
   'IMAGEURL': '/media/37746251/btc.png'}},
 'DISPLAY': {'USD': {'FROMSYMBOL': 'Ƀ',
   'TOSYMBOL': '$',
   'MARKET': 'CryptoCompare Index',
   'PRICE': '$ 44,865.6',
   'LASTUPDATE': 'Just now',
   'LASTVOLUME': 'Ƀ 0.002217',
   'LASTVOLUMETO': '$ 99.48',
   'LASTTRADEID': '304077243',
   'VOLUMEDAY': 'Ƀ 8,836.25',
   'VOLUMEDAYTO': '$ 395,276,930.3',
   'VOLUME24HOUR': 'Ƀ 11,549.3',
   'VOLUME24HOURTO': '$ 516,131,733.2',
   'OPENDAY': '$ 44,542.5',
   'HIGHDAY': '$ 45,007.6',
   'LOWDAY': '$ 44,449.4',
   'OPEN24HOUR': '$ 44,387.6',
   'HIGH24HOUR': '$ 45,032.0',
   'LOW24HOUR': '$ 44,274.9',
   'LASTMARKET': 'Coinbase',
   'VOLUMEHOUR': 'Ƀ 220.83',
   'VOLUMEHOURTO': '$ 9,911,823.9',
   'OPENHOUR': '$ 44,865.2',
   'HIGHHOUR': '$ 44,907.6',
   'LOWHOUR': '$ 44,840.5',
   'TOPTIERVOLUME24HOUR': 'Ƀ 11,549.3',
   'TOPTIERVOLUME24HOURTO': '$ 516,130,828.5',
   'CHANGE24HOUR': '$ 478.06',
   'CHANGEPCT24HOUR': '1.08',
   'CHANGEDAY': '$ 323.15',
   'CHANGEPCTDAY': '0.73',
   'CHANGEHOUR': '$ 0.42',
   'CHANGEPCTHOUR': '0.00',
   'CONVERSIONTYPE': 'direct',
   'CONVERSIONSYMBOL': '',
   'SUPPLY': 'Ƀ 18,995,562.0',
   'MKTCAP': '$ 852.25 B',
   'MKTCAPPENALTY': '0 %',
   'CIRCULATINGSUPPLY': 'Ƀ 18,995,562.0',
   'CIRCULATINGSUPPLYMKTCAP': '$ 852.25 B',
   'TOTALVOLUME24H': 'Ƀ 91.01 K',
   'TOTALVOLUME24HTO': '$ 4.08 B',
   'TOTALTOPTIERVOLUME24H': 'Ƀ 90.85 K',
   'TOTALTOPTIERVOLUME24HTO': '$ 4.07 B',
   'IMAGEURL': '/media/37746251/btc.png'}}}

which is, to be honest, a huge bag of momentary information on that crypto-asset as we retrieve these data. In the remaining part of the code, you can see that we extract from this stream only three records to compile the final table:

print('  #       Coin  Price (USD)      MarketCap (USD)')
print('-'*48)
for i in range(n_records):
    d = data[i]
    try:
        coin, price, mktcap = d['RAW']['USD']['FROMSYMBOL'], d['RAW']['USD']['PRICE'], 
                              d['RAW']['USD']['MKTCAP']
    except:
        None
    print('%3g %10s %12.2f %20.2f' % (i+1, coin, price, mktcap))

which is:

  #       Coin  Price (USD)      MarketCap (USD)
------------------------------------------------
  1        BTC     44865.64      852248046289.68
  2        ETH      3177.47      381728597083.12
  3        XRP         0.84       84041306308.42
  4       USDT         1.00       81331736008.26
  5        BNB       417.68       68968598485.35
  6       LUNA        90.56       68237433417.07
  7        SOL       103.03       53390167190.51
  8        ADA         1.13       37388525927.32
  9       AVAX        85.73       34352102609.07
 10        DOT        21.44       25261794928.60
 11       DOGE         0.14       18781321532.27
 12        AXS        65.96       17809200000.00
 13       BUSD         1.00       17566012245.93
 14      MATIC         1.65       16500000000.00
 15       LINK        16.22       16220000000.00
 16        UST         1.00       16217360514.16
 17        FTT        50.71       16082621103.68
 18       SHIB         0.00       14472044074.15
 19        CRO         0.47       14135853695.53
 20        APE        13.82       13820000000.00
 21        APE        13.82       13820000000.00
 22       KLAY         1.15       12468813157.59
 23       WBTC     44897.65       12312303787.41
 24        XLM         0.22       11125397839.60
 25        UNI        10.99       10990000000.00
 26       SAND         3.44       10329000000.00
 27        ICP        21.28       10196747598.04
 28       GALA         0.25        9384032703.86
 29        DAI         1.00        9240900157.40
 30        LTC       124.32        8694638793.59
 31       ATOM        28.92        8425389899.42
 32       NEAR        12.70        8391213579.92
 33        BCH       360.99        6866113241.63
 34        TRX         0.07        6751429662.71
 35        VET         0.08        6491870608.86
 36       ALGO         0.92        6512901638.49
 37        ETC        45.20        5966223311.20
 38        OKB        22.53        5918581086.55
 39       MANA         2.65        5811164087.47
 40        LEO         5.95        5577600565.79
 41       DYDX         5.47        5475000000.00
 42        QNT       144.21        5180201162.21
 43       RUNE        10.21        5105000000.00
 44       USDC         1.00        5081211887.50
 45       HBAR         0.24        4793615299.72
 46        IMX         2.22        4446000000.00
 47       EGLD       188.84        4422632233.48
 48        GRT         0.42        4324482971.31
 49        CRV         2.52        4315234736.33
 50        ILV       594.82        4163756688.00
 51        FIL        22.10        4039024973.10
 52        XMR       211.39        3825938691.53
 53        FTM         1.38        3705158205.95
 54        GNO       367.95        3679510260.00
 55      THETA         3.51        3514325581.20
 56        LDO         3.43        3412668118.32
 57        XTZ         3.79        3450701425.90
 58        DFI         4.37        3423941285.56
 59      WAVES        31.55        3402591149.00
 60        PCI         0.83        3253404965.22
 61       CELO         3.26        3260000000.00
 62        KCS        18.75        3189767072.12
 63        RLY         0.21        3133500000.00
 64        YGG         2.86        2856545530.00
 65       ROSE         0.28        2848968140.00
 66        HNT        23.98        2763688221.53
 67        EOS         2.57        2696824762.24
 68        AMP         0.03        2681004762.97
 69        CVX        30.24        2636089162.80
 70       AAVE       164.15        2626400000.00
 71      1INCH         1.72        2578500000.00
 72      1INCH         1.72        2578500000.00
 73        NEO        25.18        2518000000.00
 74         MC         2.51        2449618968.76
 75        ZEC       197.56        2423935288.35

This accomplishes our task.

Visit Quant at Risk  for additional insight on this topic: https://quantatrisk.com/2022/03/27/top-n-crypto-assets-by-marketcap-for-crypto-portfolio-backtesting-python/.

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 Quant at Risk and is being posted with permission from Quant at Risk. The views expressed in this material are solely those of the author and/or Quant at Risk 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.