Crypto Price Data Acquisition with the CCXT library & Exchange APIs Overview
This post just gives an overview on all the different public exchange APIs supported by the ccxt python library and the price data they deliver. This is important, as not all API's return real data (some are emulated) and depending on which project one has, one needs to make sure, that the API returns enough data i.e. that we get price data going back far enough.
As stated above I use the ccxt python library to get price data from different exchange APIs. To use the ccxt library it first needs to be installed. This can be done by installing it via pip as such:
pip install ccxt
To now use the ccxt library in a python script we need to import it:
Now we have imported the library in python and we can start to use it. To connect to an exchange we need to instantiate the exchange class from ccxt. An example is shown below, where we connect to the public API of the Binance exchange.
exchange = getattr(ccxt, "binance")()
To connect to the private API you also need to pass your API key and your secret. More information can be found here.
After connecting to the exchange we can check if we are connected to the right exchange. To do this we can simply print the exchange name.
In this case this should print "Binance". As our goal now is to get price data from the exchange API we first have to make sure that the exchange supports the fetching of price data. To do this we can simply check if the exchange has the capability to fetch OHLCV-data.
If the exchange has the capability this should print True. After making sure the exchange has the capability to fetch price data we now can fetch price data for the trading pair we want. In this post we use the "BTC/USDT" trading pair, but you can switch that to any trading pair you want. As an example, if we want to fetch daily price data from the exchange we can do this as such:
ohlc = exchange.fetch_ohlcv("BTC/USDT")
Further the 'fetch_ohlcv'-function provides additional parameters like timeframe or limit, you can check them out here.
Something to keep in mind
The ccxt library documentation points out that when the exchange is changed the markets (i.e. all its traded pairs) should be reloaded by calling the 'load_markets()'-function.
Calling this function loads all the exchange-traded trading pairs and the are accessable through the 'markets'-property of the exchange class, which is a dictionary holding all the trading pairs. As an example if we want to print all the trading pairs we simply can do this:
For further information on the ccxt library, its features and how to use it please refer to the docs.
Public Exchange APIs Overview
The following table shows my results for requesting OHLCV price data (O: open, H: high, L: low, C: close and V: volume) for the 'BTC/USDT' pair from all exchanges supported by ccxt. The table shows if the exchange has a public API, if it returns data (e.g. some exchanges return errors on request, so they don't return data), if the data is real or emulated, the date of the earliest price data returned and the limit for how many candles can be returned in one request. As hashnode does not support tables I unfortunately need to work around this. Therefore can only include the table as screenshots, as the table is too big to include it as a markdown table. If I find a better workaround for this I'll update the article in the future.