Cryptocurrency Trading Bot


A bazillion years ago, when I worked at Morgan Stanley. There was a big sign above the entrance and exit to the trading floor:  “DID YOU MAKE MONEY TODAY?”  If you are in finance that is the only metric that matters.

This summer I wrote a paper trading bot to arbitrage bid/ask spreads across different crypto exchanges. Here is the git repo – it is in progress and there is one glaring bug so if you run it you will lose $$.

So lets talk a little bit about what is going on here and how these exchanges work.

Well someone offers to buy 3 bitcoin for $100, someone else offers to buy .5 bitcoin for $105 and so forth. This builds one side of the order book, and the different quantities and prices represent the depth of the order book.

Then there is the other side of the order book, someone offers to sell 2 bitcoin for $200, another person offers to sell .5 bitcoin for $195.

The trade is made when someone else offers to take the other side. If I come in and say yes! I will buy 2 bitcoin from you for $200. Bam the trade is made and that is removed from the order book.

Here is an example json of the order book from hitbtc, an exchange where you can trade crypto.

Now the bid/ask arbitrage is when you can lock in a profit. When on one exchange you can buy 2 bitcoin for $200 and immediately sell it for $205. This will not happen on the same exchange but it can happen on different exchanges, so if you have a script that is constantly monitoring a bunch of currencies and a bunch of exchanges.

This does not happen in regular exchanges for normal people like me, and probably you if you are reading this blog instead of something on your bloomberg terminal. These types of trades happen very quickly. For traditional exchanges, you want to be as close as physically possible to the exchange, and so forth.

For the crypto exchanges, there is no physical exchange, except perhaps for Gemini, which I think has a physical exchange. So there is no direct pipe to the exchange. The reason why you can arb in this way is because of the Risk.

So this is the other big idea in trading RISK! What kind of risk are you taking, and what is your risk versus your reward. Theoretically if you are executing the trade instantaneously then what is the risk?

As Milton Friedman said – There aint no such thing as a free lunch.

Aint that the truth! When you are making all this money on this supposedly risk free arbitrage you are still taking on risk but what is that risk?

Well in the case of these crypto exchanges you have exchange risk. The exchange may shut down, may be hacked, you can lose all your money. That is the big risk and you are being compensated by it with the arbitrage trade.

There is also currency risk. If you trade your crypto into some sovereign currency (USD, EUR, etc), there is a high transaction cost and you will lose money. In order to make money you have to keep your money in crypto and maybe exchange into USD once a day. This means you have exposure to the directionality of the crypto. You may make money on each trade, but if crypto goes down one day it does not matter because you will have lost money relative to the dollar.  You can probably mitigate this with options, but it is something to consider.

Other things to consider are: the percentage the exchange takes on each trade, slippage, that is if you are unable to execute at the price you want, if the market moves and you can only execute one side of the trade and so you have to unwind the trade of keep the position on. But these are run of the mill trading issues. There is nothing special about it being in crypto.

This is interesting. I have been looking into deep learning algorithms for predicting crypto prices but it is difficult because time series data is autocorrelated. Each tick or trade is based on the previous trade. I want to look into this aspect in greater detail. I am not interested in numerical methods.

Leave a Reply