-
Notifications
You must be signed in to change notification settings - Fork 0
/
global.R
53 lines (44 loc) · 1.13 KB
/
global.R
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
# list.of.packages <- c("ggplot2", "Rcpp")
# new.packages <- list.of.packages[!(list.of.packages %in% installed.packages()[,"Package"])]
# if(length(new.packages)) install.packages(new.packages)
library(Quandl)
library(ggplot2)
library(lubridate)
library(dplyr)
library(reshape2)
library(scales)
library(ggthemes)
library(PerformanceAnalytics)
library(quantmod)
library(plotly)
BASE <- 'EUREKA'
read.quandl <- function(qcode){
data <- Quandl(qcode$Code)
colnames(data) <- c('Date', qcode$Name)
data
}
returns.data.frame <- function(x){
df <- x[[1]]
for (i in seq(x)[-1]){
df <- full_join(df, x[[i]], by = 'Date')
}
return(df)
}
to.xts <- function(x){
x1 <- as.xts(x[,-1],order.by = x[,1])
}
attach <- function(x) paste(BASE, x, sep='/')
add_zeros <- function(x){
zeros <- x[1,1:ncol(x)]
zeros$Date <- zeros$Date %m-% months(1)
zeros[,2:ncol(zeros)] <- 0
return(rbind(zeros, x))
}
rank.strategies <- function(x){
data.frame("Date"=x[,1], t(apply(-x[,-1], 1, rank, ties.method = 'min')))
}
# Get upper triangle of the correlation matrix
get.upper.tri <- function(cormat){
cormat[lower.tri(cormat)]<- NA
return(cormat)
}