はしくれエンジニアもどきのメモ

情報系技術・哲学・デザインなどの勉強メモ・備忘録です。

各仮想通貨の時価総額を取得

各仮想通貨の時価総額を取得

各仮想通貨の時価総額を取得するメモ.

時価総額は,CoinMarketCap: Cryptocurrency Market Capitalizations のAPIから取得できる

coinmarketcap.com

CoinMarketCap JSON API

Ticker

https://coinmarketcap.com/api/

今のところpublic APIとして利用できる.

  • Endpoint:/ticker/
  • Method: GET

Optional parameters:

  • (int) start - return results from rank [start] and above
  • (int) limit - return a maximum of [limit] results (default is 100, use 0 to return all results)(仮想通貨の取得したい種類数)
  • (string) convert - return price, 24h volume, and market cap in terms of another currency. Valid values are: "AUD", "BRL", "CAD", "CHF", "CLP", "CNY", "CZK", "DKK", "EUR", "GBP", "HKD", "HUF", "IDR", "ILS", "INR", "JPY"(日本円にも対応している), "KRW", "MXN", "MYR", "NOK", "NZD", "PHP", "PKR", "PLN", "RUB", "SEK", "SGD", "THB", "TRY", "TWD", "ZAR"

例:上位100の時価総額一覧(日本円表示) https://api.coinmarketcap.com/v1/ticker/?converter=JPY

返り値のmarket_cap_jpyが日本円での時価総額になる.

[
    {
        "id": "bitcoin",
        "name": "Bitcoin",
        "symbol": "BTC",
        "rank": "1",
        "price_usd": "12950.6",
        "price_btc": "1.0",
        "24h_volume_usd": "13026200000.0",
        "market_cap_usd": "217225917805",
        "available_supply": "16773425.0",
        "total_supply": "16773425.0",
        "max_supply": "21000000.0",
        "percent_change_1h": "-1.02",
        "percent_change_24h": "-5.5",
        "percent_change_7d": "-5.93",
        "last_updated": "1514713460",
        "price_jpy": "1459338.361",
        "24h_volume_jpy": "1467857347000.0000000000",
        "market_cap_jpy": "24478102547856"
    },
    {
        "id": "ripple",
        "name": "Ripple",
        "symbol": "XRP",
        "rank": "2",
        "price_usd": "2.18641",
        "price_btc": "0.00017128",
        "24h_volume_usd": "6494140000.0",
        "market_cap_usd": "84699653685.0",
        "available_supply": "38739144847.0",
        "total_supply": "99993093880.0",
        "max_supply": "100000000000",
        "percent_change_1h": "-1.92",
        "percent_change_24h": "-11.67",
        "percent_change_7d": "108.87",
        "last_updated": "1514713441",
        "price_jpy": "246.37561085",
        "24h_volume_jpy": "731792165900.0000000000",
        "market_cap_jpy": "9544380475486"
    },
    {
        "id": "ethereum",
        "name": "Ethereum",
        "symbol": "ETH",
        "rank": "3",
        "price_usd": "715.474",
        "price_btc": "0.0560494",
        "24h_volume_usd": "2824780000.0",
        "market_cap_usd": "69172086218.0",
        "available_supply": "96680084.0",
        "total_supply": "96680084.0",
        "max_supply": null,
        "percent_change_1h": "-0.76",
        "percent_change_24h": "-1.06",
        "percent_change_7d": "6.12",
        "last_updated": "1514713448",
        "price_jpy": "80623.18769",
        "24h_volume_jpy": "318310334300.0000000000",
        "market_cap_jpy": "7794656535465"
    },
    ...
]

Pythonで取得

requestsモジュールを使って取得する.

import requests
import pandas as pd

end_uri = "https://api.coinmarketcap.com/v1/ticker/"
params = {"convert": "JPY"}

res = requests.get(url=end_uri, params=params)
res_json_arr = res.json()
res_json_arr

> [
    {
        "id": "bitcoin",
        "name": "Bitcoin",
        "symbol": "BTC",
        "rank": "1",
        "price_usd": "12950.6",
        "price_btc": "1.0",
        "24h_volume_usd": "13026200000.0",
        "market_cap_usd": "217225917805",
        "available_supply": "16773425.0",
        "total_supply": "16773425.0",
        "max_supply": "21000000.0",
        "percent_change_1h": "-1.02",
        "percent_change_24h": "-5.5",
        "percent_change_7d": "-5.93",
        "last_updated": "1514713460",
        "price_jpy": "1459338.361",
        "24h_volume_jpy": "1467857347000.0000000000",
        "market_cap_jpy": "24478102547856"
    },
    {
        "id": "ripple",
        "name": "Ripple",
        "symbol": "XRP",
        "rank": "2",
        "price_usd": "2.18641",
        "price_btc": "0.00017128",
        "24h_volume_usd": "6494140000.0",
        "market_cap_usd": "84699653685.0",
        "available_supply": "38739144847.0",
        "total_supply": "99993093880.0",
        "max_supply": "100000000000",
        "percent_change_1h": "-1.92",
        "percent_change_24h": "-11.67",
        "percent_change_7d": "108.87",
        "last_updated": "1514713441",
        "price_jpy": "246.37561085",
        "24h_volume_jpy": "731792165900.0000000000",
        "market_cap_jpy": "9544380475486"
    },
    {
        "id": "ethereum",
        "name": "Ethereum",
        "symbol": "ETH",
        "rank": "3",
        "price_usd": "715.474",
        "price_btc": "0.0560494",
        "24h_volume_usd": "2824780000.0",
        "market_cap_usd": "69172086218.0",
        "available_supply": "96680084.0",
        "total_supply": "96680084.0",
        "max_supply": null,
        "percent_change_1h": "-0.76",
        "percent_change_24h": "-1.06",
        "percent_change_7d": "6.12",
        "last_updated": "1514713448",
        "price_jpy": "80623.18769",
        "24h_volume_jpy": "318310334300.0000000000",
        "market_cap_jpy": "7794656535465"
    },
    ...
]

仮想通貨のシンボル名と時価総額のみの一覧を取得

symbols = []
market_caps = []
for j in res_json_arr:
    symbols.append(j["symbol"])
    market_caps.append(j["market_cap_jpy"])

symbols, market_caps

> (['BTC',  'XRP',  'ETH',...'],
 ['24928878505122',  '9555861268499',  '7880722002166',...])

pd.Seriesを使って,時価総額の割合を求める.

market_cap_ser = pd.Series(market_caps, index=symbols, dtype=float)
market_cap_ser

> BTC      2.492888e+13
XRP      9.555861e+12
ETH      7.880722e+12
...

ser_ratio = (market_cap_ser / market_cap_ser.sum())
ser_ratio

> BTC      0.395964
XRP      0.151783
ETH      0.125175

累積して,上位20をみると,

ser_ratio.cumsum().head(20)

> BTC      0.395964
XRP      0.547747
ETH      0.672922
BCH      0.746941
ADA      0.779363
LTC      0.800783
MIOTA    0.817845
XEM      0.832895
DASH     0.846961
XLM      0.857263
XMR      0.866728
EOS      0.875579
NEO      0.883838
QTUM     0.891874
BTG      0.899639
XVG      0.904799
ETC      0.909587
TRX      0.914053
BCC      0.918334
LSK      0.922304

よって,上位20まで保有すると,仮想通貨全体(厳密には100種類)の92%を保有できることがわかる.