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: This contains the following files:


→ these are the IronPython / Resolver Betfair libraries


→ The .NET ADO SQLite connector


→ An empty SQLite database

→ 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:


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)


(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)


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)


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


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

>>> bet.sizeMatched


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 :-)


  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.


  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!



  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.

  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.

  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.

  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.

  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.

  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 -

  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

  10. I played around with making a bot at the weekend.
    [url=][color=#ffffff]gclub จีคลับ[/color][/url]

  11. Nice article great post comment information thanks for sharing


  12. Betting is easy with the internet.
    รอยัล1688Online gambling games are open to the players to gamble indefinitely. The online casino of the site that makes the player happy with the risk of multiple online casinos. With easy access to online casino games through internet gambling. There are several ways to get players to gamble with us. The risk of online horoscope has increased. Today's online games can meet the needs of the player. Because there are types of online gambling games that are easy to understand, and online games to win more than 100 games to enjoy a full size because you do not have to travel to the casino. Because they can use online services and can earn money from the real risk. Not only this, the online game also broadcast live betting, so it is like betting from a real place. If you are one of the gamblers who want to gamble, the next step is to go online. And can meet all needs. Online Gambling Betting is the Way to Choose Who I came to the risk. Have you ever been here today? viva3388

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

  14. จีคลับ 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. บาคาร่า

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


  16. Scamtested makes it simple for you to make money online. We review products and services and find out what really works! Saving you time and money Oddsmonkey

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