Skip to content

Instantly share code, notes, and snippets.

@jaehyeon-kim
Created November 23, 2014 06:56
Show Gist options
  • Save jaehyeon-kim/7866083aad037605d093 to your computer and use it in GitHub Desktop.
Save jaehyeon-kim/7866083aad037605d093 to your computer and use it in GitHub Desktop.
library(lubridate)
library(stringr)
library(reshape2)
library(plyr)
library(dplyr)
# read all csv files and merge
# assumes files exist in data folder
dataDir <- "data"
files <- dir(dataDir, full.name = TRUE)
dataList <- llply(files, function(file){
data <- read.csv(file, stringsAsFactors = FALSE)
# get code from file path
pattern <- "/[A-Z][A-Z][A-Z][A-Z]"
code <- substr(str_extract(file, pattern), 2,
nchar(str_extract(file, pattern)))
# first column's name is funny
names(data) <- c("Date","Close")
data$Date <- dmy(data$Date)
data$Close <- as.numeric(data$Close)
# code will be used to 'dcast' data
data$Code <- code
data
}, .progress = "text")
data <- rbind_all(dataList)
# convert into a wide format
data <- dcast(data, Date ~ Code, value.var="Close")
# apply log difference column wise
dailyRet <- apply(log(data[, 2:dim(data)[2]]), 2, diff, lag=1)
# obtain daily return, variance and correlation
returns <- apply(dailyRet, 2, sum, na.rm = TRUE)
variance <- apply(dailyRet, 2, var, na.rm = TRUE)
correlation <- cor(dailyRet)
returns
variance
correlation
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment