Search code examples
rxtsquantmodindicator

R (quantmod), plot own indicator, Error in seq.default


I want to plot my own simple indicator in quantmod like so:

own.ind <- as.matrix(c(1,2,3), ncol=1, nrow=3)
rownames(own.ind) <- c("2017-01-23", "2017-01-24", "2017-01-25")
own.ind <- as.xts(own.ind)
getSymbols("AAPL", from = as.Date("2017-01-23"), to = as.Date("2017-01-25"))
chartSeries(AAPL)
addTA(own.ind)

But it earned me an error saying

Error in seq.default(min(tav * 0.975, na.rm = TRUE), max(tav * 1.05, na.rm = TRUE),  : 
'from' cannot be NA, NaN or infinite

and two additional warnings:

1: In min(tav * 0.975, na.rm = TRUE) :  no non-missing arguments to min; returning Inf
2: In max(tav * 1.05, na.rm = TRUE) :  no non-missing arguments to max; returning -Inf

What's wrong?


Solution

  • Joe, you have to create a function to add the series as indicator. To create an indicator which adds 1 per trading day as in your example do the following:

    myInd <- function(x) {
      x <- 1:NROW(x)
      return(x)
    }
    

    create the new indicator:

    addMyInd <- newTA(FUN = myInd, legend = "MyInd")
    

    to check class:

    > class(addMyInd)
    [1] “function”
    

    Now let’s chart the new indicator below the price chart:

    getSymbols("AAPL", from = "2017-01-23", to = "2017-01-25")
    chartSeries(AAPL,TA=NULL)
    addMyInd()
    

    enter image description here