Skip to content

@dsparks /price_and_volume.R
Created

Embed URL

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Scraping and plotting InTrade data
# 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)
@LucaDeBenedictis

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Something went wrong with that request. Please try again.