/
scraper-yc.R
386 lines (357 loc) · 10.2 KB
/
scraper-yc.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
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
#' Fetches Yield Curve Data from B3
#'
#' Downloads yield curve data from B3 website
#' <https://www2.bmf.com.br/pages/portal/bmfbovespa/lumis/lum-taxas-referenciais-bmf-ptBR.asp>.
#' Particularly, we import data for
#' - DI X Pre (`yc_get`)
#' - Cupom limpo (`yc_usd_get`)
#' - DI x IPCA (`yc_ipca_get`)
#'
#' See <https://www.b3.com.br/data/files/8B/F5/11/68/5391F61043E561F6AC094EA8/Manual_de_Curvas.pdf>
#' for more details.
#'
#' @param refdate Specific date ("YYYY-MM-DD") to `yc_get` single curve
#' @param first_date First date ("YYYY-MM-DD") to `yc_mget` multiple curves
#' @param last_date Last date ("YYYY-MM-DD") to `yc_mget` multiple curves
#' @param by Number of days in between fetched dates (default = 1) in `yc_mget`
#' @param cache_folder Location of cache folder (default = cachedir())
#' @param do_cache Whether to use cache or not (default = TRUE)
#'
#' @details
#' `yc_get` returns the yield curve for the given date and `yc_mget` returns
#' multiple yield curves for a given range of dates.
#'
#' @return A dataframe/tibble with yield curve data
#'
#' @name yc_get
#'
#' @examples
#' \dontrun{
#' df_yc <- yc_mget(first_date = Sys.Date() - 5, last_date = Sys.Date())
#' head(df_yc)
#' }
#' @export
yc_mget <- function(first_date = Sys.Date() - 5,
last_date = Sys.Date(),
by = 1,
cache_folder = cachedir(),
do_cache = TRUE) {
first_date <- as.Date(first_date)
last_date <- as.Date(last_date)
# find biz days in between
tpl <- .retrieve_template(NULL, "TaxasReferenciais")
date_vec <- bizseq(first_date, last_date, tpl$calendar)
# use by to separate dates
date_vec <- date_vec[seq(1, length(date_vec), by = by)]
# get data!
df_yc <- bind_rows(
log_map_process_along(date_vec, get_single_yc,
"Fetching data points",
date_vec = date_vec,
cache_folder = cache_folder,
do_cache = do_cache
)
)
return(df_yc)
}
#' @rdname yc_get
#' @examples
#' \dontrun{
#' df_yc <- yc_get(Sys.Date())
#' head(df_yc)
#' }
#' @export
yc_get <- function(refdate = Sys.Date(),
cache_folder = cachedir(),
do_cache = TRUE) {
get_single_yc(1, as.Date(refdate), cache_folder, do_cache)
}
#' Fetches a single data
#'
#' @param idx_date index of data (1.. n_dates)
#' @param date_vec Vector of dates
#' @inheritParams yc_get
#'
#' @return A dataframe
#' @noRd
get_single_yc <- function(idx_date,
date_vec,
cache_folder,
do_cache) {
tpl_name <- "TaxasReferenciais"
tpl <- .retrieve_template(NULL, tpl_name)
refdate <- date_vec[idx_date]
fname <- download_marketdata(tpl_name, cache_folder, do_cache,
refdate = refdate,
curve_name = "PRE"
)
if (!is.null(fname)) {
df <- read_marketdata(fname, tpl_name, TRUE, do_cache)
if (!is.null(df)) {
tibble(
refdate = df$refdate,
cur_days = df$cur_days,
biz_days = bizdayse(refdate, .data$cur_days, tpl$calendar),
forward_date = add.bizdays(
refdate,
.data$biz_days, tpl$calendar
),
r_252 = df$col1 / 100,
r_360 = df$col2 / 100
)
} else {
NULL
}
} else {
alert("danger", "Error: no data found for date {refdate}",
refdate = refdate
)
return(NULL)
}
}
#' @details
#' `yc_ipca_get` returns the yield curve of real interest rates
#' for the given date and `yc_ipca_mget` returns
#' multiple yield curves of real interest rates for a given range of dates.
#' These real interest rates consider IPCA as its inflation index.
#'
#' @rdname yc_get
#' @examples
#' \dontrun{
#' df_yc_ipca <- yc_ipca_mget(
#' first_date = Sys.Date() - 5,
#' last_date = Sys.Date()
#' )
#' head(df_yc_ipca)
#' }
#' @export
yc_ipca_mget <- function(first_date = Sys.Date() - 5,
last_date = Sys.Date(),
by = 1,
cache_folder = cachedir(),
do_cache = TRUE) {
first_date <- as.Date(first_date)
last_date <- as.Date(last_date)
# find biz days in between
tpl <- .retrieve_template(NULL, "TaxasReferenciais")
date_vec <- bizseq(first_date, last_date, tpl$calendar)
# use by to separate dates
date_vec <- date_vec[seq(1, length(date_vec), by = by)]
# get data!
df_yc <- bind_rows(
log_map_process_along(date_vec, get_single_yc_ipca,
"Fetching data points",
date_vec = date_vec,
cache_folder = cache_folder,
do_cache = do_cache
)
)
return(df_yc)
}
#' @rdname yc_get
#' @examples
#' \dontrun{
#' df_yc_ipca <- yc_ipca_get(Sys.Date())
#' head(df_yc_ipca)
#' }
#' @export
yc_ipca_get <- function(refdate = Sys.Date(),
cache_folder = cachedir(),
do_cache = TRUE) {
get_single_yc_ipca(1, as.Date(refdate), cache_folder, do_cache)
}
#' Fetches a single data
#'
#' @param idx_date index of data (1.. n_dates)
#' @param date_vec Vector of dates
#' @inheritParams yc_get
#'
#' @return A dataframe
#' @noRd
get_single_yc_ipca <- function(idx_date,
date_vec,
cache_folder,
do_cache) {
tpl_name <- "TaxasReferenciais"
tpl <- .retrieve_template(NULL, tpl_name)
refdate <- date_vec[idx_date]
fname <- download_marketdata(tpl_name, cache_folder, do_cache,
refdate = refdate,
curve_name = "DIC"
)
if (!is.null(fname)) {
df <- read_marketdata(fname, tpl_name, TRUE, do_cache)
if (!is.null(df)) {
tibble(
refdate = df$refdate,
cur_days = df$cur_days,
biz_days = bizdayse(refdate, .data$cur_days, tpl$calendar),
forward_date = add.bizdays(
refdate,
.data$biz_days, tpl$calendar
),
r_252 = df$col1 / 100
)
} else {
NULL
}
} else {
alert("danger", "Error: no data found for date {refdate}",
refdate = refdate
)
return(NULL)
}
}
#' @rdname yc_get
#'
#' @details
#' `yc_usd_get` returns the yield curve of nominal interest rates for USD in
#' Brazil for the given date and `yc_usd_mget` returns
#' multiple yield curves of nominal interest rates for USD in Brazil for a
#' given range of dates.
#' These real interest rates consider IPCA as its inflation index.
#'
#' @examples
#' \dontrun{
#' df_yc_usd <- yc_usd_mget(
#' first_date = Sys.Date() - 5,
#' last_date = Sys.Date()
#' )
#' head(df_yc_usd)
#' }
#' @export
yc_usd_mget <- function(first_date = Sys.Date() - 5,
last_date = Sys.Date(),
by = 1,
cache_folder = cachedir(),
do_cache = TRUE) {
first_date <- as.Date(first_date)
last_date <- as.Date(last_date)
# find biz days in between
tpl <- .retrieve_template(NULL, "TaxasReferenciais")
date_vec <- bizseq(first_date, last_date, tpl$calendar)
# use by to separate dates
date_vec <- date_vec[seq(1, length(date_vec), by = by)]
# get data!
df_yc <- bind_rows(
log_map_process_along(date_vec, get_single_yc_usd,
"Fetching data points",
date_vec = date_vec,
cache_folder = cache_folder,
do_cache = do_cache
)
)
return(df_yc)
}
#' @rdname yc_get
#' @examples
#' \dontrun{
#' df_yc_usd <- yc_usd_get(Sys.Date())
#' head(df_yc_usd)
#' }
#' @export
yc_usd_get <- function(refdate = Sys.Date(),
cache_folder = cachedir(),
do_cache = TRUE) {
get_single_yc_usd(1, as.Date(refdate), cache_folder, do_cache)
}
#' Fetches a single data
#'
#' @param idx_date index of data (1.. n_dates)
#' @param date_vec Vector of dates
#' @inheritParams yc_get
#'
#' @return A dataframe
#' @noRd
get_single_yc_usd <- function(idx_date,
date_vec,
cache_folder,
do_cache) {
tpl_name <- "TaxasReferenciais"
tpl <- .retrieve_template(NULL, tpl_name)
refdate <- date_vec[idx_date]
fname <- download_marketdata(tpl_name, cache_folder, do_cache,
refdate = refdate,
curve_name = "DOC"
)
if (!is.null(fname)) {
df <- read_marketdata(fname, tpl_name, TRUE, do_cache)
if (!is.null(df)) {
tibble(
refdate = df$refdate,
cur_days = df$cur_days,
biz_days = bizdayse(refdate, .data$cur_days, tpl$calendar),
forward_date = add.bizdays(
refdate,
.data$biz_days, tpl$calendar
),
r_360 = df$col1 / 100
)
} else {
NULL
}
} else {
alert("danger", "Error: no data found for date {refdate}",
refdate = refdate
)
return(NULL)
}
}
#' Creates superset with yield curves and futures
#'
#' Creates superset with yield curves and future contracts indicating the
#' terms that match with futures contracts maturities.
#'
#' @param yc yield curve dataset
#' @param fut futures dataset
#'
#' @return
#' A dataframe with yield curve flagged with futures maturities.
#'
#' @name yc_superset
#'
#' @examples
#' \dontrun{
#' fut <- futures_get(Sys.Date() - 1)
#'
#' yc <- yc_get(Sys.Date() - 1)
#' yc_superset(yc, fut)
#'
#' yc_usd <- yc_usd_get(Sys.Date() - 1)
#' yc_usd_superset(yc_usd, fut)
#'
#' yc_ipca <- yc_ipca_get(Sys.Date() - 1)
#' yc_ipca_superset(yc_ipca, fut)
#' }
#' @export
yc_superset <- function(yc, fut) {
fut_di1 <- fut |>
filter(.data$commodity == "DI1") |>
mutate(forward_date = maturity2date(.data$maturity_code) |>
following("Brazil/ANBIMA")) |>
select("refdate", "forward_date", "symbol")
yc |>
left_join(fut_di1, by = c("refdate", "forward_date"))
}
#' @rdname yc_superset
#' @export
yc_usd_superset <- function(yc, fut) {
fut_di1 <- fut |>
filter(.data$commodity == "DDI") |>
mutate(forward_date = maturity2date(.data$maturity_code) |>
following("Brazil/ANBIMA")) |>
select("refdate", "forward_date", "symbol")
yc |>
left_join(fut_di1, by = c("refdate", "forward_date"))
}
#' @rdname yc_superset
#' @export
yc_ipca_superset <- function(yc, fut) {
fut_di1 <- fut |>
filter(.data$commodity == "DAP") |>
mutate(forward_date = maturity2date(.data$maturity_code, "15th day") |>
following("Brazil/ANBIMA")) |>
select("refdate", "forward_date", "symbol")
yc |>
left_join(fut_di1, by = c("refdate", "forward_date"))
}