Crypto Arbitrage Trading Bot

A bot which traded multiple crypto currencies at various exchanges in an ever to make profit via arbitrage

crypto currency

algorithmic trading



I've had an interest in crypto since my friend introduced me to Ethereum back in 2016. Near the end of 2017, the price of most crypto currencies began to sky rocket, in what later came to be known as the Oh My God We're All Going To Be Crypto Millionaire Bull Run of 2018. It was a topic of frequent discussion between myself and a couple colleagues of mine who were also interested in crypto currencies. One day, someone threw out the idea of building a trading bot. We looked at each other and shrugged. Why not?

Luckily, one of my colleagues had experience in fintech and knew a thing or two about algo-trading. After some discussion, we settled on using arbitrage as our strategy.

Arbitrage Overview

As defined by Investopedia, arbitrage is the simultaneous purchase and sale of an asset to profit from an imbalance in the price.

Let's take an example. Imagine the price of a gallon of milk is $2 in Boston but just $1.50 across the river in Cambridge (I don't drink milk so if that's way off take it up with the cows). I could buy 40 gallons of milk with $60 in Cambridge, transport my milk to Boston and sell the 40 gallons for $80, netting a nice $20 for myself. In real life you'd have to factor in the cost of transporting and keeping the milk chilled, but you get the idea.

If you kept doing this, the price of milk would gradually converge at both cities. This is because we'd be moving supply away from Cambridge and to Boston. Less supply in Cambridge would drive prices up, and more supply in Boston would drive prices down. In this way, arbitrage is a positive force in markets, since it eliminates arbitrary price differences for consumers.

Crypto Arbitrage

It's the same idea with crypto currencies. There are many difference exchanges where people can buy or sell crypto currencies, such as Kraken or Gdax. The price of a particular crypto currency at one of these exchanges is determined solely by what people are willing to pay for it. Since the people at Gdax are not the same as the people at Kraken, there will be differences in the price from time to time. If you can buy a coin cheaply at one exchange and sell it for more at another exchange, you turn a profit.

The situation becomes a little more complicated in practice. The price of these coins are fluctuating at intervals of a fraction of a second, and actionable price differences rarely last longer than a single trading interval. This is because there are a lot of other bots taking advantage of arbitrage opportunities. This means that there is not enough time to buy at one exchange, move it to the other exchange, and sell it there. To overcome this, our strategy was to have crypto deposits at each exchange. If we saw an actionable price difference, we'd immediately buy and sell at both exchanges.

Using the milk example, let's assume the price of milk is usually the same in Cambridge and Boston, but occasionally diverges. We keep 100 gallons of milk in each location. Let's assume that the price goes up in Cambridge by 10 cents. We would sell 10 gallons of milk in Cambridge at the higher price, and buy 10 gallons of milk in Boston at the lower price. Overall we still have 200 gallons of milk combined. But we've now gained 10 gallons * 10 cents = $1 of profit.


We built the application using Ruby. Here is a rough overview of how the code worked.

  • Retrieve buy and sell orders from both exchanges.
  • Sort buy orders from most expensive to least expensive, and vice versa for sell orders.
  • Compare buy orders from one exchange with sell orders from the other exchange. If the trade would yield positive revenue delta after factoring in fees, add it to a queue of possible trades. Do this for for both sets of buy/sell order lists.
  • Fill all possible trades for which we have enough units of the particular coin at each exchange.


Since there was going be real money on the line, we wanted confidence that our solution would work as expected. To facilitate this we added a test mode, where the application would use fake money and make fake trades. We even simulated trade failures at different probability levels, to account for things like failed network requests or trying to make a trade that was no longer available.


The bot ran for a month in total. In that time it executed over one hundred trades, of which 75% were successful. However, the remaining 25% of trades failed due to network failures, or the trade no longer being available. The 25% of failures proved to be too costly, and almost completely zeroed out any gains we made from our successful trades. We decided that our profit margins were too thin to compensate for the risk, and we turned off the bot.

But hey, now I can say that I built a crypto currency trading bot that made me money. Pretty cool!