betfaiR

This method still might require some work, especially in parsing the response from Betfair.

Having logged in using the betfair function, the environment contains the marketBook method, which can be used to get market data. You can use marketBook to retrieve data about the market, but more importantly the price data for the various runners in the market.

usage

bf$marketBook(marketIds = list(),
              priceProjection = "EX_BEST_OFFERS",
              orderProjection = "EXECUTABLE",
              matchProjection = "NO_ROLLUP",
              getRunners = NULL)

arguments

param details
marketIds string, one or more marketIds, the number of markets returned depends on the amount of data you request via the priceProjection
priceProjection the projection of price data you want to receive in the response, options include SP_AVAILABLE, SP_TRADED, EX_BEST_OFFERS, EX_ALL_OFFERS (trumps EX_BEST_OFFERS), and EX_TRADED, these are expained in Betting Enums under the Price Data header.
orderProjection the order Projection you wish to receive in the response, ALL all orders (entirely matched, or not), EXECUTABLE orders with unmatched portion, EXECUTION_COMPLETE orders with no unmatched portion, see Betting Enums under the Order Projection header.
matchProjection if you ask for orders via orderProjection, this will specify the representation of the matched bets, options are NO_ROLLUP returns raw fragments, ROLLED_UP_BY_PRICE rolled up matched amounts by distinct matched prices per side, and ROLLED_UP_BY_AVG_PRICE rolled up by average price per side, see Betting Enums under Match Projection header.
getRUnners if you wish to retrieve data about the runners, such as runners names (which are found in the [marketCatalogue])[methods_marketCatalogue.html] method) then you can supply either RUNNER_DESCRIPTION or RUNNER_METADATA to this parameter which will append runner data to the response

return

A list with 3 elements, there are two dataframes, one about the market, and one about the runners. The list also contains the raw response which may contain additional data that may be of interest. Some of this additional data could be market depth data if EX_ALL_OFFERS is entered into the priceProjection parameter. The list returned can be printed, allowing for quick glance at the market before users analyse the data further, or the list can be summarised.

examples

library(betfaiR)
# login
bf <- betfair(usr = Sys.getenv("bf_usr"),
              pwd = Sys.getenv("bf_pwd"),
              key = Sys.getenv("bf_key"))
Login successful
# get market data on just one market
tmp <- bf$marketCatalogue(filter = marketFilter(eventTypeIds = 1,
                                                marketTypeCodes = "MATCH_ODDS"),
                          marketProjection = "COMPETITION",
                          maxResults = 5)
summary(tmp)

Market ID:      1.129283812
Event ID:       2883157 
Market Name:    Match Odds
Event Name:     Israeli Premier League (W) 
Matched:        17575.75
 ---------------------------------------------------------------------------
Market ID:      1.129282478
Event ID:       2883157 
Market Name:    Match Odds
Event Name:     Israeli Premier League (W) 
Matched:        489.55
 ---------------------------------------------------------------------------
Market ID:      1.129281623
Event ID:       8616348 
Market Name:    Match Odds
Event Name:     Friendly 
Matched:        29087.73
 ---------------------------------------------------------------------------
Market ID:      1.129302418
Event ID:       5808320 
Market Name:    Match Odds
Event Name:     SPFL Development League 
Matched:        21229.97
 ---------------------------------------------------------------------------
Market ID:      1.129281559
Event ID:       5808320 
Market Name:    Match Odds
Event Name:     SPFL Development League 
Matched:        49138.95
 ---------------------------------------------------------------------------
# find market with most matched
ind <- which.max(sapply(tmp, function(i) i$market$totalMatched))
market <- tmp[[ind]]
summary(market)

Market ID:      1.129281559
Event ID:       5808320 
Market Name:    Match Odds
Event Name:     SPFL Development League 
Matched:        49138.95
 ---------------------------------------------------------------------------
market <- bf$marketBook(marketIds = market$market$marketId)
summary(market)

Market ID:      1.129281559 
Matched:        50532.89 
Available:      23457.72

Runners:     3 
     .id selectionId handicap status lastPriceTraded totalMatched
 7032151     7032151        0 ACTIVE            4.90      9865.55
 9001451     9001451        0 ACTIVE            5.10     35323.13
   58805       58805        0 ACTIVE            1.65      5344.19

 ---------------------------------------------------------------------------
# collect data on all 5 markets
markets <- bf$marketBook(marketIds = sapply(tmp, function(i) i$market$marketId))
summary(markets)

Market ID:      1.129281559 
Matched:        50532.89 
Available:      23468.72

Runners:     3 
     .id selectionId handicap status lastPriceTraded totalMatched
 7032151     7032151        0 ACTIVE            4.90      9865.55
 9001451     9001451        0 ACTIVE            5.10     35323.13
   58805       58805        0 ACTIVE            1.65      5344.19

 ---------------------------------------------------------------------------
Market ID:      1.129281623 
Matched:        29352.52 
Available:      17134.86

Runners:     3 
      .id selectionId handicap status lastPriceTraded totalMatched
  7458145     7458145        0 ACTIVE            1.01     28575.13
 12407873    12407873        0 ACTIVE         1000.00       340.32
    58805       58805        0 ACTIVE          120.00       437.07

 ---------------------------------------------------------------------------
Market ID:      1.129282478 
Matched:        904.19 
Available:      13354.89

Runners:     3 
     .id selectionId handicap status lastPriceTraded totalMatched
 6921686     6921686        0 ACTIVE            1.03       787.39
 9063891     9063891        0 ACTIVE          200.00        53.60
   58805       58805        0 ACTIVE           40.00        63.19

 ---------------------------------------------------------------------------
Market ID:      1.129283812 
Matched:        17669.58 
Available:      17152.45

Runners:     3 
     .id selectionId handicap status lastPriceTraded totalMatched
 6921676     6921676        0 ACTIVE            1.01     17453.56
 6921649     6921649        0 ACTIVE         1000.00       116.65
   58805       58805        0 ACTIVE          120.00        99.35

 ---------------------------------------------------------------------------
Market ID:      1.129302418 
Matched:        22782.58 
Available:      1322.21

Runners:     3 
     .id selectionId handicap status lastPriceTraded totalMatched
 6581076     6581076        0 ACTIVE            1.28     21047.29
 8981683     8981683        0 ACTIVE           27.00       790.64
   58805       58805        0 ACTIVE            5.00       944.65

 ---------------------------------------------------------------------------
# return runner data
marketId <- bf$marketCatalogue(filter = marketFilter(eventTypeIds = 7,
                                                     marketCountries = "GB",
                                                     marketTypeCodes = "WIN"))
summary(marketId)

Market ID:      1.129307035
Event ID:       28087305 
Market Name:    1m Hcap
Event Name:     Ling 25th Jan 
Matched:        3745.83
 ---------------------------------------------------------------------------
market_with_runner_data <- bf$marketBook(marketIds = marketId[[1]]$market$marketId,
                                         getRunners = "RUNNER_METADATA")
market_with_runner_data[[1]]$runners
$`11374273`
$basic
  selectionId handicap status adjustmentFactor lastPriceTraded
1    11374273        0 ACTIVE           21.979             4.9
  totalMatched
1       363.67

$ex
$back
  backPrice backSize
1       4.8    15.20
2       4.7    23.65
3       4.6    52.48

$lay
  layPrice laySize
1      5.0   14.41
2      5.1   12.12
3      5.2   39.16

attr(,"class")
[1] "list"                 "marketBook_runner_ex"

attr(,"class")
[1] "list"              "marketBook_runner"

$`11990284`
$basic
  selectionId handicap status adjustmentFactor lastPriceTraded
1    11990284        0 ACTIVE           21.979             3.2
  totalMatched
1      2051.33

$ex
$back
  backPrice backSize
1      3.20    20.16
2      3.15    46.15
3      3.10    72.00

$lay
  layPrice laySize
1     3.25   12.31
2     3.30   31.00
3     3.35   59.24

attr(,"class")
[1] "list"                 "marketBook_runner_ex"

attr(,"class")
[1] "list"              "marketBook_runner"

$`11333925`
$basic
  selectionId handicap status adjustmentFactor lastPriceTraded
1    11333925        0 ACTIVE           15.034               5
  totalMatched
1       298.65

$ex
$back
  backPrice backSize
1       5.3     7.62
2       5.2     5.60
3       5.0    12.00

$lay
  layPrice laySize
1      7.6   10.89
2     21.0   22.94

attr(,"class")
[1] "list"                 "marketBook_runner_ex"

attr(,"class")
[1] "list"              "marketBook_runner"

$`11799692`
$basic
  selectionId handicap status adjustmentFactor lastPriceTraded
1    11799692        0 ACTIVE           11.336             7.6
  totalMatched
1       547.48

$ex
$back
  backPrice backSize
1       7.4    11.71
2       7.2    17.15
3       7.0    48.00

$lay
  layPrice laySize
1      8.0   16.25
2      8.2   15.91
3      9.8    6.00

attr(,"class")
[1] "list"                 "marketBook_runner_ex"

attr(,"class")
[1] "list"              "marketBook_runner"

$`11511589`
$basic
  selectionId handicap status adjustmentFactor lastPriceTraded
1    11511589        0 ACTIVE           10.067             8.8
  totalMatched
1       256.97

$ex
$back
  backPrice backSize
1       8.8     6.07
2       8.6     2.04
3       7.4     2.51

$lay
  layPrice laySize
1      9.0   50.00
2      9.4    4.17
3      9.6   30.00

attr(,"class")
[1] "list"                 "marketBook_runner_ex"

attr(,"class")
[1] "list"              "marketBook_runner"

$`12162507`
$basic
  selectionId handicap status adjustmentFactor lastPriceTraded
1    12162507        0 ACTIVE            7.155            10.5
  totalMatched
1       288.35

$ex
$back
  backPrice backSize
1      10.5     6.93
2       9.8     2.92
3       9.6     9.57

$lay
  layPrice laySize
1     11.5    7.61
2     12.5    2.00
3     13.5    2.00

attr(,"class")
[1] "list"                 "marketBook_runner_ex"

attr(,"class")
[1] "list"              "marketBook_runner"

$`12081208`
$basic
  selectionId handicap status adjustmentFactor lastPriceTraded
1    12081208        0 ACTIVE            7.155              25
  totalMatched
1        38.14

$ex
$back
  backPrice backSize
1        28     3.75
2        27     2.93
3        19     2.51

$lay
  layPrice laySize
1      120   21.19

attr(,"class")
[1] "list"                 "marketBook_runner_ex"

attr(,"class")
[1] "list"              "marketBook_runner"

$`11198764`
$basic
  selectionId handicap status adjustmentFactor lastPriceTraded
1    11198764        0 ACTIVE            5.294              32
  totalMatched
1         33.8

$ex
$back
  backPrice backSize
1        32     3.06
2        26     5.76
3        25     3.19

$lay
  layPrice laySize
1       50    4.07
2       55    5.00
3       60    5.00

attr(,"class")
[1] "list"                 "marketBook_runner_ex"

attr(,"class")
[1] "list"              "marketBook_runner"

attr(,"class")
[1] "marketBook_runners"
str(market_with_runner_data[[1]]$runner_metadata)
'data.frame':   8 obs. of  32 variables:
 $ sire_name                : chr  "Makfi" "Cape Cross" "Dream Ahead" "Medicean" ...
 $ cloth_number_alpha       : chr  "4" "1" "3" "6" ...
 $ official_rating          : chr  "72" "75" "74" "68" ...
 $ colours_description      : chr  "White, dark blue star, dark blue sleeves, white stars, white cap, dark blue star" "Royal blue" "White and yellow (halved), sleeves reversed, red and white striped cap" "Royal blue and white diamonds, white sleeves, royal blue armlets, quartered cap" ...
 $ colours_filename         : chr  "c20170125lin/00830679.jpg" "c20170125lin/00012133.jpg" "c20170125lin/00842067.jpg" "c20170125lin/00001908.jpg" ...
 $ forecastprice_denominator: chr  "2" "2" "2" "2" ...
 $ damsire_name             : chr  "Clodovil" "Dubai Destination" "Choisir" "Averti" ...
 $ weight_value             : chr  "130" "133" "132" "126" ...
 $ sex_type                 : chr  "f" "f" "f" "f" ...
 $ days_since_last_run      : chr  "15" "36" "12" "7" ...
 $ owner_name               : chr  "Mr Saeed H. Altayer" "Godolphin" "Mrs Anne Coughlan & Ten Green Bottles" "Mr D. J. Deer" ...
 $ dam_year_born            : chr  "2007" "2009" "2006" "2004" ...
 $ sire_bred                : chr  "UK" "IRE" "USA" "UK" ...
 $ jockey_name              : chr  "Martin Lane" "Robert Tart" "Shane Kelly" "Kieran Shoemark" ...
 $ dam_bred                 : chr  "IRE" "UK" "IRE" "UK" ...
 $ adjusted_rating          : chr  "0" "0" "0" "0" ...
 $ runnerid                 : chr  "11374273" "11990284" "11333925" "11799692" ...
 $ cloth_number             : chr  "4" "1" "3" "6" ...
 $ sire_year_born           : chr  "2007" "1994" "2008" "1997" ...
 $ trainer_name             : chr  "Simon Crisford" "John Gosden" "David Elsworth" "Roger Charlton" ...
 $ colour_type              : chr  "b" "b" "ch" "b" ...
 $ age                      : chr  "3" "3" "3" "3" ...
 $ damsire_bred             : chr  "IRE" "USA" "AUS" "IRE" ...
 $ form                     : chr  "404-1" "4144-" "627-1" "97569-2" ...
 $ forecastprice_numerator  : chr  "7" "7" "11" "15" ...
 $ bred                     : chr  "GB" "IRE" "IRE" "GB" ...
 $ dam_name                 : chr  "Maid In Heaven" "Rythmic" "Choose Me" "Oystermouth" ...
 $ damsire_year_born        : chr  "2000" "1999" "1999" "1991" ...
 $ stall_draw               : chr  "7" "3" "6" "5" ...
 $ weight_units             : chr  "pounds" "pounds" "pounds" "pounds" ...
 $ wearing                  : chr  NA NA NA "cheekpieces" ...
 $ jockey_claim             : chr  NA NA NA "3" ...