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

58 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. Your blog is very useful for me.I really like you post.Thanks for sharing.

    หนังสงคราม

    ReplyDelete
  10. 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 มือถือ

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

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

    ReplyDelete
  13. 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 มือถือ

    ReplyDelete
  14. Your blog is very nice,Thanks for sharing good blog.

    หนังใหม่

    ReplyDelete
  15. 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.
    FIFA55

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

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

    ReplyDelete
  18. Thanks got so much inspirations.Keep it up like this,Cheers !
    หวยชุดฮานอย

    ReplyDelete
  19. This comment has been removed by the author.

    ReplyDelete


  20. I just landed up in your blog and I really appreciate your blog. It is full of resourceful information.

    Thanks,

    BetsAPI is the best betting website conducting betting for various games of sports. Visit here and enjoy the best odds API with us on your favorite game!

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

    ReplyDelete
  22. I just landed up in your blog xem da ga cua satand I really appreciate your blog. It is full of resourceful information da ga 999 .

    ReplyDelete
  23. I'd like to comment on your excellent article. Very readable
    สล็อตxoauto

    ReplyDelete
  24. You can write an article very much. Thank you for making a great article to read.
    Sa gaming สมัคร

    ReplyDelete
  25. Forbes reported that as of this June, $20.6 billion in small business loans has been guaranteed by the SBA nationwide.
    เล่นบาคาร่า
    เล่นบาคาร่า
    เล่นบาคาร่า
    เล่นบาคาร่า

    ReplyDelete
  26. Its my great pleasure to visit your blog and to enjoy your great posts here. I like it a lot. I can feel that you paid much attention for those articles, as all of them make sense and are very useful.  바둑이게임

    ReplyDelete
  27. This comment has been removed by the author.

    ReplyDelete
  28. This comment has been removed by the author.

    ReplyDelete
  29. very good written article. It will be a good support to anyone who utilizes it including me. Keep doing what you are doing can't wait to read more posts very nice article 프리서버커뮤니티

    ReplyDelete
  30. wow, awesome article post.Much thanks again. 먹튀검증

    ReplyDelete
  31. Yes i am totally agreed with this article and i just want say that this article is very nice and very informative article.I will make sure to be reading your blog more. You made a good point but I can't help but wonder, what about the other side? !!!!!!Thanks 먹튀검증

    ReplyDelete
  32. At present, there are numerous online gaming sites catering to sports betting and casino gambling based in countries like Jamaica, Costa Rica, and Ireland. check this link right here now

    ReplyDelete
  33. I think this is an informative post and it is very useful and knowledgeable. therefore, I would like to thank you for the efforts you have made in writing this article. เซ็กซี่บาคาร่า

    ReplyDelete
  34. I high appreciate this post. It’s hard to find the good from the bad sometimes, but I think you’ve nailed it! would you mind updating your blog with more information? สมัครบาคาร่า

    ReplyDelete
  35. Great content material and great layout. Your website deserves all of the positive feedback it’s been getting. slot online

    ReplyDelete
  36. We have sell some products of different custom boxes.it is very useful and very low price please visits this site thanks and please share this post with your friends. Slot

    ReplyDelete
  37. A very awesome blog post. We are really grateful for your blog post. You will find a lot of approaches after visiting your post. judi slot

    ReplyDelete
  38. I like your post. It is good to see you verbalize from the heart and clarity on this important subject can be easily observed... daftar situs judi slot online

    ReplyDelete
  39. Thanks for a very interesting blog. What else may I get that kind of info written in such a perfect approach? I’ve a undertaking that I am simply now operating on, and I have been at the look out for such info. Satta number

    ReplyDelete
  40. Play the Satta King Game.
    In India, Satta king up is a famous gambling game. Satta king 786 is a game that has been around for many years. Undoubtedly, it's one of the best betting games, more often known as types of gambling, that evolved in North India. It has several names, such as Satta king fast, Satta king up or Satta king 786.

    ReplyDelete
  41. What a sensational blog! This blog is too much amazing in all aspects. Especially, it looks awesome and the content available on it is utmost qualitative. https://ec-sites.com

    ReplyDelete
  42. I really loved reading your blog. It was very well authored and easy to undertand. Unlike additional blogs I have read which are really not tht good. I also found your posts very interesting. In fact after reading, I had to go show it to my friend and he ejoyed it as well! my site

    ReplyDelete
  43. Good to become visiting your weblog again, it has been months for me. Nicely this article that i've been waited for so long. I will need this post to total my assignment in the college, and it has exact same topic together with your write-up. Thanks, good share. pg

    ReplyDelete
  44. MJ atau MenangJudi merupakan situs slot online terbaik di indonesia dengan pelayanan 24 jam dan menyediakan berbagai game judi online seperti slot online, casino online, poker online, taruhan bola.
    kunjungi juga jago88

    ReplyDelete
  45. YES88, Slot online server thailand dengan RTP tertinggi! WEBSITE TERPERCAYA hanya di YES88 maxwin

    ReplyDelete