Skip to content

Instantly share code, notes, and snippets.

@creative-quant
Last active August 29, 2015 14:14
Show Gist options
  • Select an option

  • Save creative-quant/dd41c9b94b9225577992 to your computer and use it in GitHub Desktop.

Select an option

Save creative-quant/dd41c9b94b9225577992 to your computer and use it in GitHub Desktop.
CAD vs WTI
#sources
#https://www.quandl.com/CURRFX/USDCAD-Currency-Exchange-Rates-USD-vs-CAD
#curl https://www.quandl.com/api/v1/datasets/CURRFX/USDCAD.csv -o /tmp/USDCAD.csv
#https://www.quandl.com/DOE/RWTC-WTI-Crude-Oil-Spot-Price-Cushing-OK-FOB
#curl https://www.quandl.com/api/v1/datasets/DOE/RWTC.csv -o /tmp/RWTC.csv
library(xts)
library(ggplot2)
usd.cad <- read.csv("/tmp/USDCAD.csv")
usd.cad <- as.xts( usd.cad[,2], as.Date( usd.cad[,1], format = "%Y-%m-%d" ) )
wti <- read.csv("/tmp/RWTC.csv")
wti <- as.xts( wti[,2], as.Date( wti[,1], format = "%Y-%m-%d" ) )
start <- as.Date( ifelse( start( wti ) < start(usd.cad), start(usd.cad), start(wti)) )
end <- as.Date( ifelse( end( wti ) > end(usd.cad), end(usd.cad), end(wti)) )
df <- merge.xts( usd.cad[paste(start,end,sep="::")], wti[paste(start,end,sep="::")] )
df <- scale(df)
df <- data.frame(date=index(df),usd.cad=as.double(df[,1]),wti=as.double(df[,2]))
plot <- ggplot( df, aes(x=date, y=usd.cad*-1) ) +
geom_line( colour="red" ) +
geom_line( aes( x=date, y=wti) )
plot
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment