Well, this was quite a journey, especially as I’m still on it. I’m still going the trough the difficult process of getting the bot to scale to higher trading amounts. However, I have learned a lot getting the bot profitable at the low end of the trading spectrum and I thought a lot of people could learn from my experience.
Before we get started, I have to do some shameless plugs, have a look at its twitter account @CryptoBot_0. If your keen on running the bot (at your own risk of course) check out this article I wrote titled How to use my arbitrage bot to make a profit (maybe). As always don’t forget to follow me on Medium and Twitter to keep on learning about the next era of the internet.
So maybe let’s start at the beginning, I’ve been wanting to write about web3 for the longest time, but I never got around to it. One day I just decided, its time. I had been writing web3 projects for a while (mostly just weird weekend projects). This meant that I had the skills to write smart contracts, interact with them, and fork existing projects. But instead of starting with a rather advanced tutorial like “step 1 fork Uniswap, step 2 profit”. Although that is a great title for the future. I thought it would be much better to start off small.
So, A few months ago I began writing an Arbitrage trading bot. It started out very slow, and actually lost money due to all the times it broke down. But as I was now doing the teaching thing for real, I thought it’s time to get the bot working properly and share it with the world. Hence the GitHub repo. Once I put the bot in public, it felt as if I had a new found pressure to turn Web3ArbitrageBot into the best Arbitrage Trading Bot available. Now I know that its still a far way off, I said it in the first sentence of this article. But over the past few weeks Web3ArbitrageBot has improved leaps and bounds.
The first big improvement came from me doing some deep dives on the different kinds of arbitrage. Specifically Triangular Arbitrage, this is the result of a discrepancy between three or more assets, cryptocurrency in our case, that occurs when the currency’s exchange rates do not exactly match up. Web3ArbitrageBot takes advantage of this by scanning various decentralised exchanges as quickly as my computer will allow it, and has actually proven to be somewhat profitable on the Velas Blockcahin when trading small amounts of money.
At this point I was still stuck trading on a single exchange. This is the root cause the lost money that I alluded to above. Essentially what would happen, most of the time, when trading between multiple exchanges. Is the fact that the trade would be successful on the first exchange, but fail on the second exchange. This was such a nightmare because I needed to become sherlock homes to discover what crypto I was stuck with, then trade it back to my base currency usually at a loss.
The saving grace for this problem was the inclusion of a new smart contract Arbitrage.sol. Smart contracts are little pieces of code that are executed on the blockchain itself, in exchange for payment. Arbitrage.sol was amazing as it allows me to execute trades across multiple blockchains with a single call to the blockchain. This also improves trades on a single exchange as previously 2 calls to the blockchain were needed to execute a trade. This is only possible because I discovered that smart contract function calls are atomic, sorry it’s not a more official source. But good I think it’s good enough for a blog post. Anyway, this is very important for arbitrage trading, as it means that if a trade succeeds on the first exchange, but fails on the second. Then the entire operation is reversed. No more holding bags of tokens that you don’t know you have. No more sleuthing.
The inclusion of Arbitrage.sol was a massive performance and stability improvement as now if the bot was to fail I would only loose gas fees. And I would also be able to execute more transactions per second. Now I haven’t measured this improvement but anecdotally its was noticeable. However, I would also come to realise something almost equally powerful.
The fact that Arbitrage.sol operations are atomic, and it holds its own trading wallet. Means that in the case where multiple trades are placed simultaneously, they would simply be executed in the blockchain sequentially. Do you see where I’m going with this… Its threads, it’s always threads. Threading allows you to execute a new operation while a previous operation is pending some response (in our case, it was a network call to the blockchain). This means that we could effectively remove the waiting time between blockchain calls, now this was game changing and once again no empirical evidence, but anecdotally it was faster, much faster.
Currently we are investigating several avenues, and they all pretty much have the effect of making the bot more time efficient, meaning that it will be able to more quickly react to the formation of an arbitrage opportunity. This involves, multiprocessing (similar to multithreading, except processes can execute at the same time as long as you have free CPU cores) creating more efficient trading circuits with some data analytics, and having the bot run indefinitely on some hosted server (its currently on my old half broken laptop).
If you want to get involved, please feel free to contribute to Web3ArbitrageBot, or simply reach out on Twitter @Kyle_Jordaan3 and tell me about how you would improve this arbitrage bot. If you would like to support this project please just like this story, and follow me here and on Twitter @Kyle_Jordaan3. This project is open source project and we may seek to monetize in the future but have no plans to do so at the moment. But be sure that you will always be able to use Web3ArbitrageBot.