Search code examples
rquantmod

addOBV throwing error


I am trying to plot a graph with price and a few technical indicators such as ADX, RSI, and OBV. I cannot figure out why addOBV is giving an error and why addADX not showing at all in the graph lines in the chart?

Here my code:

tmp <- read.csv(paste("ProcessedQuotes/",Nifty[x,],".csv", sep=""),
  as.is=TRUE, header=TRUE, row.names=NULL)
tmp$Date<-as.Date(tmp$Date)
ydat = xts(tmp[,-1],tmp$Date) 
lineChart(ydat, TA=NULL, name=paste(Nifty[x,]," Technical Graph"))
plot(addSMA(10))
plot(addEMA(10))
plot(addRSI())
plot(addADX())
plot(addOBV())

Error for addOBV is:

Error in try.xts(c(2038282, 1181844, -1114409, 1387404, 3522045, 4951254,  : 
  Error in as.xts.double(x, ..., .RECLASS = TRUE) :   
        order.by must be either 'names()' or otherwise specified

Below you can see DIn is not shown fully in the graphs.

enter image description here

> class(ydat)
[1] "xts" "zoo"
> head(ydat)
  Open  High    Low  Close  Volume Trades Sma20 Sma50 DIp DIn DX ADX aroonUp aroonDn oscillator  macd signal RSI14

Solution

  • I don't know why that patch doesn't work for you, but you can just create a new function (or you could mask the one from quantmod). Let's just make a new, patched version called addOBV2 which is the code for addOBV except for the one patched line. (x <- as.matrix(lchob@xdata) is replaced with x <- try.xts(lchob@xdata, error=FALSE)).

    addOBV2 <- function (..., on = NA, legend = "auto") 
    {
      stopifnot("package:TTR" %in% search() || require("TTR", quietly = TRUE))
      lchob <- quantmod:::get.current.chob()
      x <- try.xts(lchob@xdata, error=FALSE)
      #x <- as.matrix(lchob@xdata)
      x <- OBV(price = Cl(x), volume = Vo(x))
      yrange <- NULL
      chobTA <- new("chobTA")
      if (NCOL(x) == 1) {
        chobTA@TA.values <- x[lchob@xsubset]
      }
      else chobTA@TA.values <- x[lchob@xsubset, ]
      chobTA@name <- "chartTA"
      if (any(is.na(on))) {
        chobTA@new <- TRUE
      }
      else {
        chobTA@new <- FALSE
        chobTA@on <- on
      }
      chobTA@call <- match.call()
      legend.name <- gsub("^.*[(]", " On Balance Volume (", deparse(match.call()))#, 
      #extended = TRUE)
      gpars <- c(list(...), list(col=4))[unique(names(c(list(col=4), list(...))))]
      chobTA@params <- list(xrange = lchob@xrange, yrange = yrange, 
                            colors = lchob@colors, color.vol = lchob@color.vol, multi.col = lchob@multi.col, 
                            spacing = lchob@spacing, width = lchob@width, bp = lchob@bp, 
                            x.labels = lchob@x.labels, time.scale = lchob@time.scale, 
                            isLogical = is.logical(x), legend = legend, legend.name = legend.name, 
                            pars = list(gpars))
      if (is.null(sys.call(-1))) {
        TA <- lchob@passed.args$TA
        lchob@passed.args$TA <- c(TA, chobTA)
        lchob@windows <- lchob@windows + ifelse(chobTA@new, 1, 
                                                0)
        chartSeries.chob <- quantmod:::chartSeries.chob
        do.call("chartSeries.chob", list(lchob))
        invisible(chobTA)
      }
      else {
        return(chobTA)
      }
    }
    

    Now it works.

    # reproduce your data
    ydat <- getSymbols("ZEEL.NS", src="yahoo", from="2012-09-11", 
                       to="2013-01-18", auto.assign=FALSE)
    
    lineChart(ydat, TA=NULL, name=paste("ZEEL Technical Graph"))
    plot(addSMA(10))
    plot(addEMA(10))
    plot(addRSI())
    plot(addADX())
    plot(addOBV2())
    

    enter image description here