In coingecko, trying to get historical prices, but get the prices compared to every currencies

791 Views Asked by At

When I use the cg.get_coin_history_by_id('bitcoin', date='30-12-2017') it seems to list the price compared to everything. I don't know how to make it just print one specific currency.

2

There are 2 best solutions below

0
On

cg.get_coin_history_by_id returns more than just price, it returns market cap, volume, etc... It isn't comparing the price to anything, it's just returning the data in multiple different currencies.

If you want to access the price for a specific currency just reference it using the keys you see in the response. For example, to get the price of bitcoin on that day in Australian dollars, you would do it like this:

api_resp = cg.get_coin_history_by_id('bitcoin', date='30-12-2017')
price_in_aud = api_resp['market_data']['current_price']['aud']
print(price_in_aud)

Output:
17446.3215245937

The structure of the json looks like this:

{
    "id":"bitcoin",
    "symbol":"btc",
    "name":"Bitcoin",
    "localization":{
       "en":"Bitcoin",
       .
       .
       .
    },
    "image":{
       "thumb":"https://assets.coingecko.com/coins/images/1/thumb/bitcoin.png?1547033579",
       ...
    },
    "market_data":{
       "current_price":{
          "aud":17446.3215245937,
          ...
       },
       "market_cap":{
          "aud":292616246981.057,
          ...
       },
       "total_volume":{
          "aud":4611856472.88116,
          ...
       }
    },
    "community_data":{
       "facebook_likes":"None",
       "twitter_followers":603664,
       ...
    },
    "developer_data":{
       "forks":13660,
       ...
       "code_additions_deletions_4_weeks":{
          "additions":"None",
          "deletions":"None"
       },
       "commit_count_4_weeks":147
    },
    "public_interest_stats":{
       "alexa_rank":2912,
       "bing_matches":"None"
    }
 }

source: pycoingecko

0
On

It's not a .json it's a python dictionary. You can get the prices with the keys. For example if you want the price in US Dollars (USD):

>>> history = cg.get_coin_history_by_id(id='bitcoin', date="30-12-2017")
>>> price = history["market_data"]["current_price"]["usd"]
>>> price

output:

13620.3618741461