Search code examples
rquantmod

Pass vector of dates to quantmod


I have the following vectors with tickers, start and end dates for which I want to download stock data with quantmod.

stock = c("MSFT", "WMT", "APPL")
start = c("2015-08-26", "2013-11-12","2015-11-08")
end = c("2015-09-26", "2013-12-12","2015-12-08")

I thought the following would work, but it only retrieves prices for the 3 stocks between 2015-08-26 and 2015-09-26.

library(quantmod)
stockData <- new.env()    
getSymbols(stock, env = stockData, src = "yahoo", from = start, to = end, verbose = T)

Is it not possible to pass vectors as dates to this function? Any elegant solutions are appreciated :)


Solution

  • Yahoo doesn't recognize APPL so probably this should be changed to AAPL in stock. After that you can create a character matrix from the stock, start, and end vectors and then call getSymbols for each row of the matrix. Code would look like:

    mat <- cbind(stock, start, end)
    apply(mat, 1, function(x) getSymbols(Symbols=x["stock"], env=stockData2,
                                         src="yahoo", from=x["start"], to=x["end"], verbose=T))
    attach(stockData2)