Search code examples
rchartsquantmodtechnical-indicator

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


I 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?


Solution

  • You can use na.omit and there is no need to convert to an xts-object as this is the default.

    library(quantmod)
    getSymbols("VELO.CO")
    p <- na.omit(VELO.CO)
    newEMA <- function(x) {
      EMA(p[,6], n = 20) - (EMA(p[,6], n = 50))
    }
    
    emaTA <- newTA(newEMA)
    barChart(VELO.CO)
    emaTA(col = "lightgoldenrod3", "Price")