How to build an AI crypto robo trader on binance in python with jupyter notebook. Episode 1#

Sacha Dumay
Nov 7, 2018 · 3 min read

All the tools for the entire serie are:

  • Pandas, Numpy
  • CCXT library for connecting to 132 cryptocurrency exchanges
  • TA-Lib libary for creating trading indicators
  • Keras for creating LSTM model with Tensorflow Backend

Goal of the end of the serie:

  • Build a Deep Q learning model to create the best trading strategy.

First how to connect to Binance or other exchanges?

I have made the mistake myself to start a robo trader on Bithumb by using their own API. Two months later realizing that as a foreigner in South Korea you cannot withdraw in fiat anymore… So I had to change my exchange. At that time, most of my work to connect to Bithumb were useless.. Horrible feeling. But I found the solution.

After searching the best way to connect to any exchange, I found the library CCXT. It is a MIT license so you can freely use it for any personal or business project. It is well maintain by a great community. The commun nomenclature to access data from more than 132 exchanges as today date is so useful. Let’s say if you decide to change your robot to connect to another exchange, you will probably only change 1 line of your code. Magic right!!!

You can easily get historical candelstick data points, get access to your own account to trade and check your balance as well as withdraw. It all depends on the capability of each exchange.

Please see the code below for an easy install and set up.

Secondly, for our future machine learning, we need data, more precisely historical data. We will train our ML model with it and also purely backtest our strategy (i.e model) to check our performance before going live.

Let’s start to retrieve the last 6 month of the pair “BTC/USDT” with 1 hour interval. After getting this data, let’s save it as a csv file (simpler version of an excel file) to get fast access during our project and to always work with the same data.

It’s a basic step to make sure we understand what we are doing. On binance I have noticed two pain points with data:

  • There are clearly some missing data.
  • If you are getting data from low volume pair or high frequency around the minute, you might have some candelsticks with a 0 value for volume. 0 values will be an issue later on.

How to pre-process/clean our data. Let’s retrieve our data from the csv file we just created.

Now we need to check if there is any missing value. If it is so, we use a fill forward method which basically relpicated the last value over and over until the next valid value (not NaN or empty) occur in the dataset. We will also make sure that volume data isn’t equal to 0. We can replace it by a very small number like 0.0000000001.

Our dataset includes 4 columns as follows : open, high, low, close, volume with date and time as an index. We are ready to create trading indicators out of it. The best library I found is TA-Lib. The current version is based on Cython which makes it a very fast python library to use. TA-Lib includes more than 150 indicators such as ADX, MACD, RSI, Stochastic, Bollinger Bands, etc. It has also most a the candelstlitck pattern recognition possible.

In the next post I will explain how I have selected the best indicators for differentes trading scenario.

I would be happy to hear any feedbacks or questions from you.

Welcome to a place where words matter. On Medium, smart voices and original ideas take center stage - with no ads in sight. Watch
Follow all the topics you care about, and we’ll deliver the best stories for you to your homepage and inbox. Explore
Get unlimited access to the best stories on Medium — and support writers while you’re at it. Just $5/month. Upgrade