I am currently attempting to run a for loop on about 500 stock tickers and attempting to create a chart for all of them. I have succeeded in doing this but my for loop breaks on this error Error in runSum(x, n) : n = 20 is outside valid range: [1, 5]
I got the stock tickers from BatchGetSymbols library.
Here is my script at the bottom is my for loop:
library(stocks)
library(BatchGetSymbols)
library(quantmod)
datalist <- list()
stocks <- c('MMM','ABT','ABBV','ABMD','ACN','ATVI','ADBE','AMD','AAP','AES','AFL','A',
'APD','AKAM','ALK','ALB','ARE','ALXN',
'ALGN','ALLE','AGN','ADS','LNT','ALL','GOOGL','GOOG','MO','AMZN','AMCR',
'AEE','AAL','AEP','AXP','AIG','T','AMT','AWK','AMP','ABC','AME','AMGN','APH','ADI','ANSS',
'ANTM','AON','AOS','APA','AIV','AAPL','AMAT','APTV','ADM','ARNC','ANET','AJG','AIZ','ATO','ADSK','ADP',
'AZO','AVB','AVY','BKR','BLL','BAC','BK','BAX','BDX','BRK.B',
'BBY','BIIB','BLK','BA','BKNG','BWA','BXP','BSX','BMY','AVGO',
'BR','BF.B','CHRW','COG','CDNS','CPB','COF','CPRI','CAH','KMX','CCL',
'CAT','CBOE','CBRE','CDW','CE','CNC','CNP','CTL', 'CERN','CF','SCHW','CHTR','CVX','CMG','CB',
'CHD','CI','CINF','CTAS','CSCO','C','CFG','CTXS','CLX','CME','CMS',
'KO','CTSH','CL','CMCSA','CMA','CAG','CXO','COP','ED','STZ','COO','CPRT','GLW',
'CTVA','COST','COTY','CCI','CSX','CMI', 'CVS','DHI','DHR','DRI','DVA','DE','DAL',
'XRAY','DVN','FANG','DLR','DFS','DISCA','DISCK',
'DISH','DG','DLTR','D','DOV','DOW','DTE','DUK','DRE','DD','DXC','ETFC',
'EMN','ETN','EBAY','ECL','EIX','EW','EA','EMR','ETR','EOG','EFX','EQIX',
'EQR','ESS','EL','EVRG','ES','RE','EXC','EXPE','EXPD','EXR',
'XOM','FFIV','FB','FAST','FRT','FDX','FIS',
'FITB','FE','FRC','FISV','FLT','FLIR','FLS','FMC', 'F','FTNT','FTV','FBHS','FOXA','FOX','BEN',
'FCX','GPS','GRMN','IT','GD','GE','GIS','GM',
'GPC','GILD','GL','GPN','GS','GWW','HRB','HAL','HBI','HOG','HIG',
'HAS', 'HCA','PEAK','HP','HSIC','HSY','HES','HPE','HLT', 'HFC','HOLX','HD',
'HON', 'HRL','HST', 'HWM','HPQ','HUM', 'HII', 'IEX',
'IDXX', 'INFO','ITW','ILMN','INCY', 'IR','INTC',
'ICE','IBM','IP','IPG','IFF','INTU','ISRG','IVZ','IPGP',
'IQV', 'IRM', 'JKHY','J','JBHT', 'SJM', 'JNJ', 'JCI','JPM','JNPR','KSU','K','KEY', 'KEYS',
'KMB', 'KIM','KMI', 'KLAC','KSS','KHC','KR',
'LB', 'LHX', 'LH', 'LRCX','LW', 'LVS','LEG', 'LDOS', 'LEN','LLY','LNC',
'LIN','LYV','LKQ','LMT','L','LOW','LYB','MTB','M','MRO','MPC','MKTX','MAR','MMC','MLM','MAS','MA',
'MKC','MXIM', 'MCD','MCK','MDT','MRK','MET', 'MTD','MGM','MCHP','MU','MSFT', 'MAA',
'MHK', 'TAP','MDLZ','MNST','MCO','MS', 'MOS','MSI','MSCI',
'MYL','NDAQ', 'NOV','NTAP','NFLX','NWL','NEM','NWSA','NWS','NEE','NLSN','NKE',
'NI','NBL','JWN','NSC','NTRS','NOC','NLOK','NCLH','NRG','NUE','NVDA','NVR', 'ORLY', 'OXY',
'ODFL', 'OMC','OKE','ORCL','PCAR','PKG','PH','PAYX','PAYC','PYPL','PNR', 'PBCT','PEP','PKI','PRGO',
'PFE','PM', 'PSX', 'PNW','PXD', 'PNC','PPG','PPL','PFG',
'PG','PGR','PLD','PRU','PEG','PSA','PHM','PVH','QRVO','PWR','QCOM',
'DGX','RL','RJF', 'RTX','O','REG','REGN','RF','RSG', 'RMD',
'RHI','ROK','ROL','ROP','ROST','RCL','SPGI','CRM','SBAC','SLB','STX',
'SEE', 'SRE','NOW','SHW','SPG','SWKS', 'SLG','SNA','SO','LUV', 'SWK','SBUX',
'STT', 'STE', 'SYK','SIVB', 'SYF','SNPS','SYY','TMUS','TROW','TTWO','TPR','TGT','TEL',
'FTI', 'TFX','TXN','TXT','TMO','TIF','TJX','TSCO','TT',
'TDG','TRV','TFC', 'TWTR','TSN','UDR','ULTA', 'USB', 'UAA','UA',
'UNP','UAL','UNH', 'UPS','URI','UHS','UNM','VFC','VLO','VAR', 'VTR',
'VRSN', 'VRSK','VZ', 'VRTX','VIAC', 'V', 'VNO','VMC',
'WRB','WAB','WMT','WBA', 'DIS','WM','WAT','WEC','WFC','WELL','WDC','WU','WRK','WY','WHR',
'WMB','WLTW','WYNN', 'XEL', 'XRX','XLNX', 'XYL','YUM', 'ZBRA','ZBH','ZION', 'ZTS'
)
stockEnv <- new.env()
View(stockEnv)
symbols <- getSymbols(stocks, src='yahoo', env=stockEnv)
for (stocks in ls(stockEnv)){
chartSeries(stockEnv[[stocks]], theme="white", name=stocks,
TA="addVo();addBBands();addCCI();
addSMA(5, col='red');addMACD();addRSI();addROC()", subset='last 30 days')
}
After downloading the data, I get the following warning message:
Warning message:
HWM contains missing values. Some functions will not work if objects contain missing values in the middle of the series. Consider using na.omit(), na.approx(), na.fill(), etc to remove or replace them.
This is likely the source of that error message. Let us have a look at HWM
head(stockEnv[[stocks]])
HWM.Open HWM.High HWM.Low HWM.Close HWM.Volume HWM.Adjusted
2019-10-03 24.17 24.28 23.74 24.25 2828500 24.25
2019-10-04 24.21 24.50 23.96 24.49 2250500 24.49
2019-10-07 NA NA NA NA NA NA
2019-10-08 NA NA NA NA NA NA
2019-10-09 NA NA NA NA NA NA
2019-10-10 NA NA NA NA NA NA
tail(stockEnv[[stocks]])
HWM.Open HWM.High HWM.Low HWM.Close HWM.Volume HWM.Adjusted
2020-03-27 NA NA NA NA NA NA
2020-03-30 NA NA NA NA NA NA
2020-03-31 NA NA NA NA NA NA
2020-04-01 15.40 15.40 12.71 13.20 2531000 13.20
2020-04-02 12.97 13.71 12.00 12.50 4431900 12.50
2020-04-03 12.10 12.69 11.85 12.54 4053100 12.54
So HMW has NAs in the middle of the series, as per the warning message but is it the reason why the for loop fails? The for loop will tell you the culprit that caused the loop to stop, just check the contents of the stocks
variable.
stocks
[1] "HWM"
Yep, HWM is the cause of this. The question is why does this fail? Looking into the chartseries()
source code, the first two lines with checking if it is an xts object and removing NAs:
x <- try.xts(x, error = "chartSeries requires an xtsible object")
x <- na.omit(x)
Calling na.omit
on stockEnv[[stocks]]
gives:
na.omit(stockEnv[[stocks]])
HWM.Open HWM.High HWM.Low HWM.Close HWM.Volume HWM.Adjusted
2019-10-03 24.17 24.28 23.74 24.25 2828500 24.25
2019-10-04 24.21 24.50 23.96 24.49 2250500 24.49
2020-04-01 15.40 15.40 12.71 13.20 2531000 13.20
2020-04-02 12.97 13.71 12.00 12.50 4431900 12.50
2020-04-03 12.10 12.69 11.85 12.54 4053100 12.54
Five rows of data, which matches the five from x
in the error message:
Error in runSum(x, n) : n = 20 is outside valid range: [1, 5]
Looking at the list of functions you pass to the TA
argument, addBBands
has an argument n
, with a default value of 20. I am not sure where exactly addBBands()
calls runSum()
but I am assuming it uses the value of n
, which is 20 to pass to the n
argument in runSum()
. The value of n
being larger than the number of rows of data then causes that error.
How to resolve this? It depends on how you want to treat those NAs. My suggestion would be to exclude this share entirely. You might prefer to replace the NAs with zeroes, an action which I would not recommend since a zero means the share traded on that day, where as with NA, you have no idea what happened. Data is too small to approximate or interpolate.
I hope this helps.