Temporal matching of financial market data downloaded by Batchgetsymbols package in R

Asked

Viewed 57 times

1

Through the following code, I import the stock exchange data the Ibovespa Index, Petrobras and the American Index S&P 500. However, the dates of the observations are not exactly matching

# 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 to inform me a way to temporarily pair the data, that is, to have only observations of dates with values for the three assets, eliminating from the data those dates where there is observation for only one or two assets.

1 answer

1


Using the , an 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]
# ...

Browser other questions tagged

You are not signed in. Login or sign up in order to post.