Cryptocurrency data collection in python

From Fintech Lab Wiki
Revision as of 23:13, 27 February 2023 by 3126123 (talk | contribs) (Created page with "In this document, we provide a brief tutorial on using CoinMarketCap Pro API to access cryptocurrency data. We'll display how to access exchange trading volume and cryptocurre...")
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)

In this document, we provide a brief tutorial on using CoinMarketCap Pro API to access cryptocurrency data. We'll display how to access exchange trading volume and cryptocurrency OHLCV data, but using other endpoints is essentially similar. You can find the CMC API documentation here.

The only dependencies necessary to run the illustration are load_dotenv and requests.

Snip1.png

Define a class which will contain the logic for accessing the API. The user can either pass api_key parameter directly to the __init__ method or set it as an environment variable. The implementation supports .env files as load_dotenv is called. Make sure to manually create the env file populated with your API key. The base URL is automatically selected based on the existence of a paid API key, but can be overridden if the parameter is set by the user.

Snip2.png

Add the session property, which persists the API key across all requests. Furthermore, a Session instance uses a connection pool and reuses the TCP connection, which will lead to a significant increase in performance.

Snip3.png

Define a private method, __make_req, which will be used by public methods to send requests to the API. __make_req receives the endpoint address, a dictionary of parameters, which must comply the format explained in the endoint's documentation, and the version of the endpoint. If the API returns an error, it'll be automatically raised. Otherwise, the response will be converted to a dict object and returned.

Snip4.png

Time to add the public methods. We'll define methods for cryptocurrency OHLCV and exchange quotes endpoints.

Snip5.png

The user can access the data now.

Snip6.png

The code results in:

{'status': {'timestamp': '2022-09-06T14:55:37.113Z',  'error_code': 0,  'error_message': None,  'elapsed': 22,  'credit_count': 1,  'notice': None}, 'data': {'binance': {'quotes': [{'timestamp': '2022-09-06T14:50:12.000Z',     'quote': {'USD': {'volume_24h': 73522805073.14716,       'timestamp': '2022-09-06T14:50:12.000Z'}},     'num_market_pairs': 1702}],   'id': 270,   'name': 'Binance',   'slug': 'binance'},  'kraken': {'quotes': [{'timestamp': '2022-09-06T14:50:11.000Z',     'quote': {'USD': {'volume_24h': 708319460.386547,       'timestamp': '2022-09-06T14:50:11.000Z'}},     'num_market_pairs': 619}],   'id': 24,   'name': 'Kraken',   'slug': 'kraken'}}}

Finally, you need to mind the number calls made in a limited interval and potentially add pauses to the code, so the rate limit is not hit. As suggested by CMC, an exponential backoff algorithm could be used. While in this tutorial we used the CMC API, fetching data from any other API would be quite similar.

The source code for this article can be found here.