Last active
October 25, 2015 19:32
-
-
Save durtal/236a96730cb5bb342c2d to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
bf <- betfair(usr = Sys.getenv("BETFAIR_USR"), | |
pwd = Sys.getenv("BETFAIR_PWD"), | |
key = Sys.getenv("BETFAIR_KEY")) | |
outlist <- vector("list", 120) | |
for(i in 1:120) { | |
# sleep for 60seconds | |
Sys.sleep(60) | |
# collect marketCatalogue data, contains team names | |
tmp <- bf$marketCatalogue(filter = marketFilter(marketIds = "1.121198987"), | |
marketProjection = c("COMPETITION", "EVENT", "EVENT_TYPE", "RUNNER_DESCRIPTION")) | |
# collect marketBook data, unparsed response from Betfair API | |
tmp1 <- bf$marketBook(marketIds = "1.121198987") | |
# extract runners, with price, totalMatched data | |
runners <- tmp[[1]]$runners | |
prices <- plyr::rbind.fill(lapply(1:length(tmp1$result[[1]]$runners), function(x) { | |
data.frame(tmp1$result[[1]]$runners[[x]], time = Sys.time(), stringsAsFactors = FALSE) | |
})) | |
# merge two dataframes, by selectionId and place in list | |
outlist[[i]] <- merge(x = runners, | |
y = subset(prices, select = -handicap), | |
by = "selectionId", | |
sort = F) | |
print(i) | |
} | |
library(ggplot2) | |
derby <- plyr::ldply(outlist) | |
ggplot(derby, aes(x = time, y = 1 / lastPriceTraded, col = runnerName)) + | |
geom_point() + | |
geom_path() + | |
scale_color_manual(values = c(fantasysocceR::team_pal(), "The Draw" = "#000000")) + | |
RcappeR::theme_rcapper() + | |
labs(title = "Man Utd vs Man City\n25/10/15") + | |
theme(plot.title = element_text(face = "bold")) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment