Temporary matching of financial market data downloaded by the Batchgetsymbols package in R

1

By using the following code, I import data from the stock exchange, the Petrobras Ibovespa Index and the US S & P 500 Index. However, the dates of the observations are not exactly matched

# Carregar pacote de importação de dados da bolsa
library(BatchGetSymbols)

# Inputs
# data inicial
first.date <- as.Date("2017-01-01")
#data final
last.date <- Sys.Date()
#frequencia das observações
freq.data <- 'daily'
# Ativos a serem baixados
tickers <- c("^BVSP","PETR4.SA","^GSPC")

#função que baixa ativos
ativos <- BatchGetSymbols(tickers = tickers, 
                         first.date = first.date,
                         last.date = last.date, 
                         freq.data = freq.data,
                         cache.folder = file.path(tempdir(), 
                                                  'BGS_Cache') ) 

Someone would know how to temporarily pause the data, that is, have only date observations with values for the three assets, eliminating from the data those dates where there is observation for only one or two assets.

    
asked by anonymous 09.12.2018 / 21:15

1 answer

0

Using the , one option is to group by date and keep only those observations whose group has more than two members

library(tidyverse)

ativos[[2]] %>% 
  group_by(ref.date) %>% 
  filter(n() > 2)
# A tibble: 1,416 x 10
# Groups:   ref.date [472]
# ...
    
10.12.2018 / 19:10