knitr::opts_chunk$set( collapse = TRUE, comment = "#>", fig.path = "man/figures/README-", out.width = "100%" )
B3 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:
For example, you can find the current yield curve at this link. Package rb3 uses webscraping tools to download and read these datasets from B3, 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.
The documentation is available in its pkgdown page, where articles (vignettes) with real applications can be found.
Package rb3 is available in its stable form in CRAN and its development version in Github. Please find the installation commands below:
# stable (CRAN) install.packages("rb3") # github (Development branch) if (!require(devtools)) install.packages("devtools") devtools::install_github("ropensci/rb3")
In this first example we'll import and plot the historical yield curve for Brazil using
function yc_get
.
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)
Get settlement future prices with futures_get
.
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 closing data (without ANY price adjustments) is available thru cotahist_get
.
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) )
One can also download hedge fund data with cotahist_etfs_get
.
glimpse( cotahist_etfs_get(ch) )
Download FII (Fundo de Investimento Imobiliário) data with cotahist_fiis_get
:
glimpse( cotahist_fiis_get(ch) )
Download BDR (Brazilian depositary receipts) with cotahist_bdrs_get
:
glimpse( cotahist_bdrs_get(ch) )
Download equity options contracts with cotahist_option_get
:
glimpse( cotahist_equity_options_get(ch) )
The list with available B3 indexes can be obtained with indexes_get
.
indexes_get()
And the composition of a specific index with index_comp_get
.
(ibov_comp <- index_comp_get("IBOV"))
With the index composition you can use COTAHIST to select their quotes.
glimpse( cotahist_get_symbols(ch, ibov_comp) )
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.
f <- download_marketdata("FPR", refdate = as.Date("2022-05-10")) f
download_marketdata
returns the path for the downloaded file.
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.
show_templates()
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.