Time for a new project!
In this one we will be building a triangular arbitrage scanner, something you could then turn into an entire bot via the help of our last project yourself.
Table of Content
Triangular Arbitrage
Number of Possible Arbs
Scanner Design
Final Remarks
Triangular Arbitrage
Before we jump into building a triangular arbitrage scanner we should first… well… understand triangular arbitrage.
It’s best to explain it through an example.
You have 1 banana.
You can exchange 1 banana for 2 apples.
You can exchange 2 apples for 10 strawberries.
You can exchange 10 strawberries for 2 bananas.
And boom you just doubled your number of bananas!
You could of course have arbitrages like this with longer paths as well instead of just 3 but that’s beyond the scope of this project and would require additional knowledge about graph algorithms.
We can easily calculate if we have a triangular arbitrage the following way:
You have exchange rates COIN1/COIN2, COIN2/COIN3, COIN3/COIN1.
If the product of those 3 exchange rates is bigger than 1 then you have an arb!
Number of Possible Arbs
We will make 1 additional requirement for our scanner / bot which 1. makes returns more stable and 2. greatly reduces the search space of possible triangular arbitrages.
This requirement will be that we need to start and end with a stablecoin. That way we don’t accumulate coins that expose us to market movement.
What we don’t allow is to start with one stablecoin and end with another stablecoin though. That wouldn’t be an arbitrage anymore.
We will once again be doing this on binance.
Let’s say Binance has in total 7 different coins with a spot market between each of those coins, 2 of those coins being stablecoins.
The graph for all possible tri-arbs would then look like this:
In reality though, it will look more like this:
The reason being is that there doesn’t exist an exchange rate between all coins.
You won’t be able to directly exchange AVAX for ATOM for example but you would be able to exchange AVAX for USDT and then USDT back to ATOM.
Or in the case of triangular arbitrage for example AVAX to BNB to BTC to ATOM.
Let’s calculate how many we actually have now when we start with either USDT or USDC.
We first need to get all the spot pairs on binance for that:
import requests
url = 'https://api.binance.com/api/v3/exchangeInfo'
data = requests.get(url).json()
symbols = data['symbols']
spot_pairs = [symbol['symbol'] for symbol in symbols if symbol['isSpotTradingAllowed']]
Quite a lot!
Let’s first get all of the USDT ones and calculate the amount of USDT tri arbs:
usdt_coins = [symbol.replace('USDT', '') for symbol in spot_pairs if 'USDT' in symbol]
Those are all the coins we can exchange to in step 1.
Now we need to count the total amount of pairs that exists between the coins in those list and that will be the number of possible USDT tri arbs.
count = 0
for coin1 in usdt_coins:
for coin2 in usdt_coins:
pair = coin1+coin2
if pair in spot_pairs:
count += 1
The count is 2214 which means that we have 4428 possible Triangular arbs.
For USDC we get 666 so 1332 possible Triangular arbs.
That’s 5760 in total.
Scanner Design
What we will be doing is running the websockets we developed in the last project to keep track of the best bid and ask for all the different spot pairs.
Whenever the best bid or ask price updates for a certain pair we check for all possible Triangular arbs involving that pair.
If there is an arb we print it out and log it in a file in a nice format that shows the path we took, what the exchange rates are and how much total $ and percent profit we make.
We will also include the option to incorporate fees as well as I expect the vast majority of arbs to be below what most people pay in fees.
Final Remarks
We are doing this project in python as this is mostly just a learning project.
If you actually plan on using something like this live or for accurate results then you absolutely want to use a lower level language like C++.
There are some further optimizations we could be making to the scanner design to make it even more efficient.
Imagine you don’t have an arb involving ATOM/BNB because the exchange rate is too low.
If the exchange rate now decreases we don’t actually need to recalculate anything to know that there is no tri-arb.
Optimizations like those are beyond the scope of this project and not the point though.