Skip to content
forked from ropensci/rb3

A bunch of downloaders and parsers for data delivered from B3

License

Notifications You must be signed in to change notification settings

wilsonfreitas/rb3

 
 

Repository files navigation

rb3 rb3

Project Status: Active – The project has reached a stable, usable state and is being actively developed. Codecov test coverage R build (rcmdcheck) CRAN status rb3 downloads Status at rOpenSci Software Peer Review

Overview

B3 is the main financial exchange in Brazil, offering access to trading systems for stocks, futures, options, and other financial instruments. The rb3 package provides tools for downloading, processing, and analyzing market data from B3, including:

  • Stocks & Equities: Historical price data through COTAHIST files
  • Futures Contracts: Settlement prices for futures contracts
  • Yield Curves: Historical yield curves calculated from futures contracts
  • ETFs & BDRs: Data for Exchange Traded Funds and Brazilian Depositary Receipts
  • REITs (FIIs): Data for Brazilian Real Estate Investment Trusts
  • Options: Equity and index options contracts
  • Market Indices: B3 indices composition and historical data

The package uses webscraping tools to obtain data directly from B3’s website, making it accessible in R as structured datasets. These datasets are valuable for financial analysis, academic research, and investment decision-making.

Installation

# From CRAN (stable version)
install.packages("rb3")

# Development version from GitHub
if (!require(devtools)) install.packages("devtools")
devtools::install_github("ropensci/rb3")

Basic Usage

Market Data Templates

The rb3 package uses a template system to standardize the downloading and processing of different data types. To see available templates:

library(rb3)

# List available templates
list_templates()
#> # A tibble: 5 × 4
#>   Description                                         Template Reader Downloader
#>   <chr>                                               <chr>    <chr>  <chr>     
#> 1 Arquivo de Preços de Mercado - BVBG-086             b3-bvbg… ✅     ✅        
#> 2 Cotações Históricas do Pregão de Ações - Arquivo D… b3-cota… ✅     ✅        
#> 3 Cotações Históricas do Pregão de Ações - Arquivo A… b3-cota… ✅     ✅        
#> 4 Preços de Ajustes Diários de Contratos Futuros      b3-futu… ✅     ✅        
#> 5 Taxas referenciais                                  b3-refe… ✅     ✅

Downloading Market Data

The main function for fetching data is fetch_marketdata(), which downloads data based on a template and parameters:

# Download yield curve data for specific dates
library(bizdays)
fetch_marketdata("b3-reference-rates",
  refdate = bizseq("2024-01-01", "2024-01-31", "Brazil/B3"),
  curve_name = c("PRE", "DIC")
)

Working with Historical Equity Data

# Download yearly COTAHIST files
fetch_marketdata("b3-cotahist-yearly", year = 2023)

# Access the data
ch <- cotahist_get("yearly")

# Filter for stocks
eq <- ch |>
  filter(year(refdate) == 2023) |>
  cotahist_filter_equity()

# Get top 10 most traded stocks
symbols <- eq |>
  group_by(symbol) |>
  summarise(volume = sum(volume)) |>
  arrange(desc(volume)) |>
  head(10) |>
  pull(symbol)

Yield Curve Analysis

# Access yield curve data
library(ggplot2)

# Get Brazilian nominal yield curve (PRE)
yc_data <- yc_brl_get() |>
  filter(refdate == "2024-01-31") |>
  collect()

# Plot the yield curve
ggplot(yc_data, aes(x = forward_date, y = r_252)) +
  geom_line() +
  labs(
    title = "Brazilian Yield Curve (PRE)",
    x = "Forward Date",
    y = "Annual Interest Rate"
  ) +
  scale_y_continuous(labels = scales::percent)

Futures Contracts

# Get futures settlement prices
futures_data <- futures_get() |>
  filter(commodity == "DI1") |>
  collect()

# Calculate implied rates
di1_futures <- futures_data |>
  mutate(
    maturity_date = maturity2date(maturity_code),
    business_days = bizdays(refdate, maturity_date, "Brazil/ANBIMA"),
    implied_rate = (100000 / price)^(252 / business_days) - 1
  )

Documentation

For comprehensive documentation and examples, visit:

Citation

If you use rb3 in your research, please cite it:

To cite rb3 in publications use:

  Download and parse public files released by B3 and convert them into
  useful formats and data structures common to data analysis
  practitioners.

A BibTeX entry for LaTeX users is

  @Manual{rb3-package,
    title = {Download and Parse Public Data Released by B3 Exchange},
    author = {Wilson Freitas and Marcelo Perlin},
    year = {2023},
    note = {R package version 0.0.8},
    url = {https://github.com/ropensci/rb3},
  }

About

A bunch of downloaders and parsers for data delivered from B3

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages

  • R 99.7%
  • Dockerfile 0.3%