What you need to write a crypto trading bot — part 2

Paul @ Crypto Bot Co
3 min readDec 20, 2020

If you followed part 1 of this series, you should now have an exchange account, API key and a test program that can communicate on your behalf with your exchange account. It’s tempting at this point to just start coding trading logic, but let’s get some data first.

Modes of operation

In order to have confidence that you bot is working properly and more importantly making a profit, it is critical to be able to replay historic data and test your bot. The ease with which you can replay and test is what will lead to your ultimate success.

My bots have three main modes:

  1. Replay = load historic data, make synthetic trades, calculate profit / loss
  2. Test = retrieve live data, make synthetic trades
  3. Live = retrieve live data, make live trades

Retrieving historic data

I have a separate program that retrieves historic data and stores it in files for me. I don’t want to call my exchange’s API every time I want to replay or test; retrieving data is slow, and I don’t want to hit an API limit.

Each API will have a different way to retrieve historic data. If you are using the Binance Python module, you can retrieve historic klines using this command:

history = client.get_historical_klines('ETHBTC', '1h', '2020–12–01 00:00')

This will retrieve hourly candles and other data for ETHBTC from the start of December 2020. Unfortunately, it is unlabelled data that looks like this:

[[1606780800000, ‘0.03131000’, ‘0.03142900’, ‘0.03124000’, ‘0.03127500’, ‘4938.22100000’, 1606781699999, ‘154.72123206’, 3167, ‘2318.47800000’, ‘72.64917475’, ‘0’], ..]

For convenience, the column names are as follows:

OpenTime,Open,High,Low,Close,Volume,CloseTime,QuoteAssetVolume,NumberOfTrades,TakerBuyBaseAssetVolume,TakerBuyQuoteAssetVolume,Ignored

I store the rows of data in a CSV file based on ticker and timeframe, but before that I convert the open/close times as follows to make them human readable:

datetime.fromtimestamp(line[0] / 1000) # Epoch to datetime object

Dealing with prices and volume

It’s important to note that prices and volume for most APIs are strings not floats. This prevents floating point precision inaccuracy, i.e. “0.03124000” becoming “0.031240000000002”. If you try to use equality operations on floats, you risk inaccurate results. e.g./

float("0.03124000") == float("0.03124000") # may be False

You can read more about float precision and Python’s recommended solution.

What we covered

If you followed along, you will now have a method of retrieving historic data that you can use on your bot to try synthetic trading and calculate profitability.

Coming up next

Next we will look at constructing your bot to make use of historic and live data.

--

--