phillc73 / abettor

An R package for connecting to the online betting exchange Betfair, via their API-NG product, using JSON-RPC.
Other
51 stars 36 forks source link

Help pulling market prices #60

Open samgledhill opened 2 years ago

samgledhill commented 2 years ago

Hi team - apologies in advance for logging an issue if this is not the right place to put it - but I wasn't sure how else to ask for help.

I'm attempting to build a betting bot and I'm keen to try and find market value amongst bookies, using the betfair lay markets as a guide close to jump time.

Abettor is magical in granting rapid, reliable access to the Betfair API - thanks so much for building it! My troubles start in the data-wrangling after executing a "listMarketBook" call. Essentially, I'm really struggling using any of the base or tidyverse methods to be able to pull the "availableToLay" list up to a level that also includes the marketId and SelectionId variables. ie - what I'd like to end up with is a long list of selectionId's (runner name would be even better, but let's start with what we've got first!) and prices - but I have to traverse through the "ex" list and that seems to be breaking every single time.

I've tried unnest(), unnest_longer() and unnest_wider(), hoist(), pluck() and flatten() but nothing seems to run without error!

Any tips or pointers that might help push me in the right direction?

I've tried all sorts of documentation for the tidyverse and this doesn't seem to be something that anyone on StackOverrflow has face either!

Again - apologies if this isn't the right forum - but I'm running out of ideas to get help!

Cheers,

Sam

Soccerama commented 2 years ago

Sam,

I don't think tidyverse handles these complex structures well. My code (which was written a while ago and may not be optimal) looks a bit like this. It catches some errors and empty fields but probably not all. Let me know if you are having trouble getting it to work:

eventWins <- listMarketCatalogue(eventTypeIds = "7", marketTypeCodes = "WIN", marketIds = yourMarketId, marketProjection = c("COMPETITION", "EVENT", "EVENT_TYPE", "RUNNER_DESCRIPTION",
                                      "MARKET_START_TIME", "MARKET_DESCRIPTION"))

        runnerData <- data.frame()
        winOdds <- listMarketBook(marketId = yourMarketId, priceData = "EX_BEST_OFFERS")
        numRunners <- nrow(winOdds$runners[[1]])
        for(j in 1:numRunners){
          runnerData[j,"runnerName"] <- eventWins$runners[[1]][[j,"runnerName"]]
          runnerData[j,"runnerNumber"] <- substr(runnerData[j,"runnerName"],1,str_locate(runnerData[j,"runnerName"]," ")[[1]]-2)
          if(placeOdds$status == "OPEN"){
            if(winOdds$runners[[1]]$status[[j]]=="ACTIVE"){
              runnerData[j,"selectionId"] <- placeOdds$runners[[1]]$selectionId[[j]]
              if(nrow(winOdds$runners[[1]]$ex[[1]][[j]])>0){
                runnerData[j,"bestLay"] <- winOdds$runners[[1]]$ex[[1]][[j]][[1,"price"]]
                runnerData[j,"layAmount"] <- winOdds$runners[[1]]$ex[[1]][[j]][[1,"size"]]
              } else {
                runnerData[j,"bestLay"] <- 1.01
                runnerData[j,"layAmount"] <- 5
              }
              if(nrow(winOdds$runners[[1]]$ex[[2]][[j]])>0){
                runnerData[j,"bestBack"] <- winOdds$runners[[1]]$ex[[2]][[j]][[1,"price"]] 
                runnerData[j,"backAmount"] <- winOdds$runners[[1]]$ex[[2]][[j]][[1,"size"]]
              } else {
                runnerData[j,"bestBack"] <- 1000
                runnerData[j,"backAmount"] <- 1
              }
              if(nrow(placeOdds$runners[[1]]$ex[[1]][[j]])>0){
                runnerData[j,"bestLayPlaceOdds"] <- placeOdds$runners[[1]]$ex[[1]][[j]][[1,"price"]]
              } else {
                runnerData[j,"bestLayPlaceOdds"] <- 1.01
              }
              if(nrow(placeOdds$runners[[1]]$ex[[2]][[j]])>0){
                runnerData[j,"bestBackPlaceOdds"] <- placeOdds$runners[[1]]$ex[[2]][[j]][[1,"price"]]
              } else {
                runnerData[j,"bestBackPlaceOdds"] <- 1000
              }
              numActiveRunners <- numActiveRunners + 1
            } else {
              runnerData[j,"bestLay"] <- 0
              runnerData[j,"layAmount"] <- 0
              runnerData[j,"bestBack"] <- 0
              runnerData[j,"backAmount"] <- 0
            }
          } else {
            runnerData[j,"bestLay"] <- 0
            runnerData[j,"layAmount"] <- 0
            runnerData[j,"bestBack"] <- 0
            runnerData[j,"backAmount"] <- 0
          }
        }