Peter Peter - 3 years ago 351
R Question

addTA - Error in naCheck(x, n) : Series contains non-leading NAs

I am fairly new to R and recently tried to create my own technical indicator, a simple golden cross indicator. 50 - 200 day EMA to be added to my chartSeries chart. This worked fine with the code below at first, but after the updated package of Quantmod was released it gives me this error message:

Code (stock data is downloaded through the getSymbols function in Quantmod)

#20dayEMA - 50dayEMA Technical indicator, Price and Volume
newEMA <- function(x){(removeNA(EMA(p[,6],n=50)-(EMA(p[,6],n=200))))
emaTA <- newTA(newEMA)
emaTA(col='lightgoldenrod3', 'Price')

Then it gives me this error message:

Error in naCheck(x, n) : Series contains non-leading NAs

Does anyone know how to remove these non-leading NAs?

Thanks in advance.

jsb jsb
Answer Source

I'm not familiar with the quantmod package, but I played around with your code and I think I found a working solution:

p <- as.xts(c(VELO.CO))

# remove incomplete cases
vec <- which(!complete.cases(p))  # rows 2305 2398
p2 <- p[-vec, ]

newEMA <- function(x) {
    EMA(p2[, 6], n = 20) - (EMA(p2[, 6], n = 50))

emaTA <- newTA(newEMA)
emaTA(col = "lightgoldenrod3", "Price")
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download