# Automated Triangular Arbitrage of Cryptos in 4 steps **Published by:** [dejectedPlover2](https://paragraph.com/@dejectedplover2/) **Published on:** 2022-04-30 **URL:** https://paragraph.com/@dejectedplover2/automated-triangular-arbitrage-of-cryptos-in-4-steps ## Content Build your own arbitrage trading algorithm in Python 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. There are different approaches of buying/selling the 3 assets to achieve triangular arbitrage. In this article we shall be considering two approaches. 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: 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. 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: 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 combinationsStep 2: Perform triangular arbitrageStep 3: Place the trade ordersStep 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. 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. 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. Next extract all the possible combinations to apply the BUY-BUY-SELL and the BUY-SELL-SELL approaches of triangular arbitrage. There are 63 different arbitrage combinations that the code was able to identify. Eg: If a BUY-BUY-SELL algorithm has to be applied on the first entry, then: If this circular trade can lead to a profit, then we are good to execute the 3 trades simultaneously. Pull the prices of the three assets from the exchange and identify the final price after performing the three buy/sell conversions. Similarly the BUY-SELL-SELL approach also needs to be implemented. Only a snippet of the code is provided here to avoid code congestion. Please refer to the git repository linked in the end of the article to get the complete executable code. Calculate the profit/loss in performing this triangular arbitrage by considering the exchange’s brokerage for each transaction and the minimum profit expected from the trade. Here is the code to execute the three trades on the exchange. If market price trade is not supported by the exchange, then a limit price trade needs to be executed. Limit price orders can sometimes cause the trading order to be stuck if the price has fluctuated before the execution of the order. Wrap the above steps to a single package and run it for all the possible combinations. For each combination perform the below 3 steps(Example: Buy — BTC/USDT, sell — ETH/BTC, sell — ETH/USDT) Here is a sample result on running this code for one iteration. An initial investment of 100$ was considered. With 63 combinations and 2 approaches, a total of 126 arbitrage combinations were checked and 26 of them showed a profit as below. Note that the above table is from the logs and not from actual trades that were executed. Though this table shows a rosy figure, it may not always be a smooth ride in reality. Refer to this git repo to access the complete code: TriangularArbitrageCryptos Based on the exchange and the api support that it provides, the trading algorithms can be tuned further. Try playing around with the code by changing the base currency, using market price trades or checking on other arbitrage approaches. Again, the disclaimer, this article is only for educational purposes and for you to understand the different types of trading options available. Remember that you are competing with several other trading bots out there. In case you want to experiment with real trades then first ensure that you have built a robust trading algorithm before venturing into it to avoid losses. Additionally, if you are interested in wrapping this code within a trading bot application, use this article as a reference: Building a basic Crypto Trading Bot in Python Thanks for reading till the end :-). Happy experimenting!! ## Publication Information - [dejectedPlover2](https://paragraph.com/@dejectedplover2/): Publication homepage - [All Posts](https://paragraph.com/@dejectedplover2/): More posts from this publication - [RSS Feed](https://api.paragraph.com/blogs/rss/@dejectedplover2): Subscribe to updates