Recording Betfair Odds with Python

Feb. 3, 2020 by SportsBettingQuant

This article describes how you can create a simple software script with a programming language such as Python that allows you to record prices or odds from a betting exchange platform such as Betfair. The data can then be stored in different formats (e.g. csv files or databases) and later be analysed.

In this article I would like to focus on the Python programming language and present some example code which should be rather simple to understand. I would like to mention that you can of course also use other programming languages to capture data from a bookmaker or betting exchange. Other suitable programming languages to accomplish this task might be VBA (especially if used in a combination with Excel), C#, Java or JavaScript.

Recording Exchange Data into a Database

Why Would You Record Price Data?

Especially if you would like to develop and define betting strategy you might be interested in how a strategy would have performed in the past. In a so-called backtest, your strategy is evaluated on data in the past and for this purpose you need historical odds. There are several providers that offer historical odds for both, bookmaker and betting exchange markets. Some of the resources are free (such as basic Betfair data for instance) but especially for higher resolution price data you would probably need to pay a fee to get access to the data. I believe it is a good exercise to record odds on your own for the following reasons:

  • You get familiar with the bookmaker or betting exchange API
  • You are independent of any data provider that might stop or change their service anytime
  • Running odds recording scripts on your own infrastructure like a VPS (virtual private server) is typically cheaper and more flexible than a tailored offer with recorded price data

How to Get Started with Data Collection?

There are multiple ways to record data on Betfair and many different programming languages can be used, you just need to be able to send API requests to the Betfair server.

In this article I will focus on using Python, hence you need to have Python installed on your system. For the following script I am using Python version 3 along with the requests package, that can be installed via pip install requests. You also need to have access to the Betfair Exchange API with a valid API key and session token, that are stored as environment variables on your system.

Simple Starter Script to Record Odds

As a start, the following script sends a HTTP request to the listMarketBook endpoint of the Betfair API for a specific market id:

import os
import requests

headers = {
    "X-Application": os.environ.get("BF_APPLICATION"),  # fetches app key from environment variables
    "X-Authentication": os.environ.get("BF_SESSION"),  # fetches session token from environment variables
    "content-type": "application/json",

data = {
    "jsonrpc": "2.0",
    "method": "SportsAPING/v1.0/listMarketBook",
    "params": {
        "marketIds": ["1.171100298"],  # insert market ID

url = ""

r =, json=data, headers=headers)
print(json.dumps(r.json().get("result"), indent=4, sort_keys=True))

After changing the marketId into a market that is currently open for betting on Betfair, you should obtain an output similar to the following JSON:

        "betDelay": 0,
        "bspReconciled": false,
        "complete": true,
        "crossMatching": true,
        "inplay": false,
        "isMarketDataDelayed": false,
        "lastMatchTime": "2020-07-18T10:25:31.956Z",
        "marketId": "1.171100298",
        "numberOfActiveRunners": 3,
        "numberOfRunners": 3,
        "numberOfWinners": 1,
        "runners": [
                "handicap": 0.0,
                "lastPriceTraded": 9.8,
                "selectionId": 1096,
                "status": "ACTIVE",
                "totalMatched": 23760.79
                "handicap": 0.0,
                "lastPriceTraded": 1.37,
                "selectionId": 47999,
                "status": "ACTIVE",
                "totalMatched": 135125.67
                "handicap": 0.0,
                "lastPriceTraded": 5.9,
                "selectionId": 58805,
                "status": "ACTIVE",
                "totalMatched": 12800.14
        "runnersVoidable": false,
        "status": "OPEN",
        "totalAvailable": 1141737.01,
        "totalMatched": 171686.6,
        "version": 3282353613

The json response contains the lastPriceTraded data for all the selections in the betting market along with some other information. It is also possible to have the prices included that are available on the lay and back side. Please refer to the official Betfair API documentation for all the options of the listMarketBook endpoint.

Periodically Fetching Betfair Data

With the above Python script we are able to fetch Betfair data. However, we would need to manually trigger the script to actually get the data. I would like to discuss two options to automate this process and to periodically fetch price data.

One option is to use cron to schedule the execution of the Python script. 

Another possibility is using a infinite loop along with a sleep statement within the Python code.

Storing Betfair Data for Analysis

Obviously there is no point in just displaying the price data when running the Python script and we would rather like to persist the data on disk for analysis. Depending on what volume of data you intend to record and depending on how you access the data later, there are again multiple options:

Saving Data in a CSV File

The first option that I would like to cover is storing the Betfair data in a csv file. Especially if you intend to use software such as Excel or LibreOffice for analysis it might be a good choice to store the data in a tabular format. However, the problem is scalability: Especially if you intend to collect high resolution price data from a betting exchange a file-based approach using a json or csv format might limit your analytics options.

Saving Data in a Database

A more scalable approach involves capturing the the price or odds data in a database. There are multiple options again and you could use something like a SQL database (e.g. PostgreSQL, MySQL, etc) or a No-SQL database such as MongoDB for instance. Especially for larger volume of data storing Betfair odds in a database is more efficient than using files. You can also have some level of integrity on the data.

Next Steps

Instead of using a fixed market ID you might also want to use the "listMarketCatalogue" endpoint to fetch markets according to criteria such as only football markets in the UK that go inplay etc. Once you receive the data you might also process it further and save it in a csv file or database that you can then access and use at a later stage for your backtest. Instead of using your PC or laptop, you can also setup a VPS or server and deploy the script there.

Do you like our content? Please share with your friends!

Share on Facebook Share on Twitter


No comments published yet.

Please log in to leave a comment.