How to make your own trading bot

I’m certainly not a great programmer, but writing this project taught me a lot (and kept me occupied). Most of my code were done on FMZ.COM, and if I were to refactor the python code I would use a more object orientated model. Nonetheless, I was pleasantly surprised with the results I got and the bot has made almost 100% ether profit so far.

What does it do?
It is an arbitrage bot. That means that it earns money from trading the difference between prices on two (or more) exchanges. As of now it is unidirectional and only trades between Etherdelta and Bittrex: they share approximately twenty eth/token pairs. Here’s a diagram to illustrate how it works:

Words followed by parenthesis are ethereum transactions that invoke a smart contract function call.

The Code
I could have used platform python editor to create the transactions and function calls and it would have been fairly straightforward. I needed something more reliable; a failed transaction means losing money. Every single one my GET requests needed a reply, even if the TCP packet got lost or the webserver on the other end was temporarily down. Therefore I decided to implement my own python etherscan API wrapper and used pythereum to create the transactions and etherscan to publish them. I also wrote my own requests.get decorator that is a while loop that only exits once the reply is satisfactory.

Here is the code I used to encode the etherdelta json API responses as hexadecimal, rlp encoded, ethereum transactions (not for the faint hearted):

The raw hexadecimal values in the closure at the bottom are the function signatures that correspond to each function. A function signature is derived from the keccak of the function and its arguments. It must be appended to the data parameter of a transaction followed by the data that makes up the arguments. In total my code is around 400 lines long and contained in 5 different files.

The Outcome
I made a couple of graphs from the data I logged using pymatplotlib.

Overall the entire project took me around two weeks during my spare time at school and it was a blast all round. I’ve taken a break from coding vigorously and am currently in the process of planning arbitrage bot v2. The next version is going to include 86 different exchanges and a whole lot of trading pairs.

To the moon!

Add a Comment

Your email address will not be published. Required fields are marked *