Extracting data from Coingecko with Python

in Programming & Dev3 months ago

For a while I have been meaning to try getting some crypto price data to play with. Some time ago I did some coding with Coinmarketcap, but I tend to use @coingecko for my prices these days. They have an API that is free to use and does not even require you to have an API key. My programming language of choice is Python and they have a library for that.

I am using Python 3.8.5 on Ubuntu Linux. Installing the library was as simple as:

pip install pycoingecko

For general playing around I tend to work in Jupyter Notebooks as this is fairly interactive and you can run little pieces of code at a time.

You access the API by creating an API object. I am loading a couple of other libraries I needed:

import matplotlib.pyplot as plt
from datetime import datetime

from pycoingecko import CoinGeckoAPI
cg = CoinGeckoAPI()

Getting prices is pretty simple. You need to list the coins and the currencies you want. Lists of the names of these can be obtained using other functions.

cg.get_price(ids='hive,steem',vs_currencies='usd,btc',include_market_cap='true')

This gives you a dictionary of values. You can extract the individual values easily.

{'steem': {'usd': 0.706357,
'usd_market_cap': 274169195.78568745,
'btc': 1.369e-05,
'btc_market_cap': 5308.488493606708},
'hive': {'usd': 0.788722,
'usd_market_cap': 287945892.6830051,
'btc': 1.529e-05,
'btc_market_cap': 5575.234131276685}}

Getting values over time is also simple. This gives you lists of price, market cap and volume.

hivemc = cg.get_coin_market_chart_by_id(id='hive', vs_currency='usd', days=90)
steemmc = cg.get_coin_market_chart_by_id(id='steem', vs_currency='usd', days=90)

maptplotlib is a powerful plotting library with loads of options, but I am keeping it fairly simple for now. The tricky bit was getting usable dates. The timestamp seems to be a Unix one in milliseconds, so a little conversion was required.

fig, ax = plt.subplots()
fig.set_size_inches(8,6)
ax.plot([datetime.utcfromtimestamp(x[0]/1000) for x in hivemc['prices']], 
        [x[1] for x in hivemc['prices']], label='HIVE')
ax.plot([datetime.utcfromtimestamp(x[0]/1000) for x in steemmc['prices'] ], 
        [x[1] for x in steemmc['prices']], label='STEEM')
ax.set_xlabel('Hours')
ax.set_ylabel('Price *US$)')
ax.legend();

That gives this graph (that makes me happy in more ways than one):

Prices

There are lots of ways you can customise this. With a little work you could reproduce the sort of charts you see on the Coingecko website or elsewhere online.

Ages ago I did a price ticker with a Raspberry Pi Zero and a matrix display. I am tempted to revive that to show me the $HIVE price.

Sort:  

@steevc,
Coingecko prices are not frequently updating. So I am using

So either one failed I am moving to other. If 1st 3 api failed, I am going to coingecko.
$WINE

Cheers~

I'm not relying on up to date data for now, so this API is adequate. I've not heard of all those. Where do they get their data?

!BEER

@steevc,
No idea probably from CMC. Because those data are very accurate while I tested them to use with my apps.

Cheers~

CMC seem to use a wrong value for the number of HIVE out there. I guess I could query more than one source to see what difference there is. It was just an experiment.

@steevc,
Yeah those values are not much accurate, because data appear from different exchanges. But coingecko is not updating it's hive price so frequently. So, those apis also work well.

Cheers~


Congratulations, @theguruasia You Successfully Shared 0.200 WINE With @steevc.
You Earned 0.200 WINE As Curation Reward.
You Utilized 2/5 Successful Calls.

wine_logo


Contact Us : WINE Token Discord Channel
WINE Current Market Price : 0.310

 3 months ago Reveal Comment

Your code is simple to understand and easy to implement, that is the most important thing while writing technical posts. Next time try to increase the plot size so that a loser like me could understand better.
image.png

Its a great post 👍 and I will try to implement myself in future.
!LUV

You can tweak the settings to make it bigger. Can add grid lines if you like.

!BEER

<><

<><

LUV

Connect

Trade


@steevc, you've been given LUV from @biggestloser.

Check the LUV in your H-E wallet. LUV changes soon. (1/1)

 3 months ago Reveal Comment

looks like you know your way around I am too trying to learn HTML and ruby then Python wish me luck and you have done an amazing job there


The rewards earned on this comment will go directly to the person sharing the post on Twitter as long as they are registered with @poshtoken. Sign up at https://hiveposh.com.

Congratulations @steevc! You have completed the following achievement on the Hive blockchain and have been rewarded with new badge(s) :

You published more than 3200 posts.
Your next target is to reach 3300 posts.

You can view your badges on your board and compare yourself to others in the Ranking
If you no longer want to receive notifications, reply to this comment with the word STOP

To support your work, I also upvoted your post!

Check out the last post from @hivebuzz:

Hive Power Up Month - Quick feedback
Feedback from the September 1st Hive Power Up Day