library(BatchGetSymbols)

indexes <- c('^BVSP', '^GSPC', '^FTSE', '^N225')

df_indices <- BatchGetSymbols(tickers = indexes, 
                              first.date = '2010-01-01',
                              last.date = Sys.Date())[[2]]

tab <- tibble()
for (index in indexes) {

  temp_df <- df_indices %>%
    filter(ticker == index)

  avg_ret <- mean(temp_df$ret.adjusted.prices, 
                  na.rm = TRUE)
  max_ret <- max(temp_df$ret.adjusted.prices, 
                 na.rm = TRUE)
  min_ret <- min(temp_df$ret.adjusted.prices, 
                 na.rm = TRUE)

  # save result
  tab <- bind_rows(tab, tibble(index = index,
                               mean_ret = avg_ret, 
                               max_ret = max_ret, 
                               min_ret = min_ret))

}

print(tab)
# none
my_answers <- rep(NA, 5)

Question

Use o pacote BatchGetSymbols para baixar dados do índice SP500 ('^GSPC'), Ibovespa ('^BVSP'), FTSE ('^FSTE') e Nikkei 225 ('^N225') de '2010-01-01' até a data atual. Com os dados importados, use um loop para calcular o retorno médio, máximo e mínimo de cada índice durante o período analisado. Salve todos os resultados em uma tabela única e a mostre no prompt do R.

Solution


Meta-information

extype: string exsolution: r mchoice2string(c(TRUE, FALSE, FALSE, FALSE, FALSE), single = TRUE) exname: "function 05" exshuffle: TRUE



msperlin/adfeR documentation built on March 26, 2021, 3:05 a.m.