-
Notifications
You must be signed in to change notification settings - Fork 27
/
README.rmd
259 lines (191 loc) · 6.24 KB
/
README.rmd
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
---
output: github_document
---
<!-- README.md is generated from README.Rmd. Please edit that file -->
```{r, include = FALSE}
knitr::opts_chunk$set(
collapse = TRUE,
comment = "#>",
fig.path = "man/figures/README-",
out.width = "100%"
)
```
# rb3 <img src="man/figures/logo.png" align="right" width="120" />
<!-- badges: start -->
[![Project Status: Active – The project has reached a stable, usable state and is being actively developed.](https://www.repostatus.org/badges/latest/active.svg)](https://www.repostatus.org/#active)
[![Codecov test coverage](https://codecov.io/gh/wilsonfreitas/rb3/branch/main/graph/badge.svg)](https://app.codecov.io/gh/wilsonfreitas/rb3?branch=main)
[![R build (rcmdcheck)](https://github.com/ropensci/rb3/workflows/R-CMD-check/badge.svg)](https://github.com/ropensci/rb3/actions)
[![CRAN status](https://www.r-pkg.org/badges/version/rb3)](https://CRAN.R-project.org/package=rb3)
[![](https://cranlogs.r-pkg.org/badges/rb3)](https://cran.r-project.org/package=rb3)
[![Status at rOpenSci Software Peer Review](https://badges.ropensci.org/534_status.svg)](https://github.com/ropensci/software-review/issues/534)
<!-- badges: end -->
[B3](https://www.b3.com.br) is the main financial exchange in Brazil, offering
support and access to trading systems for equity and fixed income markets.
In its website you can find a vast number of datasets regarding prices and transactions
for contracts available for trading at these markets, including:
* equities/stocks
* futures
* FII (Reits)
* options
* BDRs
* historical yield curves (calculated from futures contracts)
* B3 indexes composition
For example, you can find the current yield curve at this [link](https://www.b3.com.br/pt_br/market-data-e-indices/servicos-de-dados/market-data/consultas/mercado-de-derivativos/precos-referenciais/taxas-referenciais-bm-fbovespa/). Package **rb3** uses webscraping tools
to download and read these datasets from
[B3](https://www.b3.com.br), making it easy to consume it in R in a structured way.
The available datasets are highly valuable, going back as early as 2000's, and
can be used by industry practitioners or academics. None of these datasets are available
anywhere else, which makes rb3 an unique package for data importation from the
Brazilian financial exchange.
# Documentation
The documentation is available in its [pkgdown page](https://ropensci.github.io/rb3/),
where articles (vignettes) with real applications can be found.
## Installation
Package rb3 is available in its stable form in CRAN and its development version in Github.
Please find the installation commands below:
```r
# stable (CRAN)
install.packages("rb3")
# github (Development branch)
if (!require(devtools)) install.packages("devtools")
devtools::install_github("ropensci/rb3")
```
## Examples
### Yield curve
In this first example we'll import and plot the historical yield curve for Brazil using
function `yc_get`.
```{r setup}
library(rb3)
library(ggplot2)
library(stringr)
df_yc <- yc_mget(
first_date = Sys.Date() - 255 * 5,
last_date = Sys.Date(),
by = 255
)
p <- ggplot(
df_yc,
aes(
x = forward_date,
y = r_252,
group = refdate,
color = factor(refdate)
)
) +
geom_line() +
labs(
title = "Yield Curves for Brazil",
subtitle = "Built using interest rates future contracts",
caption = str_glue("Data imported using rb3 at {Sys.Date()}"),
x = "Forward Date",
y = "Annual Interest Rate",
color = "Reference Date"
) +
theme_light() +
scale_y_continuous(labels = scales::percent)
print(p)
```
### Futures prices
Get settlement future prices with `futures_get`.
```{r message=FALSE}
library(rb3)
library(dplyr)
df <- futures_mget(
first_date = "2022-04-01",
last_date = "2022-04-29",
by = 5
)
glimpse(
df |>
filter(commodity == "DI1")
)
```
### Equity data
Equity closing data (without **ANY** price adjustments) is available thru `cotahist_get`.
```{r}
library(rb3)
library(bizdays)
# fix for ssl error (only in linux)
if (Sys.info()["sysname"] == "Linux") {
httr::set_config(
httr::config(ssl_verifypeer = FALSE)
)
}
date <- preceding(Sys.Date() - 1, "Brazil/ANBIMA") # last business day
ch <- cotahist_get(date, "daily")
glimpse(
cotahist_equity_get(ch)
)
```
### Funds data
One can also download hedge fund data with `cotahist_etfs_get`.
```{r}
glimpse(
cotahist_etfs_get(ch)
)
```
### FIIs (Brazilian REITs) data
Download FII (Fundo de Investimento Imobiliário) data with `cotahist_fiis_get`:
```{r}
glimpse(
cotahist_fiis_get(ch)
)
```
### BDRs data
Download BDR (Brazilian depositary receipts) with `cotahist_bdrs_get`:
```{r}
glimpse(
cotahist_bdrs_get(ch)
)
```
### Equity options
Download equity options contracts with `cotahist_option_get`:
```{r}
glimpse(
cotahist_equity_options_get(ch)
)
```
### Indexes composition
The list with available B3 indexes can be obtained with `indexes_get`.
```{r}
indexes_get()
```
And the composition of a specific index with `index_comp_get`.
```{r}
(ibov_comp <- index_comp_get("IBOV"))
```
With the index composition you can use COTAHIST to select their quotes.
```{r}
glimpse(
cotahist_get_symbols(ch, ibov_comp)
)
```
## Template System
One important part of `rb3` infrastructure is its `Template System`.
All datasets handled by the rb3 package are configured in a template, that is
an YAML file.
The template brings many information regarding the datasets, like its
description and its metadata that describes its columns, their types and how
it has to be parsed.
The template fully describes its dataset.
Once you have the template implemented you can fetch and read downloaded
data directly with the functions `download_marketdata` and `read_marketdata`.
For examples, let's use the template `FPR` to download and read data regarding
primitive risk factor used by B3 in its risk engine.
```{r}
f <- download_marketdata("FPR", refdate = as.Date("2022-05-10"))
f
```
`download_marketdata` returns the path for the downloaded file.
```{r}
fpr <- read_marketdata(f, "FPR")
fpr
```
`read_marketdata` parses the downloaded file according to the metadata
configured in the template `FPR`.
Here it follows a view of the `show_templates` adding that lists the available
templates.
```r
show_templates()
```
<img src="man/figures/rb3-templates.png" width="100%" />