Package {GetTDData}


Title: Get Data for Brazilian Bonds (Tesouro Direto)
Version: 1.7.0
Description: Downloads and aggregates data for Brazilian government issued bonds directly from the website of Tesouro Direto https://www.tesourodireto.com.br/.
Depends: R (≥ 4.1.0)
Imports: stringr, readxl, utils, stats, curl, bizdays, tidyr, rvest, xml2, dplyr, fs, cli, ggplot2, tibble
License: MIT + file LICENSE
BugReports: https://github.com/msperlin/GetTDData/issues/
URL: https://github.com/msperlin/GetTDData/, https://msperlin.github.io/GetTDData/
Encoding: UTF-8
Suggests: knitr, rmarkdown, testthat, covr
Config/roxygen2/version: 8.1.0
NeedsCompilation: no
Packaged: 2026-08-29 11:54:46 UTC; msperlin
Author: Marcelo Perlin [aut, cre]
Maintainer: Marcelo Perlin <marceloperlin@gmail.com>
Repository: CRAN
Date/Publication: 2026-08-29 16:50:02 UTC

Returns metadata and information about Tesouro Direto bond types

Description

Provides metadata for Brazilian government bond types, including their official names, indexers (e.g., IPCA, SELIC, Fixed rate), payment frequency, and description.

Usage

get_asset_info(asset_codes = NULL)

Arguments

asset_codes

Optional character vector of asset codes (e.g. 'LTN', 'NTN-B'). If 'NULL', returns info for all assets.

Value

A tibble with asset metadata.

Examples

get_asset_info()
get_asset_info("LTN")

Returns the cache directory path

Description

Returns the cache directory path

Usage

get_cache_folder(persistent = getOption("GetTDData.persistent_cache", FALSE))

Arguments

persistent

Logical. If 'TRUE', uses a user-persistent directory via 'tools::R_user_dir'. If 'FALSE', defaults to session temporary directory. Can also be set globally via 'options(GetTDData.persistent_cache = TRUE)'. Defaults to 'FALSE'.

Value

A character string representing the folder path.

Examples

get_cache_folder()

Returns the available asset names at the Tesouro Direto (TD) website

Description

Returns the available asset names at the Tesouro Direto (TD) website

Usage

get_td_names(online = FALSE)

Arguments

online

Logical. If 'TRUE', attempts to query the Tesouro Direto CDN server for live asset names. Defaults to 'FALSE'.

Value

A character vector of names.

Examples

get_td_names()

Gets the current yield curve

Description

Downloads and parses information about the current Brazilian yield curve from Anbima.

Usage

get_yield_curve()

get.yield.curve()

Value

A data frame with information about the yield curve.

Examples

## Not run: 
df_yield <- get_yield_curve()
str(df_yield)

## End(Not run)

Plot Tesouro Direto Time Series Data

Description

Plots price or yield time series trajectories for Tesouro Direto assets downloaded using td_get.

Usage

plot_td_series(df_td, type = c("price", "yield"))

Arguments

df_td

A data frame or tibble returned by td_get.

type

Character string indicating what to plot: "price" (price_bid) or "yield" (yield_bid). Defaults to "price".

Value

A ggplot object.

Examples

## Not run: 
df_td <- td_get("LTN", 2020, 2022)
plot_td_series(df_td, type = "price")

## End(Not run)

Plot the ANBIMA Yield Curve

Description

Plots the yield curve data frame downloaded using get_yield_curve.

Usage

plot_yield_curve(df_yc)

Arguments

df_yc

A data frame or tibble returned by get_yield_curve.

Value

A ggplot object.

Examples

## Not run: 
df_yc <- get_yield_curve()
plot_yield_curve(df_yc)

## End(Not run)

Downloads data for Brazilian government bonds directly from the website

Description

This function looks into the Tesouro Direto website (<https://www.tesourodireto.com.br/>) and downloads all files containing prices and yields of government bonds. You can use the input 'asset_codes' to restrict the downloads to specific bonds.

Usage

td_get(
  asset_codes = "LTN",
  first_year = 2005,
  last_year = as.numeric(format(Sys.Date(), "%Y")),
  dl_folder = get_cache_folder()
)

Arguments

asset_codes

A character vector identifying the assets (one or more) in the names of the Excel files (e.g., 'LTN'). If 'NULL', downloads all available assets.

first_year

The first year of data (minimum of 2005).

last_year

The last year of data.

dl_folder

Path of the folder to save Excel files from Tesouro Direto (will create if it does not exist). Defaults to a session-temporary directory. To avoid redownloading files across different R sessions, you can pass a persistent path (e.g., a local folder path, or using tools::R_user_dir("GetTDData", which = "cache")).

Value

A data frame containing the asset data (prices and yields).

Examples

## Not run: 
df_td <- td_get("LTN", 2020, 2022)

## End(Not run)

Returns current TD prices and yields

Description

Fetches current prices and yields of Tesouro Direto (TD) assets by downloading the latest daily data available from the website.

Usage

td_get_current(asset_codes = NULL, dl_folder = get_cache_folder())

Arguments

asset_codes

A character vector identifying the assets (e.g., 'LTN', 'NTN-B'). If 'NULL', returns all available assets.

dl_folder

Path of the folder to save Excel files from Tesouro Direto. Defaults to a session-temporary directory.

Value

A tibble with current asset prices, yields, and maturity dates.

Examples

## Not run: 
df_current <- td_get_current()
head(df_current)

## End(Not run)