Last active
March 28, 2023 04:13
-
-
Save AmanAdastra/a1b6662dc2bd8e84d31d57a201558116 to your computer and use it in GitHub Desktop.
This file contains 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
# Basically for latest 24 Hour change percentage what i did is to get the current metrics and get the yesterday metric for the same time | |
# and then manually calculated the percentage. | |
client = CoinMetricsClient(api_key=api_key) | |
def get_coin_info(coin_ids): | |
data = list(client.catalog_assets(exclude=["exchanges","markets","metrics"],assets=coin_ids)) | |
data_today = client.get_asset_metrics(coin_ids,metrics="ReferenceRateUSD",frequency="1m",page_size=10,limit_per_asset=1,paging_from="end",end_time=datetime.utcnow(),end_inclusive=True) | |
yesterday = datetime.utcnow() - timedelta(days=1) | |
data_yesterday = client.get_asset_metrics(coin_ids,metrics="ReferenceRateUSD",frequency="1m",page_size=10,limit_per_asset=1,paging_from="start",start_time=yesterday,start_inclusive=True) | |
historical_data = list(data_today) + list(data_yesterday) | |
coin_data = {} | |
for coin in data: | |
price_change_data = [x for x in historical_data if x["asset"] == str(coin["asset"]).lower()] | |
price_change = float(price_change_data[0]["ReferenceRateUSD"]) - float(price_change_data[1]["ReferenceRateUSD"]) | |
price_change_pct = (price_change / float(price_change_data[1]["ReferenceRateUSD"])) | |
coin_info = { | |
"id": str(coin["asset"]).upper(), | |
"name": coin["full_name"], | |
"symbol": str(coin["asset"]).upper(), | |
"currency": str(coin["asset"]).upper(), | |
"1d": { | |
"price_change": str(price_change), | |
"price_change_pct": str(price_change_pct), | |
"volume": str(0), | |
}, | |
} | |
coin_data.update({(str(coin["asset"])).upper(): coin_info}) | |
return coin_data |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment