How to Place a Bet with abettor

Purpose

The purpose of this document is to demonstrate how to place a bet on the Betfair betting exchange using their API-NG product, the abettor R package and a simple R script.

Many of the steps outlined in this tutorial will be purposefully simplistic, in order to demonstrate a wide range of abettor functions. It is assumed that readers have no prior experience using any Betfiar API products.

Scope

This tutorial will outline a workflow for placing a simple Win Only bet on a horse racing market.

It is assumed that the bettor knows only the following three pieces of information:

Two further pieces of information must be obtained from Betfair:

An indication of the best price available and the market depth will also be demonstrated, although this is not strictly necessary for placing a bet.

Using just these few simple pieces of information it is possible to use the abettor package to place a bet.

Tutorial

Load Required Packages

Only two packages or libraries are required. All packages obviously must be installed first.

# Requires a minimum of version 1.7.2
# Older versions will most likely work but are unsupported
library("jsonlite")
# Requires a minimum of version 1.4.2
# Older versions will most likely work but are unsupported
library("httr")
require("abettor")

Login to Betfair

loginBF(username = "YourBFUsername", password = "YourBFPassword", applicationKey = "YourAppKey")

Application keys are straight forward to obtain. In short:

Betfair provides further details here.

After logging in, if all is well, you should not have received an error. There is no success message displayed by default by abettor. If you wish to check, you should look at the headersPostLogin variable. This will contain not only your application key, but also a relevant session token. Try headersPostLogin$'X-Authentication'

Find Relevant Event Type

Which sport? We need to know the relevant event type identification number for our chosen sport, in this case Horse Racing.

listEventTypes()

This will return a list of all sports with markets on Betfair. Horse racing has an event type ID of 7.

Find Relevant Country Code

This one is pretty straightforward. If you know your event venue, then you'll know which country it is in, but you will need to know the specific two letter country code.

listCountries(eventTypeIds = "7")

As you can see, we've passed an argument to the function, effectively limiting results to just countries with current horse racing markets.

More information regarding any function within the abettor package, and indeed any R package, may be found like this:

?listCountries

Find Relevant Market Type

What type of bet do you wish to place? Win, Place or antepost perhaps?

listMarketTypes(eventTypeIds = "7")

The returned results show just the market types available for Horse Racing.

Find Market and Selection IDs

Everything we've done so far has been very straightforward and easily achieved with a single abettor function and a couple of arguments. Now we start the real work in finding the relevant details for our bet.

Both the Market ID and Selection ID can be found through a single abettor function, but we'll need a little more R code to extract them.

marketCat <- listMarketCatalogue(eventTypeIds = "7", marketCountries = "GB", marketTypeCodes = "WIN")

We've used the listMarketCatalogue function, limiting results to Horse Racing in Great Britain and Win only markets. By default listMarketCatalogue returns results for markets starting immediately and 24 hours into the future. These default times may be changed. ?listMarketCatalogue for more details. We've also stored the returned data in a variable called marketCat.

Upon examining the marketCat variable you can immediately see it contains a lot of data. This data does include the Market ID and Selection ID we require. These can be viewed in isolation with the following:

marketCat$marketId
marketCat$runners[[1]]$selectionId

Have a really good look at marketCat, it contains a lot of data and is quite complex with nested lists and data frames.

head(marketCat)
str(marketCat)

As we already know a couple of things about the bet we wish to place, including the course name, start time and the horse name, we can now subset marketCat to return a much narrower and relevant set of data.

Subset using the known course name and race start time. In this case we have a sure thing in the last race on the Wolverhampton card at 17:40. Previously you will have taken note of the Betfair specific time format, such as that found in marketCat$marketStartTime

There's about eight million ways of subsetting data in R, this is just one of them.

ourSpecificRace <- marketCat[grep("^Wolverhampton", marketCat$event$venue), ]
ourSpecificRace <- ourSpecificRace[grep("^2014-12-15T17:40*", ourSpecificRace$marketStartTime),]

Here's our Market ID.

ourMarketId <- ourSpecificRace$marketId

Retain just the fields we want. ourSpecificRace$runners is a list with a nested data frame. The data we want is in the first element of that list.

runners <- ourSpecificRace$runners[[1]][ , c("selectionId","runnerName")]

Find the selectionId of just the horse we wish to bet on, a champion galloper called Memoria.

ourRunner <- runners[grep("^Memoria", runners$runnerName), ]
ourSelectionId <- ourRunner$selectionId

Check we have sensible looking data by examining the saved variable values.

ourMarketId
ourSelectionId

Find Available Prices and Market Depth

We're nearly there and have all the essential elements we need to place a bet. The last thing it would be useful to know is what prices are available for our chosen selection. listMarketBook supplies that information. Again we're dealing with nested lists and data frames.

ourMarketIdPrices <- listMarketBook(marketIds = ourMarketId, priceData = "EX_ALL_OFFERS")
allRunnersPrices <- ourMarketIdPrices$runners
ourSelectionIdPrices <- allRunnersPrices[[1]][which(allRunnersPrices[[1]]$selectionId == ourSelectionId),]

We're interested in backing to Win, so creating a final data frame with prices available and market depth can be achieved like this.

ourSelectionIdPricesDF <- data.frame(ourSelectionIdPrices$ex[[1]])

Create a variable to store the best price. This is not really needed, but may be handy later.

ourSelectionBestPrice <- ourSelectionIdPricesDF[1,c("price")]

At the time of writing, the best price available to back Memoria to Win was 6.4, with £8.33 available to be matched.

Place A Bet

Finally, we'll place a £2 bet using the placeOrders function.

PlaceBetReturn <- placeOrders(marketId = ourMarketId, 
                              selectionId = ourSelectionId, 
                              betSide = "BACK", 
                              betType = "LIMIT", 
                              betSize = "2", 
                              reqPrice = ourSelectionBestPrice, 
                              persistenceType = "LAPSE"
                              )

If everything has been successful, the PlaceBetReturn variable will contain a unique customer reference, which defaults to the exact date and time the bet was placed. See ?placeOrders to alter this.

We've now reached the end of this tutorial and hopefully you've successfully placed a bet on Betfair using R.

Final Words

If you ever need to know more about abettor functions from within R, simply type ?functionName.

It's probably a reasonable idea to consult the Betfair API-NG developer documentation, but it's by no means essential in order to successfully use abettor




phillc73/abettor documentation built on June 10, 2022, 4:43 p.m.