How to build your own monitor from scratch that will find arbitrage opportunities in crypto markets?

Let’s build our own monitor from scratch. It will look for instant arbitrage opportunities.

We will use the Python ccxt library that allows us to connect to various exchanges (115 to be precise) and trade crypto in a standardized way. We are going to use these bookstores to consult the purchase / sale prices of different assets in different exchages, compare them and then program the arbitrage strategy.

Let’s start First of all, import the bookstores:

Import numpy as np import ccxt 1 2 import numpy as np import ccxt

We will use numpy later to perform the calculations. Now, let’s define some exchanges:

exchanges = [“Allcoin”, “Binance”, “Bitfinex”, “Bittrex”, “Cex”, “Cryptopia”, “Exmo”, “Gatecoin”, “Hitbtc”, “Huobipro”, “Kraken”, “Kucoin” , “Livecoin”, “Okex”, “Poloniex”, “Qryptos”, “Quadrigacx”, “Southxchange”, “Yobit”] 1 2 exchanges = [“Allcoin”, “Binance”, “Bitfinex”, “Bittrex”, “Cex”, “Cryptopia”, “Exmo”, “Gatecoin”, “Hitbtc”, “Huobipro”, “Kraken”, “Kucoin”, “Livecoin”, “Okex”, “Poloniex”, “Qryptos”, “Quadrigacx” “,” Southxchange “,” Yobit “]

With exchanges defined, we can now initialize the corresponding clients to ask for the data. You can define it like this:

allcoin = ccxt.allcoin () 1 allcoin = ccxt. allcoin ()

Or better yet, they can do it to avoid writing so much:

clients = [getattr (ccxt, e.lower ()) () for and in exchanges] 1 clients = [getattr (ccxt, e. lower ()) () for and in exchanges]

Then clients is a list that contains all the clients we need. Now, let’s define some interest pairs:

symbols = [“ADA / BTC”, “BCH / BTC”, “BTG / BTC”, “BTS / BTC”, “CLAIM / BTC”, “DASH / BTC”, “DOGE / BTC”, “EDO / BTC” , “EOS / BTC”, “ETC / BTC”, “ETH / BTC”, “FCT / BTC”, “ICX / BTC”, “IOTA / BTC”, “LSK / BTC”, “LTC / BTC”, “ MAID / BTC “,” NEO / BTC “,” OMG / BTC “,” QTUM / BTC “,” STR / BTC “,” TRX / BTC “,” VEN / BTC “,” XEM / BTC “,” XLM / BTC “,” XMR / BTC “,” XRP / BTC “,” ZEC / BTC “] 1 2 3 symbols = [“ ADA / BTC “,” BCH / BTC “,” BTG / BTC “,” BTS / BTC “ , “CLAIM / BTC”, “DASH / BTC”, “DOGE / BTC”, “EDO / BTC”, “EOS / BTC”, “ETC / BTC”, “ETH / BTC”, “FCT / BTC”, “ ICX / BTC “,” IOTA / BTC “,” LSK / BTC “,” LTC / BTC “,” MAID / BTC “,” NEO / BTC “,” OMG / BTC “,” QTUM / BTC “,” STR / BTC “,” TRX / BTC “,” VEN / BTC “,” XEM / BTC “,” XLM / BTC “,” XMR / BTC “,” XRP / BTC “,” ZEC / BTC “]

They can modify the previous variables according to their preferences. Then we define other useful variables:

ask = np.zeros ((len (symbols), len (clients))) bid = np.zeros ((len (symbols), len (clients))) 1 2 ask = np. zeros ((len (symbols), len (clients))) bid = np. zeros ((len (symbols), len (clients)))

Finally, we ask for the customer’s data. We will use the fetch_order_book function in each client, which will return the complete order book, but we will only focus on the latest purchase / sale prices (this is a first proposal, we can explore better options by calculating the average value of our trade ).

Executing the arbitrage monitor

Assuming they have not modified the exchanges listing, the next lines may take a while. To accelerate it, let’s define the client list again.

for row, symbol in enumerate (symbols): for col, client in enumerate (clients): try: book = client.fetch_order_book (symbol) ask [row, col] = book [‘asks’] [0] [0] bid [row, col] = book [‘bids’] [0] [0] except: pass 1 2 3 4 5 6 7 8 9 for row, symbol in enumerate (symbols): for col, client in enumerate (clients): try: book = client. fetch_order_book (symbol) ask [row, col] = book [‘asks’] [0] [0] bid [row, col] = book [‘bids’] [0] [0] except: pass

I have defined the order within a try block because some symbols are not traded in all exchanges, and incorrect orders carry annoying errors. Another source of errors could be the rate request limits that, for the moment, we are leaving it aside. We can also implement a delay to reduce the frequency of orders for each exchange. Esee delay should be adjusted depending on which exchanges we are connecting to, because each one has its own limitations. To deepen this, you can check the documentation of each exchange. I have also defined the ask and bid ndarrays in the try block since some orders can return empty arrays, and for now we are trying to avoid that.

In a more efficient approach, we do not want to wait until the entire loop finishes looking for an arbitrage opportunity. But for now, let’s leave it that way and we will continue to explore the data to evaluate what our best options may be.

Having defined the purchase and sale prices with the ask and bid values for each currency in each exchange, we can now define our strategy and calculate our gains and losses. Our strategy to trade the C currency between exchanges E1 and E2 involves buying C in E1, transferring currency C to E2 and selling it there. We want to carry out all these steps as quickly as possible because we do not want someone to go ahead of us. So we are going to execute each trade immediately, where it immediately means buying at the lowest price that someone is willing to sell. So let’s define the steps:

In E1, I place the purchase order for C.

I transfer the currency C to E2

In E2, I put a sales order for C.

I calculate the return on investment (ROI) in BTC:

$ ROI = \ left \ {\ frac {bid_ {E2}} {ask_ {E1}} — 1 \ right \} \ cdot 100 \% $

In practice, they should also pay fees at the exchanges. So the ROI would be better defined like this:

$ ROI = \ left \ {\ frac {bid_ {E2} \ cdot (1-fee)} {ask_ {E1} \ cdot (1 + fee)} — 1 \ right \} \ cdot 100 \% $

Then, we will define the variable fee to calculate the ROI. We can define an array or a dictionary of fees for each exchange, but for now, in order to simplify, we are only going to define a general percentage (conservative) of fee.

fee = 0.25 1 fee = 0.25

Finally, we should also discount the commission per transaction. But for now we are going to proceed without taking this into account, because the commissions depend on each currency and the load of the network at that moment, and now we are going to worry about that.

Let’s calculate it, and count how many profit opportunities we have found:

opportunities = [] for i, symbol in enumerate (symbols): for j1, exchange1 in enumerate (exchanges): for j2, exchange2 in enumerate (exchanges): roi = 0 if j1! = j2 and ask [i, j1]> 0: roi = ((bid [i, j2] * (1-fee / 100)) / (ask [i, j1] * (1 + fee / 100)) — 1) * 100 if roi> 0: opportunities. append ([symbol, exchange1, ask [i, j1], exchange2, bid [i, j2], round (roi, 2)]) print (“Number of profitable opportunities:”, len (opportunities)) 1 2 3 4 5 6 7 8 9 10 11 12 13 14 opportunities = [] for i, symbol in enumerate (symbols): for j1, exchange1 in enumerate (exchanges): for j2, exchange2 in enumerate (exchanges): roi = 0 if j1! = j2 and ask [i, j1]> 0: roi = ((bid [i, j2] * (1 — fee / 100)) / (ask [i, j1] * (1 + fee / 100)) — 1 ) * 100 if roi> 0: opportunities. append ([symbol, exchange1, ask [i, j1], exchange2, bid [i, j2], round (roi, 2)]) print (“Number of profitable opportunities:”, len (opportunities))

We have found 108 opportunities !. Promising, right? We are going to order them by their origin and observe which are the best:

opportunities = sorted (opportunities, reverse = True, key = lambda x: x [5]) print (opportunities [: 10]) 1 2 opportunities = sorted (opportunities, reverse = True, key = lambda x: x [5]) print (opportunities [: 10])

At the time of writing this article, the best option is to buy QTUM / BTC in Bitfinex and sell them in Qryptos. For each QTUM / BTCtradeado, we can obtain an ROI of 75.45%

Now that we have found some opportunities, we can begin arbitration. The first opportunities that we have found involve all the QTUM in certain exchanges and selling them all in Qryptos. The return of that arbitration is around 75%. 75% !!! Trade all day and become rich.

You can build your Bot from scratch or enjoy AiTBot Instant Arbitrage Bot, that provides his investors with a hybrid arbitrage trading platform, that enables them to buy and sell digital currency through his debit or virtual card in offline & online global markets. Access 200 exchanges from one single account.

AiTBot API integration can load the Debit Card you currently have in your pocket. Instantly load your EXISTING CARD issued from your local bank, ANYWHERE in the world using cryptocurrencies.