Docstoc

The betfair package An R implementation of the Betfair API

Document Sample
The betfair package An R implementation of the Betfair API Powered By Docstoc
					          The betfair package: An R implementation of
                         the Betfair API

                                     Bryan Lewis and Colin Magee
                                         http://betwise.co.uk



                                           R/Finance 2011




http://www.betwise.co.uk/smartform
   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
   Betting APIs


             Global sports exchange
                 • Real time market data feed and clearing house for: Horse
                   racing, tennis, football (including American), basketball, and
                   almost 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 are
     also available).




http://www.betwise.co.uk/smartform
   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 of
     the Betfair API documentation.


     ∗
      Rcurl is available for Windows from
     http://www.stats.ox.ac.uk/pub/RWin/bin/windows/contrib/2.13/.




http://www.betwise.co.uk/smartform
   Package Functions
     Account-related
     login logout keepAlive placeBets cancelBets
     cancelBetsByMarket updateBets getBet
     getCurrentBets getBetHistory
     getMatchedandUnmatchedBets
     getMarketProfitAndLoss getAccountFunds
     getAccountStatement viewProfile

     Data-related
     convertCurrency getActiveEventTypes
     getAllCurrencies getAllEventTypes getAllMarkets
     getCompleteMarketPricesCompressed
     getDetailAvailableMarketDepth getEvents
     getInPlayMarkets getMarket getMarketInfo
     getMarketPrices getMarketTradedVolume
     getPrivateMarkets getSilks
http://www.betwise.co.uk/smartform
   Example Session


     > library(’betfair’)
     > login(username=’******’, password=’********’)
     > getActiveEventTypes()[1:8,1:3]
         id                       name nextMarketId
       6423          American Football            0
       7511                   Baseball            0
       7522                 Basketball            0
     136332                      Chess            0
          4                    Cricket            0
         11                    Cycling            0
          3                       Golf            0
         13 Horse Racing - Todays Card    102707815



http://www.betwise.co.uk/smartform
   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
   Market Info


     > getMarket(102707815)$runners
                 name selectionId
             Monslewn     5503082
          Destiny Joy     5503083
       Sweet Argument     5343101
                Dower     5503084
            Napa Love     5503085
         Eager Leader     4914430
           Vivs Tiara     5503086
     Daiseysgoneagain     4656431
         Rebas Affair     5503087
        Mi Dulce Koko     5503088



http://www.betwise.co.uk/smartform
   Order Book

     x = getCompleteMarketPricesCompressed(102707815)
     plotPrice(x[’5503084’]); plotPrice(x[’4914430’])

           To back current price 2.94                   To lay current price 3.25          To back current price 9.8                  To lay current price 30


                                             11.5                                                                           240
                                             8.8
                                                                                                                            55
                                             8.4
                                             7.8                                                                            38

                                             7.2                                                                            36
                                             6.8
                                                                                                                            30
                                             3.6
                                             3.5                                                                            9.8

                                             3.35                                                                           9.6
                                             3.25
                                                                                                                            9.4
                                             2.94
                                             2.92                                                                           9.2

                                             2.9                                                                            8.4
                                             2.88
                                                                                                                            6.6
                                             2.74
                                             2.72                                                                           6.4
                                             2.64                                                                           5.7
                                             2.58
                                                                                                                            5.5
                                             2.56
                                             2.52                                                                           5.1



            30   25   20   15   10   5   0          0   5      10    15    20   25    30   14     10   8   6   4   2    0         0   2    4    6    8    10      12   14


            Total amount matched 169.5                      Last price matched 2.92         Total amount matched 0.28                     Last price matched 38




http://www.betwise.co.uk/smartform
   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 status
     with:

     gb = getBet(b$betId)
     gb$betStatus
     [1] "M"

     gb$avgPrice
     [1] 2.90



http://www.betwise.co.uk/smartform
   Simple Time Series Example
     Many R packages for quantitative finance apply to saved
     exchange data.

     Betfair does not (yet) provide a historic data API. We saved the
     matched back prices every second for a race at Cheltenham in
     this 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
   Back Price for a Runner and Market Overround

             LocalHero                                                                               [2011−01−29 12:40:03/2011−01−29 12:55:57]
                Last 3.3
                Bollinger Bands (20,2) [Upper/Lower]: 3.383/3.262                                                                                3.4




                                                                                                                                                 3.2




                                                                                                                                                 3.0




                                                                                                                                                 2.8




                                                                                                                                                 2.6
     0.030
                overround :
     0.025      0.014
     0.020
     0.015
     0.010
     0.005
     0.000

               Jan 29           Jan 29              Jan 29           Jan 29     Jan 29     Jan 29           Jan 29        Jan 29       Jan 29
              12:40:03         12:42:00            12:44:00         12:46:00   12:48:00   12:50:00         12:52:00      12:54:00     12:55:57




http://www.betwise.co.uk/smartform
   Market Probabilities to Win Prior to Race

     plotProbs(bp1)

                                         Mark Twain    Third Intention   Lapin Garou   Maoi Chinn Tire   Akula       Local Hero   Indian Daudaie
                            1.0
                            0.8
     Market probabilities

                            0.6
                            0.4
                            0.2
                            0.0




                            12:40:00   12:42:00       12:44:00           12:46:00      12:48:00           12:50:00     12:52:00      12:54:00


                                                                                         Time




http://www.betwise.co.uk/smartform
   Market Fundamentals Data



     The Betfair API getSilks function provides basic ancillary data
     for horse racing including age, breed, weight, days since last run,
     trainer, and jockey.

     Colin and I also provide cheap subscription access to years of
     detailed historic market and fundamentals horse racing data,
     updated daily at:
     http://www.betwise.co.uk/smartform




http://www.betwise.co.uk/smartform
   An Example R-based Automated Trading Platform

                                                              Betfair


                                     Market data collection             Betting and account
                                             script                     management scripts




                                                                        Modeling and signal
                                         Data Storage                   generation script(s)




         • 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
   Lots of Opportunity for Quantitative Methods


     Gambling and mathematics have a long mutual history. The ability
     to analyze and act on high-frequency real time betting markets is
     relatively 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
   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 demand
          legalization of Internet gambling in the United States.




http://www.betwise.co.uk/smartform

				
DOCUMENT INFO
Shared By:
Categories:
Tags:
Stats:
views:17
posted:11/9/2011
language:English
pages:17