public
Created

Scraping and plotting InTrade data

  • Download Gist
price_and_volume.R
R
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37
# 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)

Hi (and thank you for the nice gist)

when I run the following code line

timeAndSales$Time <- parse_date_time(timeAndSales$Date.and.time, "b d, Y I:M p")

I receive the following error:

Errore in parse_date_time(timeAndSales$Date.and.time, "b d, Y I:M p") :
No formats could be infered from the training set.

Any hint?
Best,
Luca

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.