Search code examples
rindicatortradingalgorithmic-trading

Technical Analysis - OBV indicator calculation in R


Here is a few references about OBV calculations:

  1. http://ta.mql4.com/indicators/volumes/on_balance_volume
  2. http://stockcharts.com/school/doku.php?id=chart_school:technical_indicators:on_balance_volume_obv
  3. http://en.wikipedia.org/wiki/On-balance_volume

When I navigate to the source code of OBV function in TTR package, I see:

"OBV" <-
function(price, volume) {
    # On Balance Volume
    price <- try.xts(price, error=as.matrix)
    volume <- try.xts(volume, error=as.matrix)

    if(!(is.xts(price) && is.xts(volume))) {
        price <- as.vector(price)
        volume <- as.vector(volume)
    }
    obv <- c( volume[1], ifelse( ROC(price) > 0, volume, -volume )[-1] )
    obv <- cumsum( obv )
    if(is.xts(obv)) {
        obv <- xts(obv,index(price))
        colnames(obv) <- 'obv'
    }
    reclass( obv, price )
}

I see that the equality case (I mean the case occurs when today’s close is equal to yesterday’s close) in the reference web pages does not exist in OBV function implementation.

Is it a bug or an acceptance of the package? If it is a bug, where can I report the issue?

Thanks,


Solution

  • This looks like a bug, so I've reported it. I should be able to fix it sometime over the next few days.

    In general, you should ask the package maintainer (which happens to be me in this case) because they are more likely to know whether or not something is a bug than the general audience on stackoverflow.