Project Tradebot IV: programming the bot

Monday 18 April 2016

GuyBI’ve decided to make a tradebot and catalogue my process. This week, I’m going to be a dev.

Now for what might be the hardest part of this whole endeavour: coding my bot.

Read also: Project Tradebot III - coin pairs and exchanges

Crypto is full of devs who could knock this kind of thing out in a couple of hours, but I’m not one of them. When I originally decided to take on this project, I thought I could pay someone to do the dev work for me, but where’s the fun in that? Besides, the bot is going to need plenty of adjustment and optimisation, and if I can’t do that myself I’ve got a problem. Tuning it and squashing bugs means I need to understand the code really well. And that means writing it.

I was making simple programs on a ZX Spectrum when I was 4 or 5, and played around with C at school (what can I say, I had a misspent youth). But this is a little different. Not least because when you’re dealing with money, the stakes get higher. This is something that has to work in the field.

More recently, I’ve tinkered with Python, which I think is a great language. It’s pretty simple, although it has its quirks. There’s also loads of documentation on the web. (In fact, coding the bot is basically a matter of looking up how to do stuff on Stackexchange. And looking up error messages. On Stackexchange.)

There’s still quite a learning curve, because I’ve got to get into Polo’s API. That means not only understanding the API - which is fortunately quite simple, even for someone who didn’t know what an API was until relatively recently - but submitting calls. Someone has helpfully created examples in Python, which I can refer to in my own code. Sadly I can’t just copy it wholesale, because whoever made it knew what they were doing and I don’t, which means they use conventions I don’t understand. Sigh. One step at a time. I figure if I can get the thing working at all, that’s a start. Elegant code will be some way down the track.

Poloniex requires that you create an API key in your account. This and other details are passed to Polo via an http POST request. The good thing is I don’t really need to understand how this is all done: it’s a matter of setting a few variables (like what command to send, and any parameters like bid price and amount), encoding it correctly (using the sample code) and sending it off (ditto). I play around a while and soon figure out how to make the calls I need - it’s actually surprisingly easy.

What isn’t initially so easy is the logic behind what I want to do. This is annoying, because it’s a very simple approach (that’s the whole point). The main problem is that, as in so many areas of life, I overcomplicate things. After coding a messy and not-very-working version, I realise it’s actually very straightforward. Just get the last trade. If it’s a sell, then place a new bid at the required interval below it. If it’s a buy, place an ask above. It’s that simple. To make sure there are no gaps (such as might happen if the market moves fast, in between your API calls), check through and place any other missing orders in your desired range. And that’s about it. You don’t even need to know the last price.


'I don't even see the code. All I see is "invalid syntax", "unexpected indent", "list index out of range"...'

The main part of the code is then finished in a morning, though it takes a few more hours to hunt out bugs, during which time I spend a little BTC on orders I didn’t mean to place. The whole thing initially isn’t much more than a hundred lines of Python, so there’s not much to debug. (I’m guessing I’ll find a few more quirks as I go along.) One thing that caught me by surprise was floating point numbers. Basically, you can’t represent some small numbers accurately in binary, like satoshi amounts, so you end up with weird rounding errors, meaning that trades get set in the wrong place. That was annoying, and took a bit of a workaround.

Anyway, so far, so good. Now the fun part: setting it up and letting it run for a few hours to test it out in the wild.

Brandon Hurst

comments powered by Disqus