This content originally appeared on DEV Community and was authored by Neal Chambers
The CCXT library is a powerful Python, JS, and PHP library that can be used to pull information from all of the major crypto asset exchanges.
It has made my life a lot easier since it abstracts out the interface of each exchange. Instead of having to learn each API for each exchange or importing a separate library for each one, I just need to use one module - CCXT.
It also eliminates having to worry about keeping up with all the updates of a particular API. CCXT takes care of all the updates for me, which good because some of these APIs have missing documentation. For example, Coinbase has outdated json responses.
Getting started is easy. Just instantiate an exchange with your api key and api secret, which should have the basic permissions assigned to it. I got mine off of Binance.com.
First, navigate to the site, login and click 'API Management' under your profile:
Click on create API:
Give it a name:
And then you will be met with this screen:
1) Be sure to copy down your API key AND API secret. On Binance and a lot of other exchanges, it might only give you a copy button for the key. And the QR Code may only contain the API key, but, generally speaking, you will need both when programming or doing anything with the API really.
On top of that, you might not be able to copy the secret once you save the new API, so be sure to copy it and save it for later.
2) Optionally, you can 'edit restrictions' and limit what can be done with your API keys. I would start with reading permissions when you are first programming. You do not want to give your program the ability to make orders until you are sure you can lock it down.
3) You can also limit access to certain IP addresses, which is really handy if are accessing the exchange from a static IP. If you are accessing it from home, chances are you will not have a static IP though, so it is better not to check this.
After that you are all set. To initialize the exchange in CCXT, you simply have to use the constructor for it:
from ccxt import binance
client = binance(
{
"apiKey": "the_api_key_from_binance"
"secret": "the_api_secret_from_binance"
}
)
I like to import only the exchange I'm using to keep my code clean and less confusing.
Once you have instantiated the exchange, you can reuse it many times to make calls to the private or public API. For example, you can retrieve historical pricing data in order to run backtests:
results = binance.fetchOHLCV("BTC/USDT", "1m", 1655298673, 1)
print(results)
[
[
165529867000, // UTC timestamp in milliseconds, integer
24235.4, // (O)pen price, float
24240.6, // (H)ighest price, float
24230.0, // (L)owest price, float
24230.7, // (C)losing price, float
37.72941911 // (V)olume (in terms of the base currency), float
],
...
]
There are numerous functions you can call on an exchange like this. For a full list, be sure to check the CCXT documentation.
Conclusion
If you are anything like me, you use more than a few different exchanges when you trade crypto assets. In this world, of ever-changing policy and sudden crashes, you never know when you will have to jump off an exchange and move to another, so it is good to have more than one account.
That's where learning a library like CCXT once comes in handy. You don't have to learn multiple libraries or have multiple dependencies for your project. It can also be used in javascript or PHP. Give it a try and let me know if it works for you.
This post includes affiliate links; At no additional cost to you, I may receive compensation if you purchase products or services from the links provided in this article. Buying, selling, and trading crypto assets involves risk. Always Do Your Own Research before investing any money. This post is Not Financial Advice.
This content originally appeared on DEV Community and was authored by Neal Chambers
Neal Chambers | Sciencx (2022-06-15T13:39:46+00:00) Working with CCXT Python Library. Retrieved from https://www.scien.cx/2022/06/15/working-with-ccxt-python-library/
Please log in to upload a file.
There are no updates yet.
Click the Upload button above to add an update.