Reputation: 1
How can I work with multiple datasets (xts format) from Yahoo Finance?
tickers <- c('^GSPC','JSE.JO')
getSymbols(tickers, from = "2008-01-01", to = "2011-12-31")
I now have to apply technical analysis techniques to each dataset? I started with two stocks, but want to expand the program to 100 stocks in the end. I'm looking for a simple "for" loop to apply to each dataset.
Thank you! :)
Upvotes: 0
Views: 186
Reputation: 23608
For batch symbols you can use the package BatchGetSymbols in addition to quantmod or you can use the tidyquant package. See examples below.
BatchGetSymbols will pull all the data and create a list with entries. 1 which contains an overview of stocks retrieved and if it was successful, and 1 data.frame with all the data.
tickers <- c('^GSPC','JSE.JO')
library(BatchGetSymbols)
ticker_list <- ticker_list <- BatchGetSymbols(tickers, first.date = "2008-01-01", last.date = "2011-12-31")
Or package tidyquant to get the data in a tidy format. This will also return a message if there is data that can't be retreived.:
library(tidyquant)
ticker_tidy <- tq_get(tickers, from = "2008-01-01", to = "2011-12-31")
Or just quantmod, but then everything will be in list.
tickers_data <- lapply(tickers, getSymbols, from = "2008-01-01", to = "2011-12-31", auto.assign = FALSE)
names(tickers_data) <- tickers
Just choose your preference.
Upvotes: 1