New R package: GetCVMData

2020-07-18: Package GetCVMData is now renamed GetDFPData2. See this post for details..

Package GetCVMData is an alternative to GetDFPData. Both have the same objective: fetch corporate data of Brazilian companies trading at B3, but diverge in their source. While GetDFPData imports data directly from the DFP and FRE systems, GetCVMData uses the CVM ftp site for grabbing compiled .csv files.

When doing large scale importations, GetDFPData fells sluggish due to the parsing of large xml files. As an example, building the dataset available in my data page takes around six hours of execution using 10 cores of my home computer.

GetCVMData is lean and fast. Since the data is already parsed in csv files, all the code does is organize the files, download and read. For comparison, all DFP documents, annual financial reports, available in CVM can be imported in less than 1 minute. Additionally, GetCVMData can also parse ITR (quarterly) data, which was not available in GetDFPData.

However, be aware that the output data is not the same. I kept all original column names from CVM and some information, such as tickers, are not available in GetCVMData.

Here’s an example of usage:

if (!require(devtools)) install.packages('devtools')  
if (!require(GetCVMData)) devtools::install_github('msperlin/GetCVMData') # not in CRAN yet

library(GetCVMData)
library(tidyverse)

# fetch information about companies
df_info <- get_info_companies()


# search for companies
df_search <- search_company('odontoprev')

# DFP annual data
id_cvm <- df_search$CD_CVM[1] # use NULL for all companies
df_dfp <- get_dfp_data(companies_cvm_codes = id_cvm, 
                       first_year = 2015,
                       last_year = 2019,
                       type_docs = c('DRE', 'BPA', 'BPP'), # income, assets, liabilities
                       type_format = 'con' # consolidated statements
                       )

glimpse(df_dfp)

# ITR quarterly data
df_itr <- get_itr_data(companies_cvm_codes = id_cvm, 
                       first_year = 2010,
                       last_year = 2020, 
                       type_docs = c('DRE', 'BPA', 'BPP'), # income, assets, liabilities
                       type_format = 'con' # consolidated statements
                       )

glimpse(df_itr)

# FRE data (not yet implemented..)
#df_fre <- get_fre_data()
## Rows: 897
## Columns: 16
## $ CNPJ_CIA     <chr> "58.119.199/0001-51", "58.119.199/0001-51", "58.119.199/…
## $ CD_CVM       <dbl> 20125, 20125, 20125, 20125, 20125, 20125, 20125, 20125, …
## $ DT_REFER     <date> 2015-12-31, 2015-12-31, 2015-12-31, 2015-12-31, 2015-12…
## $ DT_INI_EXERC <date> 2015-01-01, 2015-01-01, 2015-01-01, 2015-01-01, 2015-01…
## $ DT_FIM_EXERC <date> 2015-12-31, 2015-12-31, 2015-12-31, 2015-12-31, 2015-12…
## $ DENOM_CIA    <chr> "ODONTOPREV S.A.", "ODONTOPREV S.A.", "ODONTOPREV S.A.",…
## $ VERSAO       <dbl> 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,…
## $ GRUPO_DFP    <chr> "DF Consolidado - Demonstração do Resultado", "DF Consol…
## $ MOEDA        <chr> "REAL", "REAL", "REAL", "REAL", "REAL", "REAL", "REAL", …
## $ ESCALA_MOEDA <chr> "MILHAR", "MILHAR", "MILHAR", "MILHAR", "MILHAR", "MILHA…
## $ ORDEM_EXERC  <chr> "ÚLTIMO", "ÚLTIMO", "ÚLTIMO", "ÚLTIMO", "ÚLTIMO", "ÚLTIM…
## $ CD_CONTA     <chr> "3.01", "3.02", "3.03", "3.04", "3.04.01", "3.04.02", "3…
## $ DS_CONTA     <chr> "Receita de Venda de Bens e/ou Serviços", "Custo dos Ben…
## $ VL_CONTA     <dbl> 124969100, -58252800, 66716300, -35850300, -12007400, -1…
## $ COLUNA_DF    <lgl> NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, …
## $ source_file  <chr> "dre_cia_aberta_con_2015.csv", "dre_cia_aberta_con_2015.…
## Rows: 4,868
## Columns: 16
## $ CNPJ_CIA     <chr> "58.119.199/0001-51", "58.119.199/0001-51", "58.119.199/…
## $ CD_CVM       <dbl> 20125, 20125, 20125, 20125, 20125, 20125, 20125, 20125, …
## $ DT_REFER     <date> 2011-03-31, 2011-03-31, 2011-03-31, 2011-03-31, 2011-03…
## $ DT_INI_EXERC <date> NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA,…
## $ DT_FIM_EXERC <date> 2011-03-31, 2011-03-31, 2011-03-31, 2011-03-31, 2011-03…
## $ DENOM_CIA    <chr> "ODONTOPREV S.A.", "ODONTOPREV S.A.", "ODONTOPREV S.A.",…
## $ VERSAO       <dbl> 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,…
## $ GRUPO_DFP    <chr> "DF Consolidado - Balanço Patrimonial Ativo", "DF Consol…
## $ MOEDA        <chr> "REAL", "REAL", "REAL", "REAL", "REAL", "REAL", "REAL", …
## $ ESCALA_MOEDA <chr> "MIL", "MIL", "MIL", "MIL", "MIL", "MIL", "MIL", "MIL", …
## $ ORDEM_EXERC  <chr> "ÚLTIMO", "ÚLTIMO", "ÚLTIMO", "ÚLTIMO", "ÚLTIMO", "ÚLTIM…
## $ CD_CONTA     <chr> "1", "1.01", "1.01.01", "1.01.02", "1.01.02.01", "1.01.0…
## $ DS_CONTA     <chr> "Ativo Total", "Ativo Circulante", "Caixa e Equivalentes…
## $ VL_CONTA     <dbl> 9.60803e+15, 2.41649e+15, 8.35200e+13, 1.67052e+15, 1.67…
## $ COLUNA_DF    <lgl> NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, …
## $ source_file  <chr> "itr_cia_aberta_bpa_con_2011.csv", "itr_cia_aberta_bpa_c…

Lets plot the quarterly profit of ODONTOPREV S.A.:

library(tidyverse)

quarterly_profits <- df_itr  %>%
  filter(GRUPO_DFP == 'DF Consolidado - Demonstração do Resultado',
         DS_CONTA == 'Lucro/Prejuízo Consolidado do Período') 
  
# plot it
p <- ggplot(quarterly_profits, aes(x = DT_FIM_EXERC, y = VL_CONTA)) + 
  geom_col() + 
  theme_bw() + 
  labs(title = paste0('Quarterly profits of ', quarterly_profits$DENOM_CIA[1]),
       caption = 'Data from CVM',
       x = '',
       y = 'Consolidade Profits')

p

Marcelo S. Perlin
Marcelo S. Perlin
Associate Professor of Finance

My research interests include distributed robotics, mobile computing and programmable matter.

comments powered by Disqus