Automated Triangular Arbitrage of Cryptos in 4 steps
Build your own arbitrage trading algorithm in Python
Introduction
Arbitrage takes advantage of the difference in the asset prices in the market. Arbitrage has been traditionally done in the forex market for many years but given the volatility in the crypto market, it can applied to the crypto market as well. Opportunities for arbitrage can exist within the same exchange or across exchanges.
Triangular arbitrage is a technique that tries to exploit the price discrepancy across three different assets at the same time. For example, we can exchange BTC for USDT, BTC for ETH and ETH back to USDT. If the net worth in doing these three trades simultaneously is profitable then the 3 trades are executed simultaneously.

In this article we will be looking into the arbitrage opportunities within the same exchange, in particular we will be deep diving into triangular arbitrage approaches. The focus is to develop and implement a trading algorithm that can identify a profit and trigger the required trade orders for it.
Arbitrage is considered as a lower risk trading method as compared to the traditional trading where the timing of the buy/sell is crucial. Also in arbitrage, the profit/loss is known immediately as all the required trades are executed simultaneously.
Approaches for Triangular Arbitrage
There are different approaches of buying/selling the 3 assets to achieve triangular arbitrage. In this article we shall be considering two approaches.
Approach 1: BUY — BUY — SELL

In the above example, we start with USDT as the initial investment. After performing the 3 trades, we are again left with USDT at the end. Here are the trades that will be performed:
- Buy Bitcoin (BTC) with Tether (USDT)
- Buy Ethereum (ETH) with Bitcoin (BTC)
- Sell Ethereum (ETH) for Tether (USDT)
At the end of the third trade, we can compare the final USDT with the initial investment that we started with in step 1. If this leads to a substantial profit then the 3 trades can be initiated simultaneously.
Approach 2: BUY — SELL — SELL

Similar to the first approach, the same assets are used to check for arbitrage opportunities in a different flow. In the above example the following 3 trades are evaluated:
- Buy Ethereum (ETH) with Tether (USDT)
- Sell Ethereum (ETH) for Bitcoin (BTC)
- Sell Bitcoin (BTC) for Tether (USDT)
Trading algorithm — 4-step implementation
Here is an overview of the different steps to implement a triangular arbitrage trading algorithm. We shall be looking into each of these steps in detail in the next sections.
Step 1: Get all the valid crypto combinations Step 2: Perform triangular arbitrage Step 3: Place the trade orders Step 4: Bundle it together
Before moving ahead with these steps we need to initialise the exchange to do the arbitrage. Select the exchange where you have a trading account and the one that supports api based trading. In this example I have used the WazirX exchange as I have a trading account in this exchange.
import ccxt
from config import myconfig
exchange = ccxt.wazirx({
“apiKey”: myconfig.API_KEY,
“secret”: myconfig.API_SECRET
})The package ccxt supports various exchanges and in case you have an account in any of the other exchanges then you can get the same code working by just changing the exchange’s name in the above snippet. Refer to this page to get the list of exchanges supported by ccxt.
Step 1: Get all the valid crypto combinations
We need a base currency with the initial investment in our trading account to get started. Here we consider USDT as the base currency. Note that even fiat currencies like INR or USD can be considered as the base currency.
There are hundreds of cryptos supported by the exchange and hence we can derive different combinations to perform the triangular arbitrage. We can either hard-code to a limited set of combinations or allow the code to consider all the possible combinations available in the exchange. The below code snippet implements the second approach of identifying all the possible arbitrage combinations.
There are 543 crypto assets supported by this exchange at the time of writing this article.









