Project Tradebot VI: Debugging

Wednesday 11 May 2016

My quick-and-dirty approach to bot coding basically works, but I realise now why more people don't use them. They're a pain.

My grid trading bot works, kind of. Under good conditions, it places bid and ask orders as the price on Poloniex goes up and down, selling when it rises and placing lower buy orders to pick coins back up again when it falls again. The theory is good, but I run into the old problem: In theory, theory and practice are the same. In practice, they’re not.

I’ve taken shortcuts in the interests of getting something out there and working. It’s good to know you’re on the right track, but the reality is that the bot will simply not work properly unless I can mitigate some of the many issues that affect it, human and otherwise. The chief problems I need to address include:

  • Poloniex’s API is a bit quirky, to say the least
  • My bot crashes when it can’t talk to the exchange
  • If my connection goes down, the bot also crashes
  • The bot doesn’t warn me when I run it in a way that it can’t function properly - such as when it runs out of funds because I’m buying too-large tranches of coins (when this happens, it just keeps trying to place the same orders over and over again)

All of this needs to happen before I think about optimising the bot to make the most profits out of the current market conditions. Ideally, I also want to implement some other useful things, such as:

  • Being able to monitor the bot remotely, and perhaps even control it. 
  • It would be useful to have a website that displays results, or to receive regular emails of recent activity.


I was hoping for a lean, mean, trading machine. Instead I got a needy and unsanitary cat of a tradebot.

It only takes a few days of testing - during which time I troubleshoot half-a-dozen major errors - to see what the real challenge is with bots, and perhaps why more people don’t use them. I had this dream of setting one loose on an exchange, running it from a dedicated computer (like a Raspberry Pi) and watching the profits roll in. But the reality is that it’s not a ‘set-and-forget’ kind of enterprise. Bots - at least my bot - are more like my cat. They demand constant attention. They can be very needy. They do things they’re not supposed to, which require immediate action. They bring up their last meal of next-door’s homing pigeons at inopportune times. They get into fights, despite the fact they’ve lost countless such fights in the past, and then complain when you take them to the vet to have them put back together. They bring in mice, which then escape and scurry all over the house. If you wake up at night and hear an odd noise, you wonder what the hell they’re up to now.

You get the general idea.

If you’re thinking of making a bot yourself, one of the major issues you’ll need to deal with is the connection with the exchange. That’s your lifeline, and if it drops out your bot is blind and deaf. Depending on your trading strategy, that could be catastrophic - not least because it tends to happen at times of highest use, when the price is likely to move most. (In my case, it’s fortunately more in the category of an annoyance.) A little research allows me to circumvent it with an HTTPError exception in Python; another is needed - a URLError - for when my connection is interrupted.

Polo has its fair share of idiosyncrasies, and the one that really causes me problems is timestamps. If you return a list of last orders executed, there can be several with the same timestamp - because someone bought or sold an amount that covers a range of prices. Now, you'd expect those prices to be given in time order - highest first for a sell, for example. No. Not even reverse order. Not even random order. Random order that changes each time you make the call? Yup, that's the one. It's a headache and requires an extra piece of code to sort out. At least I think I now know what's been causing all those duplicate orders.

There’s some other stuff which would be very useful (the lack-of-funds issue, for example), but I’m leaving that for now and simply printing out the result of an attempted trade. Getting the thing to run for 24 hours without interruption will be a major accomplishment. If I can get it to do a week, then I can expect some decent results from testing it against different markets and with different trading parameters. 

Onwards! Slowly.

comments powered by Disqus