![Page 1: The betfair package: An R implementation of the Betfair APIpast.rinfinance.com/agenda/2011/BryanLewis.pdf · The betfair package: An R implementation of the Betfair API Bryan Lewis](https://reader030.vdocuments.us/reader030/viewer/2022013104/5ac538087f8b9a220b8d3c50/html5/thumbnails/1.jpg)
The betfair package: An R implementation ofthe Betfair API
Bryan Lewis and Colin Magee
http://betwise.co.uk
R/Finance 2011
http://www.betwise.co.uk/smartform
![Page 2: The betfair package: An R implementation of the Betfair APIpast.rinfinance.com/agenda/2011/BryanLewis.pdf · The betfair package: An R implementation of the Betfair API Bryan Lewis](https://reader030.vdocuments.us/reader030/viewer/2022013104/5ac538087f8b9a220b8d3c50/html5/thumbnails/2.jpg)
Betfair
• Founded in 1999 by Andrew Black and Ed Wray.• Sports betting, casino games, poker, and more recently, FX
and CFD exchanges.• Processes more than five million transactions every day.• Three million betting exchange customers.
http://www.betwise.co.uk/smartform
![Page 3: The betfair package: An R implementation of the Betfair APIpast.rinfinance.com/agenda/2011/BryanLewis.pdf · The betfair package: An R implementation of the Betfair API Bryan Lewis](https://reader030.vdocuments.us/reader030/viewer/2022013104/5ac538087f8b9a220b8d3c50/html5/thumbnails/3.jpg)
Betting APIs
Global sports exchange• Real time market data feed and clearing house for: Horse
racing, tennis, football (including American), basketball, andalmost anything else you can bet on...
Games exchange• Poker, casino games
Exchanges operate across globally distributed data centers.
The APIs are free to use! (higher-speed commercial versions arealso available).
http://www.betwise.co.uk/smartform
![Page 4: The betfair package: An R implementation of the Betfair APIpast.rinfinance.com/agenda/2011/BryanLewis.pdf · The betfair package: An R implementation of the Betfair API Bryan Lewis](https://reader030.vdocuments.us/reader030/viewer/2022013104/5ac538087f8b9a220b8d3c50/html5/thumbnails/4.jpg)
The betfair Package
Implements the sports exchange API in R (depends on Rcurl∗).The API defines R functions in two broad categories:• Market data.• User account (place/update/cancel/monitor bets).
All functions use the https protocol and closely follow the syntax ofthe Betfair API documentation.
∗Rcurl is available for Windows fromhttp://www.stats.ox.ac.uk/pub/RWin/bin/windows/contrib/2.13/.
http://www.betwise.co.uk/smartform
![Page 5: The betfair package: An R implementation of the Betfair APIpast.rinfinance.com/agenda/2011/BryanLewis.pdf · The betfair package: An R implementation of the Betfair API Bryan Lewis](https://reader030.vdocuments.us/reader030/viewer/2022013104/5ac538087f8b9a220b8d3c50/html5/thumbnails/5.jpg)
Package FunctionsAccount-related
login logout keepAlive placeBets cancelBetscancelBetsByMarket updateBets getBetgetCurrentBets getBetHistorygetMatchedandUnmatchedBetsgetMarketProfitAndLoss getAccountFundsgetAccountStatement viewProfile
Data-related
convertCurrency getActiveEventTypesgetAllCurrencies getAllEventTypes getAllMarketsgetCompleteMarketPricesCompressedgetDetailAvailableMarketDepth getEventsgetInPlayMarkets getMarket getMarketInfogetMarketPrices getMarketTradedVolumegetPrivateMarkets getSilks
http://www.betwise.co.uk/smartform
![Page 6: The betfair package: An R implementation of the Betfair APIpast.rinfinance.com/agenda/2011/BryanLewis.pdf · The betfair package: An R implementation of the Betfair API Bryan Lewis](https://reader030.vdocuments.us/reader030/viewer/2022013104/5ac538087f8b9a220b8d3c50/html5/thumbnails/6.jpg)
Example Session
> library(’betfair’)> login(username=’******’, password=’********’)> getActiveEventTypes()[1:8,1:3]
id name nextMarketId6423 American Football 07511 Baseball 07522 Basketball 0
136332 Chess 04 Cricket 011 Cycling 03 Golf 013 Horse Racing - Todays Card 102707815
http://www.betwise.co.uk/smartform
![Page 7: The betfair package: An R implementation of the Betfair APIpast.rinfinance.com/agenda/2011/BryanLewis.pdf · The betfair package: An R implementation of the Betfair API Bryan Lewis](https://reader030.vdocuments.us/reader030/viewer/2022013104/5ac538087f8b9a220b8d3c50/html5/thumbnails/7.jpg)
Market Info
> getMarketInfo(102707815)$marketStatus[1] "ACTIVE"$marketSuspendTime[1] "2011-04-27 18:43:00 GMT"$numberOfRunners[1] 9$delay[1] 0$reconciled[1] FALSE$openForBspBetting[1] "true"
http://www.betwise.co.uk/smartform
![Page 8: The betfair package: An R implementation of the Betfair APIpast.rinfinance.com/agenda/2011/BryanLewis.pdf · The betfair package: An R implementation of the Betfair API Bryan Lewis](https://reader030.vdocuments.us/reader030/viewer/2022013104/5ac538087f8b9a220b8d3c50/html5/thumbnails/8.jpg)
Market Info
> getMarket(102707815)$runnersname selectionId
Monslewn 5503082Destiny Joy 5503083
Sweet Argument 5343101Dower 5503084
Napa Love 5503085Eager Leader 4914430Vivs Tiara 5503086
Daiseysgoneagain 4656431Rebas Affair 5503087Mi Dulce Koko 5503088
http://www.betwise.co.uk/smartform
![Page 9: The betfair package: An R implementation of the Betfair APIpast.rinfinance.com/agenda/2011/BryanLewis.pdf · The betfair package: An R implementation of the Betfair API Bryan Lewis](https://reader030.vdocuments.us/reader030/viewer/2022013104/5ac538087f8b9a220b8d3c50/html5/thumbnails/9.jpg)
Order Book
x = getCompleteMarketPricesCompressed(102707815)plotPrice(x[’5503084’]); plotPrice(x[’4914430’])
To back current price 2.94
Total amount matched 169.5
30 25 20 15 10 5 0
2.52
2.56
2.58
2.64
2.72
2.74
2.88
2.9
2.92
2.94
3.25
3.35
3.5
3.6
6.8
7.2
7.8
8.4
8.8
11.5
To lay current price 3.25
Last price matched 2.92
0 5 10 15 20 25 30
To back current price 9.8
Total amount matched 0.28
14 10 8 6 4 2 0
5.1
5.5
5.7
6.4
6.6
8.4
9.2
9.4
9.6
9.8
30
36
38
55
240
To lay current price 30
Last price matched 38
0 2 4 6 8 10 12 14
http://www.betwise.co.uk/smartform
![Page 10: The betfair package: An R implementation of the Betfair APIpast.rinfinance.com/agenda/2011/BryanLewis.pdf · The betfair package: An R implementation of the Betfair API Bryan Lewis](https://reader030.vdocuments.us/reader030/viewer/2022013104/5ac538087f8b9a220b8d3c50/html5/thumbnails/10.jpg)
Placing a Bet
bet = newBet(betType=’B’, marketId=102707815,price=2.9, selectionId=5503084, size=100)
b = placeBets(bet)
Note that bets may be partially matched. Interrogate bet statuswith:
gb = getBet(b$betId)gb$betStatus[1] "M"
gb$avgPrice[1] 2.90
http://www.betwise.co.uk/smartform
![Page 11: The betfair package: An R implementation of the Betfair APIpast.rinfinance.com/agenda/2011/BryanLewis.pdf · The betfair package: An R implementation of the Betfair API Bryan Lewis](https://reader030.vdocuments.us/reader030/viewer/2022013104/5ac538087f8b9a220b8d3c50/html5/thumbnails/11.jpg)
Simple Time Series Example
Many R packages for quantitative finance apply to savedexchange data.
Betfair does not (yet) provide a historic data API. We saved thematched back prices every second for a race at Cheltenham inthis example:
library(quantmod)load(’chelt.rdata’)LocalHero = bp1[,"Local Hero"]chartSeries(LocalHero, theme=chartTheme(’white’))overround = as.xts(rowSums(1/bp1) - 1,
order.by=index(bp1))addTA(overround, col=2)addBBands()
http://www.betwise.co.uk/smartform
![Page 12: The betfair package: An R implementation of the Betfair APIpast.rinfinance.com/agenda/2011/BryanLewis.pdf · The betfair package: An R implementation of the Betfair API Bryan Lewis](https://reader030.vdocuments.us/reader030/viewer/2022013104/5ac538087f8b9a220b8d3c50/html5/thumbnails/12.jpg)
Back Price for a Runner and Market Overround
2.6
2.8
3.0
3.2
3.4
LocalHero [2011−01−29 12:40:03/2011−01−29 12:55:57]
Last 3.3Bollinger Bands (20,2) [Upper/Lower]: 3.383/3.262
overround :0.014
0.000
0.005
0.010
0.015
0.020
0.025
0.030
Jan 2912:40:03
Jan 2912:42:00
Jan 2912:44:00
Jan 2912:46:00
Jan 2912:48:00
Jan 2912:50:00
Jan 2912:52:00
Jan 2912:54:00
Jan 2912:55:57
http://www.betwise.co.uk/smartform
![Page 13: The betfair package: An R implementation of the Betfair APIpast.rinfinance.com/agenda/2011/BryanLewis.pdf · The betfair package: An R implementation of the Betfair API Bryan Lewis](https://reader030.vdocuments.us/reader030/viewer/2022013104/5ac538087f8b9a220b8d3c50/html5/thumbnails/13.jpg)
Market Probabilities to Win Prior to Race
plotProbs(bp1)
0.0
0.2
0.4
0.6
0.8
1.0
Time
Mar
ket p
roba
bilit
ies
Mark Twain Third Intention Lapin Garou Maoi Chinn Tire Akula Local Hero Indian Daudaie
12:40:00 12:42:00 12:44:00 12:46:00 12:48:00 12:50:00 12:52:00 12:54:00
http://www.betwise.co.uk/smartform
![Page 14: The betfair package: An R implementation of the Betfair APIpast.rinfinance.com/agenda/2011/BryanLewis.pdf · The betfair package: An R implementation of the Betfair API Bryan Lewis](https://reader030.vdocuments.us/reader030/viewer/2022013104/5ac538087f8b9a220b8d3c50/html5/thumbnails/14.jpg)
Market Fundamentals Data
The Betfair API getSilks function provides basic ancillary datafor horse racing including age, breed, weight, days since last run,trainer, and jockey.
Colin and I also provide cheap subscription access to years ofdetailed historic market and fundamentals horse racing data,updated daily at:
http://www.betwise.co.uk/smartform
http://www.betwise.co.uk/smartform
![Page 15: The betfair package: An R implementation of the Betfair APIpast.rinfinance.com/agenda/2011/BryanLewis.pdf · The betfair package: An R implementation of the Betfair API Bryan Lewis](https://reader030.vdocuments.us/reader030/viewer/2022013104/5ac538087f8b9a220b8d3c50/html5/thumbnails/15.jpg)
An Example R-based Automated Trading Platform
Betfair
Market data collection script
Data StorageModeling and signal generation script(s)
Betting and account management scripts
• Three sets of independent R scripts.• Signals published via new Redis Publish/Subscribe or
Websockets.• Betting scripts run locally by users.
http://www.betwise.co.uk/smartform
![Page 16: The betfair package: An R implementation of the Betfair APIpast.rinfinance.com/agenda/2011/BryanLewis.pdf · The betfair package: An R implementation of the Betfair API Bryan Lewis](https://reader030.vdocuments.us/reader030/viewer/2022013104/5ac538087f8b9a220b8d3c50/html5/thumbnails/16.jpg)
Lots of Opportunity for Quantitative Methods
Gambling and mathematics have a long mutual history. The abilityto analyze and act on high-frequency real time betting markets isrelatively new and pretty interesting.
• Bias detection and classification (favorite/longshot bias).• Arbitrage detection.• Handicapping models.• Estimating optimal bet allocation.• Backtesting and simulation.• Time series analysis.
http://www.betwise.co.uk/smartform
![Page 17: The betfair package: An R implementation of the Betfair APIpast.rinfinance.com/agenda/2011/BryanLewis.pdf · The betfair package: An R implementation of the Betfair API Bryan Lewis](https://reader030.vdocuments.us/reader030/viewer/2022013104/5ac538087f8b9a220b8d3c50/html5/thumbnails/17.jpg)
On Deck
• This will be on CRAN and http://www.betwise.co.uk soon.• Games exchange REST API (a pokeR bot perhaps?).
USA residents: Please, write your Congressman and demandlegalization of Internet gambling in the United States.
http://www.betwise.co.uk/smartform