Information regarding prices and yields of bonds issued by the Brazilian government can be downloaded manually as excel files from the Tesouro Direto website. However, it can be painful to aggregate all of this data into something useful as the files don’t have an uniform format and are all divided by year and asset code.
Package GetTDData makes the process of importing data from Tesouro direto much easier. All you need is the name of fixed income assets (LFT, LTN, NTN-C, NTN-B, NTN-B Principal, NTN-F).
Suppose you need financial data (prices and yields) for a bond of type LTN with a maturity date equal to 2016-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:
library(GetTDData)
<- 'LTN' # Identifier of assets
asset.codes <- paste0(tempdir(), '/TD_cache')
cache_folder
<- download.TD.data(asset.codes = asset.codes,
my.flag dl.folder = cache_folder)
## Warning in download.TD.data(asset.codes = asset.codes, dl.folder =
## cache_folder): Folder /tmp/RtmpkhCoLh/TD_cache was not found. Creating a it..
##
## Downloading file /tmp/RtmpkhCoLh/TD_cache/LTN_2005.xls (1-18) Downloading...
## Downloading file /tmp/RtmpkhCoLh/TD_cache/LTN_2006.xls (2-18) Downloading...
## Downloading file /tmp/RtmpkhCoLh/TD_cache/LTN_2007.xls (3-18) Downloading...
## Downloading file /tmp/RtmpkhCoLh/TD_cache/LTN_2008.xls (4-18) Downloading...
## Downloading file /tmp/RtmpkhCoLh/TD_cache/LTN_2009.xls (5-18) Downloading...
## Downloading file /tmp/RtmpkhCoLh/TD_cache/LTN_2010.xls (6-18) Downloading...
## Downloading file /tmp/RtmpkhCoLh/TD_cache/LTN_2011.xls (7-18) Downloading...
## Downloading file /tmp/RtmpkhCoLh/TD_cache/LTN_2012.xls (8-18) Downloading...
## Downloading file /tmp/RtmpkhCoLh/TD_cache/LTN_2013.xls (9-18) Downloading...
## Downloading file /tmp/RtmpkhCoLh/TD_cache/LTN_2014.xls (10-18) Downloading...
## Downloading file /tmp/RtmpkhCoLh/TD_cache/LTN_2015.xls (11-18) Downloading...
## Downloading file /tmp/RtmpkhCoLh/TD_cache/LTN_2016.xls (12-18) Downloading...
## Downloading file /tmp/RtmpkhCoLh/TD_cache/LTN_2017.xls (13-18) Downloading...
## Downloading file /tmp/RtmpkhCoLh/TD_cache/LTN_2018.xls (14-18) Downloading...
## Downloading file /tmp/RtmpkhCoLh/TD_cache/LTN_2019.xls (15-18) Downloading...
## Downloading file /tmp/RtmpkhCoLh/TD_cache/LTN_2020.xls (16-18) Downloading...
## Downloading file /tmp/RtmpkhCoLh/TD_cache/LTN_2021.xls (17-18) Downloading...
## Downloading file /tmp/RtmpkhCoLh/TD_cache/LTN_2022.xls (18-18) Downloading...
<- read.TD.files(dl.folder = cache_folder) my.df
##
## Reading xls data and saving to data.frame
## Reading File = /tmp/RtmpkhCoLh/TD_cache/LTN_2005.xls
## Reading Sheet LTN 040105
## Reading Sheet LTN 010405
## Reading Sheet LTN 010705
## Reading Sheet LTN 011005
## Reading Sheet LTN 010106
## Reading Sheet LTN 010406
## Reading Sheet LTN 010706
## Reading Sheet LTN 011006
## Reading Sheet LTN 010107
## Reading Sheet LTN 010407
## Reading Sheet LTN 010707
## Reading Sheet LTN 010108
## Reading Sheet LTN 010708
## Reading File = /tmp/RtmpkhCoLh/TD_cache/LTN_2006.xls
## Reading Sheet LTN 010406
## Reading Sheet LTN 010706
## Reading Sheet LTN 011006
## Reading Sheet LTN 010107
## Reading Sheet LTN 010407
## Reading Sheet LTN 010707
## Reading Sheet LTN 011007
## Reading Sheet LTN 010108
## Reading Sheet LTN 010408
## Reading Sheet LTN 010708
## Reading Sheet LTN 010109
## Reading File = /tmp/RtmpkhCoLh/TD_cache/LTN_2007.xls
## Reading Sheet LTN 010407
## Reading Sheet LTN 010707
## Reading Sheet LTN 011007
## Reading Sheet LTN 010108
## Reading Sheet LTN 010408
## Reading Sheet LTN 010708
## Reading Sheet LTN 011008
## Reading Sheet LTN 010109
## Reading Sheet LTN 010709
## Reading Sheet LTN 011009
## Reading Sheet LTN 010110
## Reading File = /tmp/RtmpkhCoLh/TD_cache/LTN_2008.xls
## Reading Sheet LTN 010408
## Reading Sheet LTN 010708
## Reading Sheet LTN 011008
## Reading Sheet LTN 010109
## Reading Sheet LTN 010409
## Reading Sheet LTN 010709
## Reading Sheet LTN 011009
## Reading Sheet LTN 010110
## Reading Sheet LTN 010710
## Reading Sheet LTN 010111
## Reading File = /tmp/RtmpkhCoLh/TD_cache/LTN_2009.xls
## Reading Sheet LTN 010409
## Reading Sheet LTN 010709
## Reading Sheet LTN 011009
## Reading Sheet LTN 010110
## Reading Sheet LTN 010710
## Reading Sheet LTN 010111
## Reading Sheet LTN 010112
## Reading File = /tmp/RtmpkhCoLh/TD_cache/LTN_2010.xls
## Reading Sheet LTN 010710
## Reading Sheet LTN 010111
## Reading Sheet LTN 010711
## Reading Sheet LTN 010112
## Reading Sheet LTN 010113
## Reading File = /tmp/RtmpkhCoLh/TD_cache/LTN_2011.xls
## Reading Sheet LTN 010711
## Reading Sheet LTN 010112
## Reading Sheet LTN 010113
## Reading Sheet LTN 010114
## Reading Sheet LTN 010115
## Reading File = /tmp/RtmpkhCoLh/TD_cache/LTN_2012.xls
## Reading Sheet LTN 010113
## Reading Sheet LTN 010114
## Reading Sheet LTN 010115
## Reading Sheet LTN 010116
## Reading File = /tmp/RtmpkhCoLh/TD_cache/LTN_2013.xls
## Reading Sheet LTN 010114
## Reading Sheet LTN 010115
## Reading Sheet LTN 010116
## Reading Sheet LTN 010117
## Reading File = /tmp/RtmpkhCoLh/TD_cache/LTN_2014.xls
## Reading Sheet LTN 010115
## Reading Sheet LTN 010116
## Reading Sheet LTN 010117
## Reading Sheet LTN 010118
## Reading File = /tmp/RtmpkhCoLh/TD_cache/LTN_2015.xls
## Reading Sheet LTN 010116
## Reading Sheet LTN 010117
## Reading Sheet LTN 010118
## Reading Sheet LTN 010121
## Reading File = /tmp/RtmpkhCoLh/TD_cache/LTN_2016.xls
## Reading Sheet LTN 010117
## Reading Sheet LTN 010118
## Reading Sheet LTN 010119
## Reading Sheet LTN 010121
## Reading Sheet LTN 010123
## Reading File = /tmp/RtmpkhCoLh/TD_cache/LTN_2017.xls
## Reading Sheet LTN 010118
## Reading Sheet LTN 010119
## Reading Sheet LTN 010120
## Reading Sheet LTN 010121
## Reading Sheet LTN 010123
## Reading File = /tmp/RtmpkhCoLh/TD_cache/LTN_2018.xls
## Reading Sheet LTN 010119
## Reading Sheet LTN 010120
## Reading Sheet LTN 010121
## Reading Sheet LTN 010123
## Reading Sheet LTN 010125
## Reading File = /tmp/RtmpkhCoLh/TD_cache/LTN_2019.xls
## Reading Sheet LTN 010120
## Reading Sheet LTN 010121
## Reading Sheet LTN 010122
## Reading Sheet LTN 010123
## Reading Sheet LTN 010125
## Reading File = /tmp/RtmpkhCoLh/TD_cache/LTN_2020.xls
## Reading Sheet LTN 010121
## Reading Sheet LTN 010122
## Reading Sheet LTN 010123
## Reading Sheet LTN 010125
## Reading Sheet LTN 010126
## Reading File = /tmp/RtmpkhCoLh/TD_cache/LTN_2021.xls
## Reading Sheet LTN 010122
## Reading Sheet LTN 010123
## Reading Sheet LTN 010724
## Reading Sheet LTN 010125
## Reading Sheet LTN 010126
## Reading File = /tmp/RtmpkhCoLh/TD_cache/LTN_2022.xls
## Reading Sheet LTN 010123
## Reading Sheet LTN 010724
## Reading Sheet LTN 010125
## Reading Sheet LTN 010126
## Reading Sheet LTN 010129
Let’s plot the prices to check if the code worked:
library(ggplot2)
library(dplyr)
##
## Attaching package: 'dplyr'
## The following objects are masked from 'package:stats':
##
## filter, lag
## The following objects are masked from 'package:base':
##
## intersect, setdiff, setequal, union
# filter single LTN
<- my.df %>%
LTN filter(matur.date == as.Date("2020-01-01") )
<- ggplot(data = LTN,
p 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)
As expected, the prices from this bond will converge to its face value, 1000 R$, at the maturity date (2016-01-01). We can also look at its time-varying annual yield:
<- ggplot(data = LTN,
p aes(x = as.Date(ref.date),
y = yield.bid,
color = asset.code)) +
geom_line(size = 1) +
scale_x_date() +
labs(title = '',
x = 'Dates')
print(p)
By default, GetTDData
downloads all available data from
the website. Suppose we wanted to look at the prices of all available
maturities for bonds of type LTN. Since the data is already downloaded,
all we need to do is to plot the prices and yields of the different debt
contracts:
# plot data (prices)
<- ggplot(data = my.df,
p aes(x = as.Date(ref.date), y = price.bid, color = asset.code)) +
geom_line() +
scale_x_date() +
labs(title = '', x = 'Dates', y = 'Prices' )
print(p)
# plot data (yields)
<- ggplot(data = my.df,
p aes(x = as.Date(ref.date), y = yield.bid, color = asset.code)) +
geom_line() +
scale_x_date() +
labs(title = '', x = 'Dates', y = 'Yields' )
print(p)