-
-
Save abresler/3965962 to your computer and use it in GitHub Desktop.
Scraping and plotting InTrade data
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
# Scraping and plotting InTrade data | |
doInstall <- TRUE # Change to FALSE if you don't want packages installed. | |
toInstall <- c("ggplot2", "lubridate") | |
if(doInstall){install.packages(toInstall, repos = "http://cran.r-project.org")} | |
lapply(toInstall, library, character.only = TRUE) | |
# Fin the "contractId" of the contract in which you're interested | |
# Let's try Barack Obama to win the third Presidential debate: | |
# http://www.intrade.com/v4/markets/contract/?contractId=766623 | |
contractID <- 766623 # second debate is 766621 | |
# InTrade makes it easy to get their contract data: | |
URL <- paste0("http://data.intrade.com/graphing/jsp/downloadTaS.jsp?contractId=", | |
contractID, | |
"&timezone=US/Eastern") | |
timeAndSales <- read.csv(URL, as.is = T) | |
head(timeAndSales) | |
# Use lubridate to convert characters to POSIXlt date/times: | |
timeAndSales$Time <- parse_date_time(timeAndSales$Date.and.time, | |
"b d, Y I:M p") | |
# Narrow window of interest | |
startCutoff <- ymd_hms("2012-10-22 20:00:00") # So | |
endCutoff <- ymd_hms("2012-10-22 23:30:00") # easy! | |
timeAndSales <- with(timeAndSales, timeAndSales[Time > startCutoff & Time < endCutoff, ]) | |
zp1 <- ggplot(data = timeAndSales) # \/ as.numeric seems necessary to avoid an error | |
zp1 <- zp1 + geom_vline(aes(xintercept = as.numeric(Time), alpha = Volume), | |
lwd = 2, colour = gray(1/3)) | |
zp1 <- zp1 + geom_smooth(aes(x = Time, y = Price)) | |
zp1 <- zp1 + geom_line(aes(x = Time, y = Price), | |
colour = "firebrick") | |
zp1 <- zp1 + scale_x_datetime() | |
zp1 <- zp1 + theme_bw() | |
zp1 <- zp1 + scale_y_continuous(limits = c(0, 100), expand = c(0, 0)) | |
zp1 <- zp1 + ggtitle("InTrade Price for\n\"Barack Obama to win the third Presidential debate\"") | |
print(zp1) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment