dynamic-chart-series

quantmod, not able to loop through chartSeries


install.packages("quantmod")
library(quantmod)

company_list <- c("AMD","AMZN","JPM","GOOG","COST")
for (i in 1:length(company_list)){
  symbol <- company_list[i]
  data_in <- as.name(symbol)
  getSymbols(symbol)
  chartSeries(data_in,subset="last 9 months")
  addSMA(10,col="blue")
  addSMA(20,col="red")
}

Error in try.xts(x, error = "chartSeries requires an xtsible object") : chartSeries requires an xtsible object

somehow this code stuck at chartSeries(data_in,subset="last 9 months")


Solution

  • you have to give the xts object to chartSeries, with get(symbol):

    company_list <- c("AMD","AMZN")
    for (i in 1:length(company_list)){
      symbol <- company_list[i]
      getSymbols(symbol)
      chartSeries(get(symbol),subset="last 9 months")
      addSMA(10,col="blue")
      addSMA(20,col="red")
    }