3 Things you need to learn when starting to algo trade on CCXT


What is CCXT?

CCXT is a library that you can connect with the top exchanges for the purpose of algorithmic trading, this includes FTX, Coinbase, Kraken, Phemex, and more. CCXT is very easy to use, making it simple to connect and consolidate all of your functions to make your trading experience as smooth as possible.

1. Getting connected to the exchange

Connecting with CCXT is a bit different compared to other libraries out there, so we’ll be sure to walk you through the process properly. Firstly, you need to import CCXT by writing “import ccxt”.

Next, we need to pick an exchange to connect to, for the sake of example we are going to go with Phemex, but it’s really up to you. Firstly we need to put Phemex into the code along with enabling the rate limit. Doing this with the rate limit isn’t necessary but it’s a good practice for safety purposes.

Lastly, you need to get your API Keys, for this example below, the keys will be imported from a file called dontshare_config, in order to hide them from the screenshots. For you personally, you can either write your key in there or import a file in the same way that we have. Once you have imported both your keys as you will see below, we can then move on to the next step: fetching data.

Fetching your balance and other data

To fetch your balance, we need to enter the code “print(phemex.fetch_balance())”. When typing in the command, you’ll be presented with an array of different data that you may want to print, It’s worth taking a look through this in your own time to get familiar with how it all works and how the information is displayed. In this case, we are going to use “fetch_balance” of course.

2. Getting the bid and ask price

To get the bid and ask price, we first need to find the order book, this is done by typing “def get_bid_ask():” then “btc_phe_book = phemex.fetch_order_book(‘uBTCUSD’)” The text below is how it should usually look to get the order book, though it can change depending on the exchange. Using contracts is a good idea as they have credits when you make limit orders.

Looking at the order book, we can tap into it to look at the best bids. We want to look at the 00 indexes. We also want to do the same for the best ask so that we can see that too. The code here shows us how we can find these indices.

When we run the code, this is how it looks as it displays to us the best bid and ask price.

Finally, we can return both the bid and the ask down below. Just add the line below in the code stating “return btc_bid, btc_ask”.

3. Post only orders

When you put in a market order it will cancel, the current code can only use limit orders. We want to be using limit orders as they have lower fees. The first thing you want to do is set your parameters like the code below. Once again, this code is specific to using Phemex, so it may be different if you are using Kraken or Binance etc.

When you put in a market order it will cancel, the current code can only use limit orders. We want to be using limit orders as they have lower fees. The first thing you want to do is set your parameters like the code below. Once again, this code is specific to using Phemex, so it may be different if you are using Kraken or Binance etc.

Time to make our limit order, we need to go above our parameters and start declaring the variables that we are going to use “symbol = ‘uBTCUSD’ and “pos_size = 1” above your parameters and position size, followed by your command to get the best bid ready: mybid = get_bid_ask()[0]. I am also going to input “mybid = mybid – 1000” as there is currently an algorithm running outside of this that we don’t want to disrupt. Next, we enter the code which creates the order: “phemex.create_limit_buy_order(symbol, pos_size, mybid, params)”, when typing in the code you are presented with a range of different orders you can create. In this tutorial though the focus is solely on limit orders. As you can see in the final line of code, we added some additional context to our order, this way it provides us a message when the order is placed.

Wrapping things up

Beginner tutorials like these can often be hard to follow since there are so many small things present that you may have not seen before, so to conclude this tutorial, here’s a reminder of what we covered today:

  • First, we covered how to connect to the exchange, this is obviously essential to any form of algorithm trading so it’s important you have a good understanding of this.
  • Next, we showed you how you can find the best bid and asking price, this is once again very important as you will be using this info to execute buy and sell orders.
  • Lastly, we showed you how you can submit an order.

These three things are the first steps you need to learn to ensure that you have a successful and profitable time as an algorithm trader. Working with something as volatile as crypto, it’s essential that you have all the basics learned to a very strong level, in order to leave little to no room for human error.

want to learn how to algo trade so you can remove all emotions from trading and automate it 100%? click below to join the free discord and then join the bootcamp to get started today.


Leave a Reply

Your email address will not be published. Required fields are marked *