Margin trading robot on the BitMEX exchange

Ilia Krotov
5 min readMay 22, 2018

--

Hello everyone!

My name is Ilia and today I want to tell you a little about my hobby — crypto-algo-trading. Soon there will be a year when I thought of writing a trading robot that would minimize the human factor of trade (people who trade for sure know what it is every five minutes to update the balance and often make some hasty, and therefore incorrect, trade decisions). Therefore, it was decided to shift everything to a robot, remove applications from viewing the exchange trade rates from the phone and start sleeping peacefully. Having spent a lot of time writing something more or less working, I want to give the reader a little insight into where to begin on this funny (and nervous) area, like algotrade. This guide is not an invitation to start trading, does not contain investment advice, only educational goals are pursued.

As you can see from the header, the robot that we will write will work on the BitMEX exchange. The choice is very simple — there are leverages up to 100. And our robot will work with margin leverage.

Example of potential ROI with margin. Image is taken from https://www.tdameritrade.com/account-types/margin-trading.page.

So, I hope that the reader has a basic knowledge of trading on the exchange, for example, what is OHLCV candles, that there are different time intervals for their representation, etc.

Well, let’s start our little adventure in the country of algorithmic margin trading?

I’m a fan of top-down communication and so I’ll start by telling you what I’m going to do, and then we’ll start to implement. Globally, there are two important things that you need to do to start a robot: develop a decision strategy (under what conditions to buy) and develop logic over the decision (how much and how to buy).

The decision-making strategy (the brain) in our case will consist of the indicator MACD (Moving Average Convergence / Divergence — convergence / divergence of moving averages). The indicator is used to check the strength and direction of the trend, as well as determine the turning points. It is built on the basis of moving averages (https://en.wikipedia.org/wiki/MACD). There are two modifications of the MACD indicator: linear MACD and MACD-histogram. We will use the realization of the MACD-histogram from the TA-lib library and on the basis of this indicator we will decide whether to buy or sell.

MACD example from Wikipedia (https://en.wikipedia.org/wiki/MACD).

The logic over the decision of our indicator will be simple: if the indicator said to buy, then we will buy, if said to sell, then we will sell. We will trade with a fixed amount of money with a fixed leverage.

The first thing we’ll start with is setting up the right libraries. To begin with, we need a stock exchange client, we take it here (https://github.com/BitMEX/api-connectors/tree/master/official-http/python-swaggerpy). The brain of our robot will be based on technical analysis, so we need to put the library TA-lib as well. And, of course, the standard set like numpy, pandas.

After installing all the necessary libraries, I suggest that the reader register at https://testnet.bitmex.com — this is a copy of the BitMEX exchange, but there you do not trade real money, but virtual money. It is very convenient to use for debugging robots before running on a real exchange. After registration, we create our own keys and create a stock exchange client:

Note the test parameter. It is set to true, which means that we will trade on the test exchange. This post will not be about using the API of the exchange, you can redirect all the arising questions to the API explorer.

The next step is to get the data to use on the MACD indicator. We will get 100 candles with a resolution of 1 hour.

Now we have a dateframe with candles, let’s apply the indicator.

I chose such parameters a little playing around with the strategy and its backtest on the Trading View. I recommend you to do the same. But here you can find some of my results.

So, having received the indicator, let’s understand how to make a decision on its basis. Well, everything is simple. When the histogram crosses the zero value from the bottom up, this is a signal to buy, and vice versa.

Everything is very simple and understandable. Let’s put it in one class, which will be a strategy.

Now let’s write the executor of our solutions. The logic is simple — the strategy told us to buy, so we buy. I’ll quote the trader’s code at once, there’s nothing complicated in it:

A small nuance — to simplify your life, orders are executed here not at a pre-set price, but at a market price.

Now we have the logic for making a decision, we have the logic for executing decisions, let’s arrange the whole thing together. We need to start the trading process once in the chosen time period, in this case it was 1 hour. I’ll do it in a inefficient way for speed of writing, but it’s better to do it, for example, through cron.

Well, now this can be formalized in one script and run, but only on the test exchange(!), not on the real exchange, because you can lose money. This script aims to show that it’s easy to start, and made a smarter brain in this model, you can easily earn some btc.

Code I posted to my repository, if someone is interested in viewing — you are welcome! https://github.com/IlyaKrotov/BitMEX-simple-trading-robot

Also I recommend you to use my affiliate link for your registration on BitMEX.

https://www.bitmex.com/register/dduBF7

Users who have signed up with a valid affiliate link will receive a 10% fee discount for 6 months and my thanks :)

--

--