Thursday 4 March 2010

Building Our First Betfair Bot IV - Talking to the API and storing your bets

*EDIT: In this piece, I originally said you needed to use the paid API to place bets; this is no longer the case*

In the last missive, I wrote about how we want to store data. In theory, we could use Betfair itself to store data – and pull everything we need on an
ad hoc basis. So: if we want to see which bets are open, we'd query Betfair, and it would return a list of betIDs. But, this comes with two major problems: speed and cost. Betfair can charge as much as 0.5p for certain data intensive API calls: you need some pretty amazing programming and trading skills to overcome those costs.

So, our 'bot needs to store as much information locally as possible. And this means a (very simple) database – in our case SQLite. In today's missive, we're going to put a bet on, and then store it in our database.

The first thing we'll need is a programming language. I am going to be a little Windows-centric and use IronPython – the Microsoft implementation of Python 2.6. You'll also need this zip file: betfairdatabase.zip. This contains the following files:

betfair.py

lib.py

betfair.dll

→ these are the IronPython / Resolver Betfair libraries

SystemData.SQLite.dll

→ The .NET ADO SQLite connector

bettinghistory.s3db

→ An empty SQLite database

database.py

→ our database library

What we'd like to achieve (ultimately) is to put bets on, and then put them in a table that looks something like this:




After you've installed IronPython, it's time to have a play. For simplicities sake, copy the contents of the above folder into your IronPython directory. Now, run 'ipy.exe', which you'll find in the selfsame directory. You should get something that looks like this:




OK: Let's import the database module:


>>> import database


And create a new database connection object:


>>> dc = database.database()


Open the database file, so we can interact with it:


>>> dc.open('bettinghistory.s3db')


OK: so, what can we do with our database connection? Well, firstly we can record our betting activities. Let's set-up and record a bet:


>>> import betfair

>>> betfairGateway = betfair.BetfairGateway('username', 'password', productId = 22)

OK


(This assumes you have a Betfair paid API subscription. If you don't, you'll be reading, rather than doing.)


>>> betfairMarket = betfairGateway.GetMarketById(20692870)


We've set up a betfairMarket object for the winner of the World Cup 2010 (which, slightly surprisingly, Spain is the favourite to win.)


Shall we see who the runners are?


>>> for runner in betfairMarket.GetRunners()

... print runner, runner.Id, runner.Back

...


The screen should look something like this:




Now, I'm working on the theory that – if England moves out to 10-1 (11.0 in Betfair speak) – then I want some of that action. (Of course, the current price is 7.0, so that isn't very likely right now... but still...) To do this we have to get hold of the Betfair runner (selection) object:


>>> selection = betfairMarket.getRunner(27)


(We've gotten the selectionID by looking at the print-out above, and entering it manually. You'd probabaly want to iterate over GetRunners() until you got the object you wanted... but this will do for now.) And let's check we have the right object:


>>> selection, selection.Back

(England, 7.0)


Perfect: that looks like the right object, and we need to pass this object to the betfairGateway if we want to put a bet on:


>>> response = betfairGateway.Back(selection, 11, 2)

OK


The first thing we pass is the selection object, the second the desired odds, and finally the amount (in this case, the £2 minimum).


Now, the Betfair bet response object is a complex beast, but the only bit that is really of interest to us is the array of results. Let's pull it out:


>>> betsArray = response.betResults


And see how many bet objects it contains:


>>> len(betsArray)

1


No great surprise here: the bet we've put into the system is so far out the money I'd be surprised if there was more than one object: that of an unmatched bet. (If you were to offer to back England at 6, with £100,000, then you would get an awful lot of bet objects in return: one covering the bets matched at 7.0, one at 6.9, etc.


But let's take a look at the bet object we've gotten here:


>>> bet = betsArray[0]


Ha ha! Now we (finally) have an object with the details of our bet. Let's see if it was succesfully placed into the system (i.e. that the market is still live, and we still have money in our account):


>>> bet.success

True


Excellent. We've placed the bet. That's a good start. Was it matched?


>>> bet.sizeMatched

0.0


No surprise: it wasn't matched. It's sitting in the Betfair system waiting for someone to offer 10-1 on England. Finally, before we call it a wrap for the evening, let's store this bet in our database:


>>> dc.openBet(bet, 20692870, 27, 200, 11, 'BACK')


The openBet method takes the following paramaters: bet object, marketID, selectionID, amount (in pence), odds (a float), type (either 'BACK' or 'LAY').


Tomorrow – or in a few days – we'll play a little more with putting bets into the system, and reading the responses. But, hey, pat yourself on the back, you've just put some money on England to win the World Cup :-)


41 comments:

  1. Is there an advantage to using IronPython in this situation? It's only recently that I've started exploring python and I hadn't considered IronPython at all. I can see how these variants, I guess Jython would be similar, could be useful though.

    I've not run through your example yet but is there actually anything in there which you couldn't do from the free api? I've taken a look at the comparison [1] between the services and it looks like following your example should be possible on the free access api. I'll give it a go later on anyway.

    Given that we want to minimise the number of requests sent to betfair what is your opinion in storing all data requests sent to betfair. Storage space is so cheap these days it's tempting to store everything and the possibility always exists that it could aid debugging later on. On the other hand the more data we store the more difficult it may become to extract the data we genuinely want.

    [1] http://bdp.betfair.com/index.php?option=com_content&task=view&id=36&Itemid=64

    ReplyDelete
  2. Hi Jonathan,

    I've used IronPython for two reasons: (1) we're using the Betfair Windows DLL, and (2) I really like the .NET tools available (mostly for free) from Microsoft.

    Re the free API: you're absolutely right. At some point in the past, you needed the paid for API to place bets. This has obviously changed, and explains why Betfair has not been removing £200/month from my account!

    Cheers

    Robert

    ReplyDelete
  3. Just to add: if you want to use the Python Betfair Library (PBL) (which I'm going to cover in a future post...), then you won't have to change the code very much. The only real issue I have with the PBL is that it's pretty slow.

    But if you're just playing, that's fine.

    ReplyDelete
  4. Thanks for the followup. I think I'll have a crack at re-implementing it with the PBL. If for no other reason that it will force me to be fully engaged rather than just copy and pasting.

    ReplyDelete
  5. I played around with making a bot at the weekend. Currently all it does is identify potentially interesting markets. It doesn't place any bets.

    One thing I noticed though is that it's *slow* on the free api. The getRunners method seems to be limited to 10 requests per minute. Hopefully it will be fast enough to get a proof of principal set up and working but I suspect it won't be suitable over the long term.

    ReplyDelete
  6. Hi Robert! Really enjoying this blog, was wondering if you're planning on continuing or if you had any interest in collaborating on such a project. I'm London based, Gav.

    ReplyDelete
  7. It would be a shame to see this blog die.

    I've continued to play around with the betfair api but haven't given it much time for various reasons. One of those distractions was playing around with pybrain for another project. If I ever progress beyond market making hopefully that will be useful here. Currently though I have code which identifies opportunities but I've held off placing bets for the moment.

    I can't speak for Robert but I would be interested in collaborating. I can be reached by emailing jonathan at firstname lastname (all one word) dot com.

    ReplyDelete
  8. Interesting blog - shame the author didn't keep it up. I've just started doing some posts on creating a bot on my own blog - web2linux.com
    Richard

    ReplyDelete
  9. FYI. There is klabor bee command-line tool which is betfair bot toolkit. It allows easily and quickly compose different betfair sport bot. You can find it on http://klabor.com

    ReplyDelete
  10. I played around with making a bot at the weekend.
    [url=https://www.gclub-casino.com/baccarat/][color=#ffffff][/color]บาคาร่า[/url]
    [url=https://www.gclub-casino.com][color=#ffffff]gclub จีคลับ[/color][/url]

    ReplyDelete
  11. Royal1688 Online casino today, we will introduce how to save mobile battery. Turn on the automatic screen light, because it is one of the features that people think that much energy, because the sensor must work so hard it will consume power. But the fact is that in the automatic screen light, low power and light conditions in the form that we are to maintain the eye. And another good is the energy saving because the screen does not need to accelerate too. In addition, the time to turn off the screen to suit the user, usually the screen clock to turn it off as appropriate and relative to the use, it will save energy. Do not open all the time will help to save energy. Have the right guidance to set the screen time. In order to close it within 30 seconds when not in use, it is sufficient. Finally, do not use white wallpaper, because some screens may be sensitive to certain colors, especially white. If you are interested in news, more information, follow the web. Royal1688

    ReplyDelete
  12. จีคลับ Fun and entertaining sites to help gamblers. To play all areas. Stay at home at work. Fully playable. Gambling games do not require long holidays to enter gambling casinos. Can be played easily at home. Play professional. Make every bet every day fun, just like playing a game of betting. Which can be played selectively. Give good money There are daily gambling games. A fun and realistic gambler. Make good money this way. No need to be cumbersome to play all the games are always good.

    In addition, it is fun and interesting to give players gambling fun every time. Give a good return everywhere. Choose to play friendly. There are betting games to like. Who wants to make a good income for yourself, you can gamble as you choose. Come play every day, I will be satisfied every match. Gamblers can play professionally. You will like and satisfy all the competition. To play this way. Have fun every time. บาคาร่า

    ReplyDelete
  13. Thank you for sharing valuable informationNice post,I enjoyed reading this post.

    หนังใหม่

    ReplyDelete
  14. Fantastic articles is post by you in this blog. You give a nice thing. Thank you for such a nice article.
    ufabet

    ReplyDelete
  15. Playing in all areas of sports is not necessary. You may stay at home and enjoy fully playable gambling games online. Betting or other sports toto can be played easily sitting in your room. May your bet as everyday fun. These can be what you are looking for 안전공원 안전놀이터

    ReplyDelete
  16. Which website is good for football betting? As mentioned at the beginning, the current online football betting website A lot happens, only ufa24h We only ufabet care about our members fully and willingly.

    ReplyDelete
  17. I really appreciate the kind of topics you post here. Thanks for sharing us a great information that is actually helpful. Good day! pgslot

    ReplyDelete
  18. I am definitely enjoying your website. You definitely have some great insight and great stories. เซ็กซี่บาคาร่า

    ReplyDelete
  19. 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
  20. i am always looking for some free stuffs over the internet. there are also some companies which gives free samples. สล็อตออนไลน์

    ReplyDelete
  21. Nice to read your article! I am looking forward to sharing your adventures and experiences. Slot

    ReplyDelete
  22. Thanks for a wonderful share. Your article has proved your hard work and experience you have got in this field. Brilliant .i love it reading. เล่นสล็อต

    ReplyDelete
  23. Positive site, where did u come up with the information on this posting?I have read a few of the articles on your website now, and I really like your style. Thanks a million and please keep up the effective work. judi slot online

    ReplyDelete
  24. Thank you, I’ve recently been looking for information about this subject for ages and yours is the greatest I have discovered so far. But, what about the bottom line? Are you sure about the source?
    joker123 login

    ReplyDelete
  25. Positive site, where did u come up with the information on this posting?I have read a few of the articles on your website now, and I really like your style. Thanks a million and please keep up the effective work. daftar situs judi slot online

    ReplyDelete
  26. I haven’t any word to appreciate this post.....Really i am impressed from this post....the person who create this post it was a great human..thanks for shared this with us. slot online

    ReplyDelete
  27. daftar dan mainkan game slot online dengan bet murah sekarang di YOYO88 >https://manneli.com/

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

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

    ReplyDelete
  30. daftar diri anda di Yoyo88 situs slot online pragmatic88 sekarang juga karena banyak bonus menanti anda!

    ReplyDelete
  31. I am constantly surprised by the amount of information accessible on this subject. What you presented was well researched and well written to get your stand on this over to all your readers. Thanks a lot my dear. 먹튀검증사이트

    ReplyDelete
  32. I just found this blog and have high hopes for it to continue. Keep up the great work, its hard to find good ones. I have added to my favorites. Thank You. my site

    ReplyDelete
  33. I am impressed by the information that you have on this blog. It shows how well you understand this subject. pg

    ReplyDelete
  34. daftar dan mainkan game slot online di situs judi slot online jago88

    ReplyDelete