Skip to content

Instantly share code, notes, and snippets.

What would you like to do?
import os
import argparse
from typing import NamedTuple
from coinmarketcapapi import CoinMarketCapAPI, CoinMarketCapAPIError
api_key = os.environ["COINMARKETCAP_API_KEY"]
cmc = CoinMarketCapAPI(api_key)
class Dat(NamedTuple):
symbol: str
mcap: float
volume: int
last: str
price: float
change24: float
def main():
r = cmc.cryptocurrency_listings_latest(sort="market_cap")
total_vm = 0
data =
args = parse_args()
for ent in iterate(data):
total_vm += ent.mcap * ent.volume
date = ent.last[0:10]
for i, ent in enumerate(iterate(data)):
rank = i + 1
pct = ent.mcap * ent.volume/total_vm
vwcap = ent.mcap * pct
out = "{rank}\t{symbol}\t{pct:.2%}\t{change:.2%}".format(symbol=ent.symbol, pct=pct, rank=rank, change=ent.change24)
if rank <=
def iterate(data):
for ent in data:
symbol = ent["symbol"]
tags = ent["tags"]
price = ent["quote"]["USD"]["price"]
if "stablecoin" in tags:
mcap = ent["quote"]["USD"]["market_cap"]
volume = ent["quote"]["USD"]["volume_24h"]
change24 = ent["quote"]["USD"]["percent_change_24h"] / 100.0
last = ent["last_updated"]
dat = Dat(symbol, mcap, volume, last, price, change24)
yield dat
def parse_args():
parser = argparse.ArgumentParser(description='Volume weighted market cap')
parser.add_argument('top', type=int, default=10, help='print the top ranks', nargs="?")
return parser.parse_args()
if __name__ == "__main__":
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment