Last active
April 5, 2022 17:13
-
-
Save earonesty/f3ae1ce1c3158d76c17ef306ec2d7e97 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 = r.data | |
args = parse_args() | |
for ent in iterate(data): | |
total_vm += ent.mcap * ent.volume | |
date = ent.last[0:10] | |
print(date) | |
print("RANK\tSYM \tMCAP\tCHANGE") | |
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 <= args.top: | |
print(out) | |
def iterate(data): | |
for ent in data: | |
symbol = ent["symbol"] | |
tags = ent["tags"] | |
price = ent["quote"]["USD"]["price"] | |
if "stablecoin" in tags: | |
continue | |
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__": | |
main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment