Search code examples
rquantmod

Error in getSymbols, must use auto.assign=TRUE for multiple symbol requests


I'm trying to write a program that would take a .csv file of stock symbols and test them against each other for things like cointegration. However, when I run the following code quatnmod gives me something about having to use auto.assign = TRUE for multiple symbol requests.

getprices<-function(sym){
  #get prices from last 7 years
  prices<-getSymbols(sym, from = Sys.Date() - (365*7), auto.assign=FALSE)
  #exract closing prices
  prices<-Cl(prices)

  return(prices)}


symbols1 <- c('TSN', 'MSFT')
symbols2 <- c('AAPL', 'NFLX')

container<-c()
addprices <- function(symbols1, symbols2){
   for (i in symbols1){
    for (g in symbols2){
      i<-getprices(i)
      g<-getprices(g)
      container <- i+g
      }
     }
    return(container)
    }

When I run addprices(symbols1, symbols2) I get this error:

Error in getSymbols(sym, from = Sys.Date() - (365 * 7), auto.assign = FALSE) : 
  must use auto.assign=TRUE for multiple Symbols requests
Calls: addprices -> getprices -> getSymbols

I know when I do this I should get that error, and I believe this is what the error is referring to:

getSymbols(sym, from = Sys.Date() - (365 * 7), auto.assign = FALSE)

However, what I'm doing isn't that, so what gives? Any advice? Is there a work around?

I googled this and there really weren't any relevant questions/answers.


Solution

  • The problem is that you're over-writing the iterator i inside the g for loop. The first iteration of g works fine but i is no longer symbols1[1] in the second iteration... it's the output from getprices(i).