简体   繁体   中英

Having trouble fixing error using quantmod package in R : Error in inherits(x, “xts”): object 'M' not found calls

below is my code. What I am trying to do is download multiple symbols of stocks and plot charts, but I am getting this error:

Error in inherits(x, "xts"): object 'M' not found calls: ... eval-> chartSeries -> try.xts ->is.xts -> inherits

library(quantmod)
stocks <- c("INFY.NS","HINDALCO.NS","TCS.NS","TATASTEEL.NS","NESTLEIND.NS",
  "TECHM.NS","HCLTECH.NS","HDFCBANK.NS","JETAIRWAYS.NS","BLUEDART.NS",
  "AUROPHARMA.NS","APOLLOHOSP.NS", "IOC.NS", "HINDPETRO.NS","BHEL.NS",
  "NTPC.NS", "HINDZINC.NS", "M&M.NS", "RELIANCE.NS","ONGC.NS", "ORIENTBANK.NS",
  "MARUTI.NS", "ASHOKLEY.NS", "ASIANPAINT.NS", "DABUR.NS", "DRREDDY.NS",
  "BHARTIARTL.NS", "BATAINDIA.NS", "LTI.NS")
getSymbols(stocks,from="2017-01-01")
chartSeries(INFY.NS)  
chartSeries(HINDALCO.NS)
chartSeries(TCS.NS)
chartSeries(TATASTEEL.NS)
chartSeries(NESTLEIND.NS)
chartSeries(TECHM.NS)
chartSeries(HCLTECH.NS)
chartSeries(HDFCBANK.NS)
chartSeries(JETAIRWAYS.NS)
chartSeries(BLUEDART.NS)
chartSeries(AUROPHARMA.NS)
chartSeries(APOLLOHOSP.NS)
chartSeries(IOC.NS) 
chartSeries(HINDPETRO.NS) 
chartSeries(BHEL.NS) 
chartSeries(NTPC.NS)
chartSeries(HINDZINC.NS)
chartSeries(M&M.NS)
chartSeries(RELIANCE.NS)
chartSeries(ONGC.NS)
chartSeries(ORIENTBANK.NS)
chartSeries(MARUTI.NS)
chartSeries(ASHOKLEY.NS)
chartSeries(ASIANPAINT.NS)
chartSeries(DABUR.NS)
chartSeries(DRREDDY.NS)
chartSeries(BHARTIARTL.NS)
chartSeries(BATAINDIA.NS)
chartSeries(LTI.NS)

The problem is that M&M.NS is not a syntactically valid R name (the & has special meaning).

library(quantmod)
getSymbols("M&M.NS")
chartSeries(M&M.NS)    # error
chartSeries(`M&M.NS`)  # use backticks for names that aren't syntactically valid

Or you can use setSymbolLookup() to map the symbol to a syntactically valid name. Then you can use the syntactically valid ticker in your getSymbols() and chartSeries() calls.

setSymbolLookup(M.AMP.M.NS = list(name = "M&M.NS"))
getSymbols("M.AMP.M.NS")
chartSeries(M.AMP.M.NS)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM