Welcome Degen Gambler!
Betfair is a British gambling company which operates the world's largest online betting exchange. Betting exchanges are important factors in the betting industry since they’re offering more serious actors/bettors a way to participate in betting markets without having to deal with the ordinary, in many cases absolutely terrible, bookmaker model which restricts/limits any sharp action [in fact, standard bookmakers are probably closer to “marketing companies” than they are to “betting companies”]. Owing to this, the exchanges attract a great amount of clever customers which in turn generates liquid order books and significant betting volume.
With a satisfactory share of sharp volume these platforms have evolved into some of the most efficient and informative ones currently available, hence providing an excellent opportunity for aspiring bettors to learn the fundamentals [and more] of how to price uncertainty.
To understand the basics of the betting exchange model, consult our Betting 101.
Note: “Liquid order books and significant betting volume” depends on the specific markets you’re looking at as well as who you’re asking. The point: mostly it’s much better than what’s offered with standard bookies.
Note: There is often hidden liquidity [iceberg orders, see e.g. The impact of hidden liquidity in limit order books, Frey, Sandås] on the exchanges [market books convey information → information affects prices → market participants prefer hiding their intentions and present incomplete information to others → participants send partial orders programmatically instead of huge orders at once] which means you’ll only observe parts of the *real* order book.
Today we’ll familiarize ourselves with the basics of the Betfair API, an API that allows us to retrieve both historical and current betting data in order to learn from and analyze one of the most efficient betting markets, the Betfair Exchange, out there. Furthermore, the API offers all the necessary tools to create and automate betting strategies, i.e. build and launch betting bots.
Access to the Betfair API is restricted to Betfair customers only. For full access [ability to place bets etc.] you’ll therefore have to be a resident in one of the many EU countries in which Betfair is operating. However, we’ve figured out a way for our US subscribers to get partial access [retrieve odds, analyze historical betting data, …] by setting up “dummy accounts” and we believe that should be good enough for 99 % of our subscribers at this point in time.
Let’s go!
Note: Our coding/programming material assumes that you have some familiarity with Python. If you do not, please get started today. Learning Python is definitely one of the most +EV decisions you can make if you are interested in earning WIFI-money. Combine Corey Schafer and Automate the Boring Stuff and you will soon be an excellent herpetologist.
Create a Betfair account
If you’re in the US, follow the account setup guide below.
If you’re in the EU and Betfair *does not* operate in your country, follow the account setup guide below.
If you’re in the EU and Betfair *does* operate in your country, you simply sign up with them the ordinary way. This is the best case since it will allow for full access to the API.
Guide for US subscribers [and EU unless Betfair is accessible in your country]
Step 1: Use a VPN and connect to a UK server. [Any VPN will do, we use Mullvad since they’re crypto & privacy friendly + a great bunch of autists like them].
Step 2: Visit https://www.betfair.com. Click “Join Now”.
Step 3: Fill in the form, just go with dummy details [example in picture below]. However, make sure to remember/write down the email address and the password since you’ll need them to access the Betfair account. Moreover, you may at some point have to verify the email address → use one you’re able to access.
Step 4: After finalizing the previous step, you’ll now arrive at an “automatic check” where Betfair is trying to verify your details. This process will of course fail and you’ll instead be redirected to a new page. Click “All details are correct”. Again you’ll be redirected to another verification page asking you to upload supporting documents to verify your identity. However, at this point we’re already done since the account has been created which is all we need. Close the pop-up window and return to https://www.betfair.com [you should be logged in by default].
Generate an API key
Time to get an API key!
To do this, you’ll want to head over to this website, at which you should be presented with the below view.
Note that you need a Session Token (ssoid). You can find this token by heading back to the Betfair Exchange landing page [if you’re getting more “you need to verify your account” messages, just ignore them] → right click → “Inspect” → “Storage” → “Cookies” → locate the one marked ‘ssoid’. Finally, copy pasta this session token into the box, then click on createDeveloperAppKeys → fill in an [arbitrary] “Application Name” → click on “Execute” at the bottom of the page.
When you’ve completed the above steps your “Application” should show up on the right hand side as in the above picture. Click on it and copy the application key corresponding to the one with version “1.0-DELAY”. Save this key at a place where it’s easily accessible [notes or whatever], it’ll be used every time we write code that calls the API.
Notes:
The Delayed App Key operates on the live (production) Betfair Exchange and not a testbed/sandbox environment.
The Delayed App Key should be used for development purposes and any functional testing. The key provides delayed Betfair price data. The delay is variable between 1-180 second snapshots.
Note: If you’re finding this interesting/useful, please consider reading Betfair’s own API documentation as well.
betfairlightweight
Since we’re coding in Python, we’ll be using a [excellent] Python wrapper, betfairlightweight, for all our Betfair API material. This library/package allows for all the available API operations and simplifies the stuff we’ll be interested in doing tremendously. Instead of having to spend an enormous amount of time coding up a bunch of classes [with terrible performance, zero security & thousands of bugs] that sends & handles requests & responses, we take advantage of the fact that a bunch of turbo autists have done this exact thing for us already.
Begin with running
pip install betfairlightweight
to install the library.
Basic operations, set up and close a session
Nothing teaches coding as well as sample code. Hence, let’s jump straight into it and perform a couple of trivial operations in order to get some basic experience with betfairlightweight.
Below we’ll learn how to set up and close a session with the Betfair API.
To begin with, we have to define three variables that are required to establish a connection, username [use the email address you registered], password and API_key. As soon as they have been defined we set up an APIClient object [frequently named “trading” in the betfairlightweight documentation so we’ll go with that here as well] taking those variables as parameters.
Code below.
import betfairlightweight
username = "INPUT EMAIL ADRESS"
password = "INPUT PASSWORD"
API_key = "INPUT API KEY"
trading = betfairlightweight.APIClient(username, password, app_key = API_key)
Now, calling
trading.login_interactive()
establishes a connection/session with the Betfair API. To check whether this connection is up and running you can do
trading.session_expired
which returns False → our session is live.
Note: When the connection is established an automatic timeout of 12 hours is applied.
Doing
trading.logout()
terminates the session which can be verified by another
trading.session_expired
now returning True.
A couple of useful links below.
Github repository [many hours have to be spent analyzing the code to “fully understand” how everything’s connected]
Documentation [not too much material but at least a couple of brief examples]
Summary
Since more fruitful examples require a deeper understanding [of betfairlightweight & the Betfair API] and this is supposed to be a *gentle* introduction, i.e. a brief presentation with a concise set-up guide, we’ll reserve the practical & therefore more interesting content for future posts.
Nevertheless, if you’re curious and would prefer to delve deeper ASAP, we’d recommend checking out our Github Betfair API material, which e.g. contains full support for automation of the hedging process for bonus rugging.
The idea is the following: instead of wasting precious time recruiting & speaking to new BR-prospects 24/7, you hire someone to do this. This person is then responsible for both the communication and the placement of bets on *their* sportsbook accounts. Every evening, he sends you an Excel file [predefined template] with all the new [unhedged] bets → you run the script which reads the Excel file and hedges the bets, one by one, at Betfair. Efficient, right?
[Now repeat this process by hiring a second person ;)]
Expect more interesting Betfair API material to pop up at a regular basis. In the next post on this subject we’ll probably have a look at historical data and how you can utilize the betfairlightweight library to parse the relatively unfriendly files Betfair provides you with.
Closing note: We fetch and use odds data from the Betfair API multiple times a week to beat less efficient markets [“pick the winner in 7-15 games” and similar multi-game bets are very popular in some parts of the world]. betfairlightweight + itertools, IYKYK.
Until next time…
Disclaimer: None of this is to be deemed legal or financial advice of any kind. These are *opinions* written by an anonymous group of mathematicians who moved into betting.