Tuesday, 9 March 2010

Comparing the APIs: what's the best way to access Betfair from Python?

(Just a quick break from the blog series on building a Betfair bot: this is a quick comparison of the various options for accessing the API through Python.)


In the examples I've used to date, I've used either Betfair's own Excel plugin, or I've used Resolver Systems' own Betfair plugin (which I've modified to add additional features to...) But Resolver's own plugin may not be the best for accessing Betfair – even from within Resolver One – and it does come with a number of disadvantages: it is Windows only, for instance. But what are the other options:


Well, there's the Python Betfair Library, hosted on Google Code, and last updated in mid-2008. The readme suggests it will not be completely simple to install under Windows, but as I'm using my Ubuntu box at the moment, that isn't likely to be a serious problem. So, OK, let's download and untar it. Looking into the example code, it looks like it operates in a very similar way to the Resolver libraries:


exchange = Exchange()

exchange.login(username, password)

# How to call get_account_funds

funds = exchange.get_account_funds(Exchange.EXCHANGE_UK)


Yep: all similar here. If it works, and is easily cross-platform, this might be a goer. OK: let's run the example code:


python example.py


Uh oh:


ImportError: No module named ZSI


OK: this may not be quite as simple as I thought. ZSI is the Python Web Services project (Zolera SOAP Infrastructure). It's no big deal to install this on my box, but it does explain with these libraries might struggle to be easily cross-platform. Is ZSI/pywebsvc in the Ubuntu packages? Yep, it's python-zsi (as you might expect). A simple sudo apt-get install python-zsi is all that's needed.


Let's run again. It's asking me for my username and password – this is good. And now I wait... and I wait...


...lots of errors followed by...

socket.gaierror: [Errno -2] Name or service not known


Grr: let's run again. It's always hard to know whether the problem is Betfair's (often down, particularly for the free service, API) or whether it's because the PythonBetfairLibrary is out of date.


This time it's better: it's giving me lots of information on markets that I don't have positions in. Then it craps out on a 'MARKET_CLOSED' error, that I can at least trap. Initial impression: promising (the syntax looks fine), but it's not been updated for a long while, and it doesn't seem to trap errors very well.


Let's try a slightly more complex test: I have a Python app for simple market making, that I've optimized for golf tournaments. Normally, I run this in Resolver. But there's no reason it cannot run it as a standalone application – with the proviso that I lose the pretty display.


[25 minutes later]


Well, that was simpler than I thought. The Python Betfair Library works pretty well, with two major provisos. Firstly, it doesn't trap errors particularly well. I may do a little bit of work to allow it to throw out more useful exceptions. Secondly, it's sloooooooooooooow. (This can be an advantage, as you are less likely to accidentally exceed 60 API requests per minutes.) Perhaps it's because I'm used to the Windows native Betfair DLL that backs up the Resolver libraries, but taking 20-30 seconds to execute and output the results of GetAllMarkets (against less than 2 for the native DLL) is pretty slow. It was hard to market-make more than 6-7 golfers in an efficient manner, against to 30-40 I manage using the Resolver libraries. There is nothing intrinsically slow about using pure Python libraries, so I shall do a little investigation, and see if it's anything about my setup that is causing the slow performance...


Which bring us to the library at BespokeBets.com. This can be got here: http://bespokebots.com/showme.php?id=betfair_api_python.zip. It's a pretty simple library, that uses Python's built-in url2 library to do web service queries:


bot = API() # create new api object
print "Login:", bot.login(username, password)
bot.get_account_funds()


The code behind the library is pretty simple: this is a very thin wrapper around the Betfair API. (I was impressed to note that there is no Betfair DLL dependence, and everything fits into a few hundred lines of code. Nice work.) And this thin-ness brings a cost: it implements only a limited subset of the API – there is no GetDetailAvailableMktDepth or GetMarketProfitAndLoss for example. That said, extending this library would be simple, and it certainly seems capable of 4-5 simple API calls a second, which makes it quite a lot faster than the PBL.


I am not going to re-implement my market making system, because a small part of the algorithm relies on GetMarketProfitAndLoss. But in theory, working around that would not be difficult.


For now, I am going to stick with the Resolver libraries – which are, of course, just a thin interface round Betfair's own .NET ones: but for those in a non-Windows environment, both the Python Betfair Library and BespokeBot's library will work well. For anyone playing with building my bot, very few code changes should be necessary...


And now I need to get back to work...

10 comments:

  1. I must admit that when you said that PBL was slow I thought you were being overly picky. I figured the overhead of making the http request would dwarf any other differences. 20-30 seconds is massive though.

    I had a look around at the options after your last post and noticed, contrary to my initial expectation, that IronPython can apparently run on Linux. Another thing to add to my to-do list.

    ReplyDelete
  2. I've finally found time to play around with IronPython (and betfair). I've been through the last tutorial you posted without any problems in Windows 7, though I did skip the database interactions, but ubuntu proved more problematic.

    It took me a little time just to get the betfair module to import. That is now fine but connecting to the gateway returns multiple errors. Sadly they're not very informative errors.

    I've not given up yet but it's looking to be more hassle than I had hoped

    ReplyDelete
  3. I am the dev of the BespokeBots library. It was only meant as a brief introduction to the main functions, however i am currently extending it to be more "useful" in the real world.

    ReplyDelete
  4. Hi,
    The bespoke URL for the betfair API is not available at the moment... could you please upload it (maybe to rapidshare?) so that we can get it?

    Much appreciated!

    ReplyDelete
  5. I've stopped hot-linking from external sites but the download is still there. Just go to http://www.bespokebots.com and click "Betfair Bots" on the left hand menu.

    The library has been greatly improved recently and lots of useful functions have been added, including get_market_profit_and_loss(). Betfair.py has full details of the new features.

    ReplyDelete
  6. Getting somewhat overdue for an update.

    ReplyDelete
  7. I just stumbled upon your blog and wanted to say that I have really enjoyed reading your blog posts. Any way I’ll be subscribing to your feed and I hope you post again soon.
    Beat The Casino-Beat The Casino

    ReplyDelete
  8. It's gone quiet. Are you coining it?

    ReplyDelete
  9. Excellent post, I agree with you 100%! I’m always scouring the Internet for new information and learning whatever I can, and in doing so I sometimes leave comments on blogs. I don’t do it for SEO purposes necessarily, but to learn new things.

    Togel2000

    ReplyDelete
  10. Your blog is very useful for me.I really like you post.Thanks for sharing.

    หนังสงคราม

    ReplyDelete