-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.R
219 lines (170 loc) · 5.79 KB
/
main.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
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
# load necessary packages ------------------------------------------------------
logger::log_info("Checking for necessary packages.")
is_installed <- function(packagename) {
packagename %in% utils::installed.packages()[, "Package"]
}
necessary_pkgs <-
unlist(
strsplit(
x = as.data.frame(read.dcf("DESCRIPTION"))$Imports,
split = ",\\n"
)
)
installed_pkgs <- unlist(lapply(X = necessary_pkgs, is_installed))
if (!all(installed_pkgs)) {
missing_pkgs <- necessary_pkgs[!installed_pkgs]
logger::log_error("Necessary packages are not installed: {missing_pkgs}")
stop()
}
logger::log_info("Loading necessary packages.")
suppressPackageStartupMessages({
library("dplyr")
})
# set general i/o paths --------------------------------------------------------
benchmarks_preparation_inputs_path <-
Sys.getenv(
"BENCHMARKS_PREPARATION_INPUTS_PATH",
"./inputs"
)
if (dir.exists(benchmarks_preparation_inputs_path)) {
logger::log_info("Setting benchmarks preparation inputs path: {benchmarks_preparation_inputs_path}")
} else {
logger::log_error("Benchmarks preparation inputs path does not exist: {benchmarks_preparation_inputs_path}")
stop()
}
benchmarks_preparation_outputs_path <- Sys.getenv(
"BENCHMARKS_PREPARATION_OUTPUTS_PATH",
"./outputs"
)
if (dir.exists(benchmarks_preparation_outputs_path)) {
logger::log_info("Setting benchmarks preparation outputs path: {benchmarks_preparation_outputs_path}")
} else {
logger::log_error("Benchmarks preparation outputs path does not exist: {benchmarks_preparation_outputs_path}")
stop()
}
# load config ------------------------------------------------------------------
logger::log_info("Loading config.")
config <-
config::get(
file = "config.yml",
config = Sys.getenv(x = "R_CONFIG_ACTIVE", unset = "2023Q4"),
use_parent = FALSE
)
# set and check paths ----------------------------------------------------------
msci_xlsx_path <-
file.path(
benchmarks_preparation_inputs_path,
config[["msci_filename"]]
)
if (!dir.exists(msci_xlsx_path) && file.exists(msci_xlsx_path)) {
logger::log_info("Setting MSCI XLSX input filepath: {msci_xlsx_path}")
} else {
logger::log_warn("MSCI XLSX input filepath does not exist: {msci_xlsx_path}")
msci_xlsx_path <- ""
}
# create output list ------------------------------------------------------
benchmark_portfolios <- list()
# scrape iShares indices data --------------------------------------------------
logger::log_info("Scraping iShares indices data")
logger::log_debug("Scraping iShares indices bonds data.")
ishares_indices_bonds <-
dplyr::bind_rows(
lapply(
seq_along(config[["bonds_indices_urls"]]), function(index) {
pacta.data.scraping::get_ishares_index_data(
config[["bonds_indices_urls"]][[index]],
names(config[["bonds_indices_urls"]])[[index]],
config[["ishares_date"]]
)
}
)
) %>%
pacta.data.scraping::process_ishares_index_data()
logger::log_debug("Scraping iShares indices equity data.")
ishares_indices_equity <-
dplyr::bind_rows(
lapply(
seq_along(config[["equity_indices_urls"]]), function(index) {
pacta.data.scraping::get_ishares_index_data(
config[["equity_indices_urls"]][[index]],
names(config[["equity_indices_urls"]])[[index]],
config[["ishares_date"]]
)
}
)
) %>%
pacta.data.scraping::process_ishares_index_data()
logger::log_debug("Combining iShares indices data.")
ishares_indices <- bind_rows(ishares_indices_bonds, ishares_indices_equity)
benchmark_portfolios <- c(benchmark_portfolios, list(ishares_indices))
# load MSCI indices ------------------------------------------------------------
if (nzchar(msci_xlsx_path)) {
sheet_names <- readxl::excel_sheets(path = msci_xlsx_path)[-1]
logger::log_info("Setting MSCI XLSX sheet names: {sheet_names}")
logger::log_debug("Reading and combining MSCI indices data.")
msci_indices <-
dplyr::bind_rows(
lapply(sheet_names, function(sheet_name) {
index_name <- paste0("MSCI - ", sub("_[0-9]*$", "", sheet_name))
readxl::read_excel(path = msci_xlsx_path, sheet = sheet_name) %>%
dplyr::mutate(investor_name = "Benchmark Portfolios") %>%
dplyr::mutate(portfolio_name = index_name) %>%
dplyr::mutate(currency = "USD") %>%
dplyr::select(
investor_name,
portfolio_name,
isin = `Isin Code`,
market_value = `Security Closing Weight`,
currency
)
})
)
benchmark_portfolios <- c(benchmark_portfolios, list(msci_indices))
} else {
logger::log_info("Skipping MSCI XLSX import because input file is not available.")
}
# combine all benchmarks -------------------------------------------------------
benchmark_portfolios <- dplyr::bind_rows(benchmark_portfolios)
if (nzchar(config[["project_code"]])) {
run_id <-
paste(
config[["pacta_financial_timestamp"]],
config[["project_code"]],
sep = "_"
)
} else {
run_id <- config[["pacta_financial_timestamp"]]
}
benchmarks_investor_name <-
paste0(
"Benchmark Portfolios ",
run_id
)
benchmark_portfolios <-
dplyr::mutate(
benchmark_portfolios,
investor_name = benchmarks_investor_name
)
# save output to output path ---------------------------------------------------
run_timestamp <- format(Sys.time(), "%Y%m%d-%H%M%S")
output_filename <-
paste0(
paste(
run_id,
"benchmark_portfolios",
run_timestamp,
sep = "_"
),
".rds"
)
output_filepath <-
file.path(
benchmarks_preparation_outputs_path,
output_filename
)
logger::log_info("Saving benchmark output file: {output_filepath}")
saveRDS(
object = benchmark_portfolios,
file = output_filepath
)
logger::log_info("Benchmark preparation complete!")