Skip to content

Instantly share code, notes, and snippets.

@rbresearch
Last active January 21, 2016 04:40
Show Gist options
  • Save rbresearch/5231939 to your computer and use it in GitHub Desktop.
Save rbresearch/5231939 to your computer and use it in GitHub Desktop.
Revised quandl to allow for loading multiple symbols
library(RJSONIO)
library(xts)
Quandl <- function(code, env = .GlobalEnv, type = c('raw', 'ts', 'zoo', 'xts'), start_date, end_date, transformation = c('', 'diff', 'rdiff', 'normalize', 'cumul'), collapse = c('', 'weekly', 'monthly', 'quarterly', 'annual'), rows, authcode = Quandl.auth()) {
## Flag to indicate frequency change due to collapse
freqflag = FALSE
# remove everything before the "/" for assigning the symbols to variable names
sym.names <- gsub(".*/", "", code)
## Check params
type <- match.arg(type)
transformation <- match.arg(transformation)
collapse <- match.arg(collapse)
## Helper function
frequency2integer <- function(freq) {
switch(freq,
'daily' = 365,
'monthly' = 12,
'quarterly' = 4,
'yearly' = 1,
1)
}
# Loop through the symbols to download the data and assign to the environment
for(i in 1:length(code)) {
## Build API URL and add auth_token if available
string <- paste("http://www.quandl.com/api/v1/datasets/", code[i], ".json?sort_order=asc&", sep="")
if (is.na(authcode))
warning("It would appear you aren't using an authentication token. Please visit http://www.quandl.com/help/r or your usage may be limited.")
else
string <- paste(string, "&auth_token=", authcode, sep = "")
## Add API options
if (!missing(start_date))
string <- paste(string, "&trim_start=", as.Date(start_date), sep = "")
if (!missing(end_date))
string <- paste(string,"&trim_end=", as.Date(end_date) ,sep = "")
if (transformation %in% c("diff", "rdiff", "normalize", "cumul"))
string <- paste(string,"&transformation=", transformation, sep = "")
if (collapse %in% c("weekly", "monthly", "quarterly", "annual")) {
string <- paste(string, "&collapse=", collapse, sep = "")
freq <- frequency2integer(collapse)
freqflag = TRUE
}
if (!missing(rows))
string <- paste(string,"&rows=", rows ,sep = "")
## Download and parse data
json <- try(fromJSON(string, nullValue = as.numeric(NA)), silent = TRUE)
## Check if code exists
if (inherits(json, 'try-error'))
stop("Code does not exist")
## Detect frequency
if (!freqflag)
freq <- frequency2integer(json$frequency)
## Shell data from JSON's list
data <- as.data.frame(matrix(unlist(json$data), ncol = length(json$column_names), byrow = TRUE),stringsAsFactors=FALSE)
names(data) <- json$column_names
data[,1] <- as.Date(data[, 1])
## Transform values to numeric
if (ncol(data) > 2)
data[, 2:ncol(data)] <- apply(data[, 2:ncol(data)], 2, as.numeric)
else
data[, 2] <- as.numeric(data[, 2])
## Returning raw data
if (type == "raw") {
fr <- data
# Assign the symbol to the environment
assign(sym.names[i], fr, env)
}
if (type == "ts") {
## Returning ts object
date <- data[1,1]
year <- 1900+as.POSIXlt(date)$year
startdate <- 1
if(freq == 1) {
start <- year
}
else if (freq == 4) {
quarter <- pmatch(quarters(date), c("Q1","Q2","Q3","Q4"))
startdate <- c(year, quarter)
}
else if (freq == 12) {
month <- 1+as.POSIXlt(date)$mon
startdate <- c(year, month)
}
else{
freq <- 1
}
fr <- ts(data[, -1], frequency = freq, start = startdate)
# Assign the symbol to the environment
assign(sym.names[i], fr, env)
}
## Returning zoo object
if (type == "zoo"){
fr <- zoo(data[c(-1)], data[,1])
# Assign the symbol to the environment
assign(sym.names[i], fr, env)
}
## Returning xts object
if (type == "xts"){
fr <- xts(data[c(-1)], data[,1])
assign(sym.names[i], fr, env)
}
## Just in case
# stop("Invalid Type")
# This is not necessary and an invalid type will be caught by match.arg
}
}
# Example to load US GDP, Nymex Crude Oil, and the S&P 500
datasets <- c("FRED/GDP", "OFDP/FUTURE_CL1", "YAHOO/INDEX_GSPC")
Quandl(code=datasets, type="foo", authcode="29Mxp9yC8iXyURxXwUys")
Quandl(code=datasets, type="zoo", authcode="29Mxp9yC8iXyURxXwUys")
Quandl(code=datasets, type="ts", authcode="29Mxp9yC8iXyURxXwUys")
Quandl(code=datasets, type="raw", authcode="29Mxp9yC8iXyURxXwUys")
Quandl(code=datasets, type="xts", authcode="29Mxp9yC8iXyURxXwUys")
head(FUTURE_CL1)
head(GDP)
head(INDEX_GSPC)
@daroczig
Copy link

Not sure if publishing an authcode is a good idea :)

Anyway, to be on-topic, why not simply running something like this with the original Quandl function instead of tweaking that:

for (code in c("FRED/GDP", "OFDP/FUTURE_CL1"))
    assign(make.names(code), Quandl(code))

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment