Downloading Data from Tesouro Direto

Marcelo Perlin

2016-05-24

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.

The 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) and the desired dates of maturity (e.g. 2016-01-01).

How to use GetTDData

Suppose you need financial data (prices and yields) for a bond of type LTN with a maturity date equal to 2016-01-01. This debt constract is the most basic bond that the Brazilian government issues. It does not pay any value during its lifetime and will pay 1000R$ at its maturity date.

In order to get the data, all you need to do is to run the following code in R:

library(GetTDData)

asset.codes <- 'LTN'   # Identifier of assets 
maturity <- '010116'  # Maturity date as string (ddmmyy)

my.flag <- download.TD.data(asset.codes = asset.codes)
## Warning in download.TD.data(asset.codes = asset.codes): Folder TD Files was
## not found. Creating a it..
## 
## Downloading html page (attempt = 1|10)
## Downloading file TD Files/LTN_2016.xls (1-15) Downloading...
## Downloading file TD Files/LTN_2015.xls (2-15) Downloading...
## Downloading file TD Files/LTN_2014.xls (3-15) Downloading...
## Downloading file TD Files/LTN_2013.xls (4-15) Downloading...
## Downloading file TD Files/LTN_2012.xls (5-15) Downloading...
## Downloading file TD Files/LTN_2011.xls (6-15) Downloading...
## Downloading file TD Files/LTN_2010.xls (7-15) Downloading...
## Downloading file TD Files/LTN_2009.xls (8-15) Downloading...
## Downloading file TD Files/LTN_2008.xls (9-15) Downloading...
## Downloading file TD Files/LTN_2007.xls (10-15) Downloading...
## Downloading file TD Files/LTN_2006.xls (11-15) Downloading...
## Downloading file TD Files/LTN_2005.xls (12-15) Downloading...
## Downloading file TD Files/LTN_2004.xls (13-15) Downloading...
## Downloading file TD Files/LTN_2003.xls (14-15) Downloading...
## Downloading file TD Files/LTN_2002.xls (15-15) Downloading...
my.df <- read.TD.files(asset.codes = asset.codes,
                       maturity = maturity)
## 
## Reading xls data and saving to data.frame
##  Reading File = TD Files/LTN_2002.xls
##     Cant find maturities in file  TD Files/LTN_2002.xls
##  Reading File = TD Files/LTN_2003.xls
##     Cant find maturities in file  TD Files/LTN_2003.xls
##  Reading File = TD Files/LTN_2004.xls
##     Cant find maturities in file  TD Files/LTN_2004.xls
##  Reading File = TD Files/LTN_2005.xls
##     Cant find maturities in file  TD Files/LTN_2005.xls
##  Reading File = TD Files/LTN_2006.xls
##     Cant find maturities in file  TD Files/LTN_2006.xls
##  Reading File = TD Files/LTN_2007.xls
##     Cant find maturities in file  TD Files/LTN_2007.xls
##  Reading File = TD Files/LTN_2008.xls
##     Cant find maturities in file  TD Files/LTN_2008.xls
##  Reading File = TD Files/LTN_2009.xls
##     Cant find maturities in file  TD Files/LTN_2009.xls
##  Reading File = TD Files/LTN_2010.xls
##     Cant find maturities in file  TD Files/LTN_2010.xls
##  Reading File = TD Files/LTN_2011.xls
##     Cant find maturities in file  TD Files/LTN_2011.xls
##  Reading File = TD Files/LTN_2012.xls
##     Reading Sheet LTN 010116
##  Reading File = TD Files/LTN_2013.xls
##     Reading Sheet LTN 010116
##  Reading File = TD Files/LTN_2014.xls
##     Reading Sheet LTN 010116
##  Reading File = TD Files/LTN_2015.xls
##     Reading Sheet LTN 010116
##  Reading File = TD Files/LTN_2016.xls
##     Cant find maturities in file  TD Files/LTN_2016.xls

Let’s plot the prices to check if the code worked well.

library(ggplot2)

p <- ggplot(data = my.df, aes(x = as.Date(ref.date), y = price.bid, color = asset.code))
p <- p + 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:

p <- ggplot(data = my.df, aes(x = as.Date(ref.date), y = yield.bid, color = asset.code))
p <- p + geom_line(size = 1) + scale_x_date()+ labs(title = '', x = 'Dates' )
print(p)

The functions in GetTDData also works with multiple arguments as asset.codes and maturity. Suppose we wanted to look at the prices of all available maturities for bonds of type LTN. All we need to do is to add the value NULL to the argument maturity:

library(GetTDData)
library(ggplot2)

asset.codes <- 'LTN'   # Name of asset
maturity <- NULL      # = NULL, downloads all maturities

# download data
my.flag <- download.TD.data(asset.codes = asset.codes, 
                            do.clean.up = F)
## 
## Downloading html page (attempt = 1|10)
## Downloading file TD Files/LTN_2016.xls (1-15) Downloading...
## Downloading file TD Files/LTN_2015.xls (2-15) Found file in folder, skipping it.
## Downloading file TD Files/LTN_2014.xls (3-15) Found file in folder, skipping it.
## Downloading file TD Files/LTN_2013.xls (4-15) Found file in folder, skipping it.
## Downloading file TD Files/LTN_2012.xls (5-15) Found file in folder, skipping it.
## Downloading file TD Files/LTN_2011.xls (6-15) Found file in folder, skipping it.
## Downloading file TD Files/LTN_2010.xls (7-15) Found file in folder, skipping it.
## Downloading file TD Files/LTN_2009.xls (8-15) Found file in folder, skipping it.
## Downloading file TD Files/LTN_2008.xls (9-15) Found file in folder, skipping it.
## Downloading file TD Files/LTN_2007.xls (10-15) Found file in folder, skipping it.
## Downloading file TD Files/LTN_2006.xls (11-15) Found file in folder, skipping it.
## Downloading file TD Files/LTN_2005.xls (12-15) Found file in folder, skipping it.
## Downloading file TD Files/LTN_2004.xls (13-15) Found file in folder, skipping it.
## Downloading file TD Files/LTN_2003.xls (14-15) Found file in folder, skipping it.
## Downloading file TD Files/LTN_2002.xls (15-15) Found file in folder, skipping it.
# reads data
my.df <- read.TD.files(asset.codes = asset.codes, 
                       maturity = maturity)
## 
## Reading xls data and saving to data.frame
##  Reading File = TD Files/LTN_2002.xls
##     Reading Sheet LTN 070104
##  Reading File = TD Files/LTN_2003.xls
##     Reading Sheet LTN 011003
##     Reading Sheet LTN 070104
##     Reading Sheet LTN 010404
##     Reading Sheet LTN 010704
##     Reading Sheet LTN 011004
##     Reading Sheet LTN 040105
##     Reading Sheet LTN 010705
##  Reading File = TD Files/LTN_2004.xls
##     Reading Sheet LTN 070104
##     Reading Sheet LTN 010404
##     Reading Sheet LTN 010704
##     Reading Sheet LTN 011004
##     Reading Sheet LTN 040105
##     Reading Sheet LTN 010405
##     Reading Sheet LTN 010705
##     Reading Sheet LTN 011005
##     Reading Sheet LTN 010106
##     Reading Sheet LTN 010706
##  Reading File = TD Files/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 = TD Files/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 = TD Files/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 = TD Files/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 = TD Files/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 = TD Files/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 = TD Files/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 = TD Files/LTN_2012.xls
##     Reading Sheet LTN 010113
##     Reading Sheet LTN 010114
##     Reading Sheet LTN 010115
##     Reading Sheet LTN 010116
##  Reading File = TD Files/LTN_2013.xls
##     Reading Sheet LTN 010114
##     Reading Sheet LTN 010115
##     Reading Sheet LTN 010116
##     Reading Sheet LTN 010117
##  Reading File = TD Files/LTN_2014.xls
##     Reading Sheet LTN 010115
##     Reading Sheet LTN 010116
##     Reading Sheet LTN 010117
##     Reading Sheet LTN 010118
##  Reading File = TD Files/LTN_2015.xls
##     Reading Sheet LTN 010116
##     Reading Sheet LTN 010117
##     Reading Sheet LTN 010118
##     Reading Sheet LTN 010121
##  Reading File = TD Files/LTN_2016.xls
##     Reading Sheet LTN 010117
##     Reading Sheet LTN 010118
##     Reading Sheet LTN 010119
##     Reading Sheet LTN 010121
##     Reading Sheet LTN 010123
# plot data (prices)
p <- ggplot(data = my.df, aes(x = as.Date(ref.date), y = price.bid, color = asset.code))
p <- p + geom_line() + scale_x_date() + labs(title = '', x = 'Dates', y = 'Prices' )
print(p)

# plot data (yields)
p <- ggplot(data = my.df, aes(x = as.Date(ref.date), y = yield.bid, color = asset.code))
p <- p + geom_line() + scale_x_date() + labs(title = '', x = 'Dates', y = 'Yields' )
print(p)