-
Notifications
You must be signed in to change notification settings - Fork 1
/
MSCI Functions.R
138 lines (114 loc) · 3.78 KB
/
MSCI Functions.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
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
#############################
###### MSCI FUNCTIONS #######
#############################
# These scripts will download data from MSCI's end of day history app, found here:
# https://www.msci.com/end-of-day-history?chart=country&priceLevel=41&scope=R&style=B¤cy=15&size=36&indexId=119152
# Download data
msci.download <- function(countries,
countrylabels = character(),
startDate = "1969-12-29",
endDate = Sys.Date(),
priceLevel = "41",
currency = "15",
baseValue = "true",
annual = FALSE,
change = FALSE,
rank = FALSE) {
# Setup
require(quantmod)
require(XLConnect)
options(stringsAsFactors = FALSE)
# load download code dataframe
codeDF <- msci.list()
# extra codes given by user
codes <- codeDF[codeDF$Country.Code %in% countries,]
# Convert start and end dates to URL format
# start date
startdate <- as.Date(startDate)
startchar <- format(startdate, format = "%d %b, %Y")
startchar <- gsub(" ","%20",startchar)
# end date
enddate <- as.Date(endDate)
endchar <- format(enddate, format = "%d %b, %Y")
endchar <- gsub(" ","%20",endchar)
# construct the URL
URL <- paste0("https://www.msci.com/webapp/indexperf/charts?indices=",
paste(codes$Download.Code, collapse = "|"),
"&startDate=",
startchar,
"&endDate=",
endchar,
"&priceLevel=",
priceLevel,
"¤cy=",
currency,
"&frequency=D&scope=R&format=XLS&baseValue=",
baseValue,
"&site=gimi")
### Download and Process Initial Data ###
# Download file
download.file(URL, destfile = "msci file.xls", mode = "wb")
# Load file into variable
msciWB <- XLConnect::loadWorkbook("msci file.xls")
# Extract sheet from WB
index <- XLConnect::readWorksheet(msciWB, sheet = 1, startRow = 7)
# Subset resulting DF to not include Copyright text at the bottom
index <- index[1:(as.numeric(head(rownames(index[is.na(index$Date),]),1)) - 1), ]
### Replace column names with user's codes ###
# create named vector of country codes
codesvec <- codes$Country.Code
names(codesvec) <- codes$Col.Name
# Replace index names with country codes by looking them up from named vector
names(index) <- c("Date",codesvec[names(index)[-1]])
### Further Processing ###
#Convert to date
index$Date <- as.Date(index$Date)
# Fill in blanks
if(length(countries)>1) {
index[, -1] <- apply(index[, -1], 2, na.locf, na.rm = FALSE, maxgap = 12)
} else {
index[,2] <- na.locf(index[,2], na.rm = FALSE, maxgap = 12)
}
# Remove file after finishing
file.remove("msci file.xls")
#### Conditional Transformations ####
## Convert to annual if requested
if(annual) {
index <- index[format(index$Date, format = "%m") == "12", ]
index$Date <- format(index$Date, format = "%Y")
}
## Convert to % change if requested
if(change) {
if(length(countries) > 1) {
index[, -1] <- apply(index[,-1], 2, Delt)
} else {
index[,2] <- as.numeric(Delt(index[,2]))
}
}
## Convert to rank if requested
if(rank) {
# If more than 1 country, convert to rank
if(length(countries) > 1) {
index[, -1] <- t(apply(index[, -1], 1, function(x) rank(-x, na.last = "keep", ties.method = "min")))
} else {
# If only one country, return warning
warning("Only one index downloaded, returning unranked value.")
}
}
## Change column names, if labels provided
if(length(countrylabels) != 0){
if(length(countrylabels) != length(countries)){
warning("Country labels length different from country codes length. Using country codes as labels.")
} else {
for(i in 1:length(countries)) {
names(index) <- replace(names(index), names(index) == countries[i], countrylabels[i])
}
}
}
return(index)
}
# List countries with download codes
msci.list <- function() {
codeDF <- read.csv("https://raw.githubusercontent.com/JeremyBowyer/MSCI-Indices/master/MSCI%20Download%20Codes.csv")
return(codeDF)
}