Quandl: An example on the EUR/CHF FX and its odd behaviour

The R community is in luck because every day there are more and more companies and institutions which develop packages to link their products and R. That is one of the consequences of the expansion of R between the data scientist community and also reinforces itself through a feedback loop. The more interfaces with data sources, the more users switching to R; and then, the more companies interested in R. That is what in business jargon is called a virtuous circle.

Quite recently Dirk Eddelbuettel has announced a new interface to link Bloomberg and R. (Rblpapi) . That could be a major topic on the R community and I am looking forward to try it out.

But today I would like to show you another great data source “Quandl”. Quandl is a small company based on Toronto which puts together a huge variety of financial and economic data and offers it in a very user-friendly way. They have done I keep doing great job, so, I wish them the best.Of course, there is a non-free version for commercial purposes, but the free version is quite powerful and allows downloading up to 50 series per day. The only pre-requisite is the registration. The package is called “quandl” and you can download from the quandl website and from CRAN as well.

To show you how it works, lets have a look to the recent evolution of the FX EUR/CHF, a topic on which I am more or less forced to keep an eye because affects my pocket.

library(Quandl)
library(zoo)

Quandl.auth(«your Quandl Id: get it with your registration»)
today <- as.POSIXlt(Sys.Date())
start_date = today – years(20)
FX.tickers <- c(«CURRFX/EURCHF», «CURRFX/USDCHF», «CURRFX/USDEUR» )
FX.zoo <- Quandl(FX.tickers, type = «zoo», start_date, end_date = today, collapse = «weekly»)

# StackOverflow http://stackoverflow.com/questions/14064097/
zooToDf <- function(z) {
df <- as.data.frame(z)
df$Date <- time(z) #create a Date column
rownames(df) <- NULL # row names not filled with dates
df <- df[,c(ncol(df), 1:(ncol(df)-1))] #reorder
return(df)
}
FX.df <- zooToDf(FX.zoo)

FX.df <- FX.df[,c(grep(«Date»,colnames(FX.df)),grep(«Rate»,colnames(FX.df)))]
Headers <- unlist(lapply(FX.tickers, function(x) {strsplit(x,»/»)[[1]][2]}))
colnames(FX.df) <- c(«Date»,Headers)

In this example I decide to get the data in a zoo format to transform the object into a data frame right afterwards.