First steps: Connecting to Betfair

In order to build spreadsheets that make bets, the obvious first requirement is to connect it to a betting exchange. I decided on Betfair, as it’s the most popular exchange in the UK. They have an API that one can use to write programs that interact with their exchange, and you can sign up to use it here. When you sign up, you can either get free access to somewhat limited data (with no technical support), or paid-for access with support and no limitations. Free was good enough for my purposes, so that’s what I went for!

The next step is a bit trickier. In order to communicate with Betfair from Resolver One, you obviously need some kind of code that knows how to talk to Betfair’s servers. There are WSDL files on Betfair’s API pages to help accomplish this. WSDL is a language that allows suppliers of Web-based APIs to specify how other computers should connect to them; at some later stage I’ll describe all of the steps necessary to take these files and use them to create code to connect Resolver One to Betfair, but for this first post we can use some code that my colleague Kamil prepared earlier :-)

Here’s a screenshot of a really simple test spreadsheet running:

If you have a Betfair account, you can download the test spreadsheet and try it out yourself. (Betfair isn’t licensed to operate in all countries — a notable exception is the US — so you may not be able to sign up with them. Check their site for details.)

The file on the Resolver Exchange is a ZIP; unpack it somewhere, and look inside: you’ll see four files:

  1. betfair.dll. This is interface code that knows how to talk to Betfair’s servers, and was automatically generated from the WSDL.
  2. betfair.py. This is code that Kamil wrote that provides a nice Python-style interface to the code in the .dll file. (The auto-generated stuff can be a bit of a pain to use.)
  3. lib.py. This is some extra utility code used by betfair.py.
  4. test1.rsl. This is the important one: a Resolver One file that uses all of the others to connect to Betfair.

If you have a Betfair account (and, of course, Resolver One!), try opening the RSL file. You will see two yellow cells near the top; the rest of the sheet will show errors. You need to enter your Betfair username and password into the yellow cells, and you should then see the errors disappear, and a list of some of Betfair’s betting markets appear from row 10 downwards.

Let’s take a quick look at the formulae. The first interesting cell is B4, which contains this:

=betfair.BetfairGateway(betfairUsername, betfairPassword)

betfairUsername and betfairPassword are just named cells (referencing B1 and B2 respectively as of this writing), and contain exactly what you’d expect. betfair is the Python module defined in betfair.py. This is imported from the spreadsheet’s pre-constants user code. The formula as a whole creates a new BetfairGateway object, whose purpose (as you’d expect) is to handle our interactions with Betfair.

Let’s move on to B6, which contains this:

=dir(B4)

One nice thing about Python is that if you don’t know what methods an object supports, you can easily find out. This cell just shows the list of methods on the BetfairGateway object.

Moving on to B8, we find this:

=B4.GetMarkets()

So, we call the GetMarkets method on the BetfairGateway. This returns a generator object, which we could access repeatedly to find out all of the markets available to us on Betfair.

As we only want a quick list of the first few dozen markets, what I did was enter the formula =B8 into cell B10, selected the range B2:I32 and then used the Unpack feature (from the tools menu) to tell Resolver One to expand the generator into that range. You can see the result: the first few dozen markets appear in the cell range.

So, so far I’ve connected to Betfair from Resolver One and got a list of all of the available markets, the displayed the first few. I’ll stop at that for this post; next time I’ll start looking at the markets and seeing what we can do with them.