Repository navigation
Expand file tree
/
Copy pathR_script.R
More file actions
162 lines (137 loc) · 4.24 KB
/
Copy pathR_script.R
File metadata and controls
162 lines (137 loc) · 4.24 KB
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
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
# Load libraries ----
library(readxl)
library(quantmod)
library(igraph)
library(dplyr)
library(lubridate)
library(shiny)
library(imputeTS)
library(tidyverse)
setwd("//Users//radinatalanova//Downloads")
# # Import the available list of tickers ----
equity.tickers = read_excel("List_of_assets.xlsx", sheet = 1)
fixed.income.tickers = read_excel("List_of_assets.xlsx", sheet = 2)
commodities.tickers = read_excel("List_of_assets.xlsx", sheet = 3)
#
# # Create Environments
equity = list()
equity$tickers = equity.tickers$Ticker
equity$env = new.env()
fixed_income = list()
fixed_income$tickers = fixed.income.tickers$Ticker
fixed_income$env = new.env()
commodities = list()
commodities$tickers = commodities.tickers$Ticker
commodities$env = new.env()
# Download historical information ----
# For stocks:
getSymbols(
equity$tickers,
env = equity$env,
from = "2006-01-01",
"getSymbols.warning4.0" = FALSE
)
rm(.getSymbols, envir = equity$env)
save(list = ls(equity$env),
file = 'equity.RData',
envir = equity$env)
# There is no data for ticker AGN in yahoo finance. Removed it from Excel
# list of assets, because it brings an error when we load the data.
# For fixed income ETFs:
getSymbols(
fixed_income$tickers,
env = fixed_income$env,
from = "2006-01-01",
"getSymbols.warning4.0" = FALSE
)
rm(.getSymbols, envir = fixed_income$env)
save(
list = ls(fixed_income$env),
file = 'fixed_income.RData',
envir = fixed_income$env
)
# For commodity ETFs:
getSymbols(
commodities$tickers,
env = commodities$env,
from = "2006-01-01",
"getSymbols.warning4.0" = FALSE
)
rm(.getSymbols, envir = commodities$env)
save(
list = ls(commodities$env),
file = 'commodities.RData',
envir = commodities$env
)
# # Load data (not downloading it every time) ----
#load("equity.RData", envir=equity$env)
#load('fixed_income.RData', envir=fixed_income$env)
#load('commodities.RData', envir=commodities$env)
# Create data frames with adjusted prices ----
eq = lapply(names(equity$env), get, envir = equity$env)
fi = lapply(names(fixed_income$env), get, envir = fixed_income$env)
com = lapply(names(commodities$env), get, envir = commodities$env)
# Equity data frame
eq_dd = list()
for (i in 1:length(eq)) {
eq_dd[[i]] = eq[[i]][, 6]
}
eq_dd = do.call(merge, eq_dd)
colnames(eq_dd) = gsub(".Adjusted", "", colnames(eq_dd))
eq_dd = as.data.frame(eq_dd)
# Fixed income ETFs data frame
fi_dd = list()
for (i in 1:length(fi)) {
fi_dd[[i]] = fi[[i]][, 6]
}
fi_dd = do.call(merge, fi_dd)
colnames(fi_dd) = gsub(".Adjusted", "", colnames(fi_dd))
fi_dd = as.data.frame(fi_dd)
# Commodities data frame
com_dd = list()
for (i in 1:length(com)) {
com_dd[[i]] = com[[i]][, 6]
}
com_dd = do.call(merge, com_dd)
colnames(com_dd) = gsub(".Adjusted", "", colnames(com_dd))
com_dd = as.data.frame(com_dd)
rm(eq, fi, com, i)
rm(commodities.tickers, equity.tickers, fixed.income.tickers)
# Bind data frames
all_dd = cbind(eq_dd, fi_dd, com_dd)
all_dd$row_names = rownames(all_dd)
all_dd$date = as.Date(all_dd$row_names)
all_dd = all_dd[,!(names(all_dd) %in% c("row_names"))] # remove row_names column
rownames(all_dd) = all_dd$date
testdates = all_dd
all_dd$date = as.Date(all_dd$date)
all_dd = all_dd %>%
complete(date = seq(min(all_dd$date), max(all_dd$date), by = "day"))
# #inpute missing dates
testdates$date = as.Date(testdates$date)
testdates = testdates %>%
complete(date = seq(min(testdates$date), max(testdates$date), by = "day"))
# missing values interpolation
testdates <- na_interpolation(testdates, option = "linear")
testdates = as.data.frame(testdates)
rownames(testdates) = as.character(testdates$date)
#remove date from the set
final = testdates[, !names(testdates) %in% c("date")]
rownames(final) = rownames(testdates)
# Perform rolling sample estimation of the correlation matrix ---
date = ymd(rownames(final))
rdate = date[-1]
# Function for calculation of returns
rets = function(x) {
lx = dplyr::lag(x)
r = (x - lx) / lx
return (r[-1])
}
rr = as.data.frame(sapply(final, rets))
rownames(rr) = rdate
n = 120 # length of the sliding window
test_cc = list()
suppressWarnings(for (i in 1:(nrow(rr) - n + 1)) {
test_cc[[i]] = cor(rr[i:(i + n - 1), ], use = "pairwise.complete.ob")
})
names(test_cc) = as.character(date[n:nrow(rr)])