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)

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...


  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.

  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

  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.

  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!

  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.

  6. Getting somewhat overdue for an update.

  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

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

  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.


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


  11. eToro is the most recommended forex trading platform for beginning and advanced traders.

  12. Most entertaining betting channels
    Gclub It is a way to open online betting games where customers will experience the new generation of gambling. Online gambling has been improved to ease luck. And uninterrupted gambling that is ready to make fun of you all the time. The online casinos offer a wide selection of casinos, as well as games that will allow all users to gamble online. Opportunity to enjoy a lot of fun. It also offers more fun to customers. We are a popular online gambling site. Due to the fact that our website is in good faith and the games are up-to-date, it is a requirement of modern bettors. The fun to give the user anytime with online gambling games that will make you happy and earn money from the game. The use of online gambling games in real time with a new look that makes it like to use the service in place. Not only that, you can still understand or find good tips. Before placing bets Gambling with other players online in the game. And find the easy results with online gambling. Gclub มือถือ

  13. Online Betting
    รูบี้888 It is a site that offers online gambling services that are famous for fun, entertainment and entertainment for more than 10 years, making the casino industry known and become accepted in just a short time. For playing online gambling games, it is one of the beginnings of the rich. Anyone who can join and start rich is not difficult. Because of the variety of online gambling games, gamblers or ordinary people are becoming the highest earners in the country and the highest in the world because of their income from playing online gambling games. The story is very simple. Because every time you beat online gambling, you can make money from playing online gambling games as well. Just join the fun and play online casino games with online casino sites. If you want to know that the income from playing the original game, you can make money from playing online games to go with the players around the world. Online has much to prove you can have your own website รอยัล1688

  14. Casino World Fun Club
    viva3388Is a web casino Asia's leading online casino games provider for more than 10 years, with an age-old and attractive online gaming service. This casino has been And it is of interest to gamblers East. Because of the website provider G-Club. Online casinos are open to the honest and transparent, and continue to innovate the system of online gambling games, so it is not strange that now playing games online casino games will be popular. Aside from Asian gamblers, risk takers can also find and experience the atmosphere of online casino games. Voice bet there's Wi-Fi only. Risks can be played on all online gambling sites on our online casino site. The convenience and easy access to online gambling games is very popular and when risk takers have questions during the game, the online risk can be contacted by our staff. Every time a player wants to be on the web viva9988

  15. Did you know!! Online Betting Makes You Rich
    คาสิโนIs a web-based casino online casino where players can gamble in all forms. The Internet only to play. Every time an internet gambler has a bet, the bettor can play all the online gambling risks on Poipet. This casino. The online casino. It is a fun agent and also a new channel for people who love and passionate about gambling and gambling. Nowadays, online gambling games in each form are constantly being developed. This makes the game of risk online is now well-known and popular in itself. Whether it is a gambler. Thais, or foreigners, are passionate about playing online gambling games, because online gambling in each form is fun and easy to play and important to gamble through. Online gambling is another safe alternative to online gambling. Wise to the Internet no matter where it is a risk I can play the lottery online. When thinking about risk online, remember to think of the services of the site Gclub Slot

  16. Online casino games are full.
    Gclub The website offers online gambling games that emphasize joy and a way of making money. Our online casino games have created online gambling. And each of our games as a service provider can be vulnerable. Create gambling that can meet the needs of users. With the style of online gambling games are thrilling. The time is available to players all the time. Use our online games to win a lot of games. No matter how lucky to win online, you risk it with us. Because of the online games, online games that bring more than 100 games to you to enjoy. Bet on gambling online with us, then the users will have both money and vitality. Do not miss out on all the online gambling games that can satisfy and give the user more fun. If you are looking for online casino games to make a living, many players have chosen to gamble with us. The best way to make money online is to make money online. Gclub มือถือ

  17. Your blog is very nice,Thanks for sharing good blog.


  18. I am unable to read articles online very often, but I’m glad I did today. This is very well written and your points are well-expressed. Please, don’t ever stop writing.

  19. . One turns out to be quick, the moves programmed. In different types of unadulterated gambling, individuals simply, say, pull the arm of a one-outfitted brigand - and the hankering to continue doing this that be strong to the point that individuals have turn (or so I've heard) to wearing diapers so they don't have to leave to go to the washroom. bitcoin games

  20. I have a big family. I live with my mom, my dad, my two sisters, and my brother. Both of my sisters
    are older than me,but my brother is younger. My sister Elizabeth is the oldest. She is sixteen years
    old. Carrie is thirteen 928bet old.We used to share a He helps little kids are sick. room, when
    we moved to our new town,we each got rooms of our own. My brother John is youngest.He is only
    years old. My mom’s name is Sally. She used to be a teacher, but now she stays home to take
    us.My dad’s name is Tim they is a doc.

  21. I don't understand how people make money from gambling. Rather I think gambling is a way of wasting money because the ultimate result of gambling is ending up with nothing. Gambling is also prohibited in many religions too. We all should know how to save money and make profit from saved money by securely investing them.