Last active
November 8, 2015 20:25
-
-
Save tomas-rampas/41fbf144c4057a48341b to your computer and use it in GitHub Desktop.
Correlation Sample
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
require(chron) | |
require(Hmisc) | |
require(ggplot2) | |
#this function creates and returns date time for indexing purposes later | |
f = function(d, t) as.chron(paste(strptime(d, "%Y.%m.%d"), t)) | |
#path to data, change for pointing your repository | |
path <- "G://TickData//" | |
#suffix of the file names with extension | |
suffix <- "_UTC+0_00_noweekends_fxmtf.csv" | |
#timeframe | |
tf <- "H1" | |
#in following lines data are read into the memory, note we are loading just Close price | |
#this could be done more effectivelly, | |
#but I'd like to leave soemthing for some upcoming articles :) | |
AUDUSD <- read.zoo(read.csv(file=paste(path, "AUDUSD_", tf, suffix, sep = ""), header=F, col.names=c("Date", "Time", "O","H","L","C","V")), header=F, index=1:2, FUN=f)$C | |
EURUSD <- read.zoo(read.csv(file=paste(path, "EURUSD_", tf, suffix, sep = ""), header=F, col.names=c("Date", "Time", "O","H","L","C","V")), header=F, index=1:2, FUN=f)$C | |
GBPUSD <- read.zoo(read.csv(file=paste(path, "GBPUSD_", tf, suffix, sep = ""), header=F, col.names=c("Date", "Time", "O","H","L","C","V")), header=F, index=1:2, FUN=f)$C | |
NZDUSD <- read.zoo(read.csv(file=paste(path, "NZDUSD_", tf, suffix, sep = ""), header=F, col.names=c("Date", "Time", "O","H","L","C","V")), header=F, index=1:2, FUN=f)$C | |
USDCAD <- read.zoo(read.csv(file=paste(path, "USDCAD_", tf, suffix, sep = ""), header=F, col.names=c("Date", "Time", "O","H","L","C","V")), header=F, index=1:2, FUN=f)$C | |
USDJPY <- read.zoo(read.csv(file=paste(path, "USDJPY_", tf, suffix, sep = ""), header=F, col.names=c("Date", "Time", "O","H","L","C","V")), header=F, index=1:2, FUN=f)$C | |
USDCHF <- read.zoo(read.csv(file=paste(path, "USDCHF_", tf, suffix, sep = ""), header=F, col.names=c("Date", "Time", "O","H","L","C","V")), header=F, index=1:2, FUN=f)$C | |
#merge everything into the single data frame | |
df <- merge(EURUSD, AUDUSD, GBPUSD, NZDUSD, USDCAD, USDJPY, USDCHF) | |
#calculate correlation, tail with parameter 11 instructs runtimes to get only last 11 observations | |
cor <- rcorr(tail(df,11), type="pearson") | |
#ggplot likes the data 'melted' one value per row | |
m <-melt(cor$r) | |
#plot it finally | |
ggplot(data=m, aes(x=Var1, y=Var2, fill=value)) + geom_tile() | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment