https://github.com/msperlin/GetTDData
https://github.com/msperlin/GetTDData
Last synced: 10 months ago
JSON representation
- Host: GitHub
- URL: https://github.com/msperlin/GetTDData
- Owner: msperlin
- License: other
- Created: 2017-09-12T19:14:52.000Z (over 8 years ago)
- Default Branch: master
- Last Pushed: 2024-04-11T17:37:32.000Z (almost 2 years ago)
- Last Synced: 2024-05-01T14:25:16.617Z (almost 2 years ago)
- Language: R
- Homepage: https://msperlin.github.io/GetTDData/
- Size: 4.22 MB
- Stars: 20
- Watchers: 4
- Forks: 6
- Open Issues: 2
-
Metadata Files:
- Readme: README.Rmd
- Changelog: NEWS.md
- License: LICENSE
- Codemeta: codemeta.json
Awesome Lists containing this project
- awesome-quant - GetTDData - Downloads and aggregates data for Brazilian government issued bonds directly from the website of Tesouro Direto. (R / Data Sources)
README
---
output: github_document
---
```{r, include = FALSE}
knitr::opts_chunk$set(
collapse = TRUE,
comment = "#>",
fig.path = "man/figures/README-",
out.width = "100%"
)
```
[](https://app.codecov.io/gh/msperlin/GetTDData?branch=master)
[](https://www.repostatus.org/#active)
[](https://github.com/msperlin/GetTDData/actions)
# Package `GetTDData`
Information regarding prices and yields of bonds issued by the Brazilian government can be downloaded manually as excel files from the [Tesouro Direto website](https://www.tesourodireto.com.br/). However, it is painful to aggregate all of this data into something useful as the several files don't have an uniform format.
Package `GetTDData` makes the process of importing data from Tesouro direto much easier. All that you need in order to download the data is the name of the assets (LFT, LTN, NTN-C, NTN-B, NTN-B Principal, NTN-F).
## Installation
```
# from CRAN (stable version)
install.package('GetTDData')
# from github (development version)
devtools::install_github('msperlin/GetTDData')
```
## How to use GetTDData
Suppose you need financial data (prices and yields) for a bond of type LTN with a maturity (end of contract) at 2023-01-01. This bullet bond is the most basic debt contract the Brazilian government issues. It does not pay any value (coupon) during its lifetime and will pay 1000 R$ at maturity.
In order to get the data, all you need to do is to run the following code in R:
```{r example1}
library(GetTDData)
assets <- 'LTN' # Identifier of assets
first_year <- 2020
last_year <- 2022
df_td <- td_get(assets,
first_year,
last_year)
```
Let's plot the prices to check if the code worked:
```{r plot.prices, fig.width=7, fig.height=2.5}
library(ggplot2)
library(dplyr)
# filter LTN
my_asset_code <- "LTN 010123"
LTN <- df_td %>%
filter(asset_code == my_asset_code)
p <- ggplot(data = LTN,
aes(x = as.Date(ref_date),
y = price_bid,
color = asset_code)) +
geom_line(size = 1) + scale_x_date() + labs(title = '', x = 'Dates')
print(p)
```
## Downloading the Brazilian Yield Curve
The latest version of `GetTDData` offers function `get.yield.curve` to download the current Brazilian yield curve directly from Anbima. The yield curve is a tool of financial analysts that show, based on current prices of fixed income instruments, how the market perceives the future real, nominal and inflation returns. You can find more details regarding the use and definition of a yield curve in [Investopedia][https://www.investopedia.com/terms/y/yieldcurve.asp].
```{r, eval=FALSE}
library(GetTDData)
df.yield <- get.yield.curve()
str(df.yield)
```
And we can plot it for the derised result:
```{r, eval=FALSE}
library(ggplot2)
p <- ggplot(df.yield, aes(x=ref.date, y = value) ) +
geom_line(size=1) + geom_point() + facet_grid(~type, scales = 'free') +
labs(title = paste0('The current Brazilian Yield Curve '),
subtitle = paste0('Date: ', df.yield$current.date[1]))
print(p)
```