quantmod icon indicating copy to clipboard operation
quantmod copied to clipboard

Volume Endpoints Functionality

Open rossb34 opened this issue 6 years ago • 0 comments

I propose adding functionality to create an endpoints vector based on volume. This is similar to xts::endpoints, but extracts index values based on volume rather than a time based frequency. For example, this function could be used to create an OHLCV series with an observation every 5000 shares traded. This is based on the idea of a "volume clock" as presented in The Volume Clock: Insights into the High Frequency Paradigm.

volume.endpoints <- function (x, k = 1)
{
    # assumes x is univariate with volume or trade size
    x <- try.xts(x)
    z <- coredata(na.fill(x, 0))
    sum.vol <- 0
    v.ep <- 0
    ne0 <- which(z != 0)
    for (i in ne0) {
        sum.vol <- sum.vol + z[i]
        if (sum.vol >= k) {
            v.ep <- c(v.ep, i)
            sum.vol <- 0
        }
    }
    if (last(v.ep) != nrow(x))
        v.ep <- c(v.ep, nrow(x))
    v.ep
}

rossb34 avatar May 10 '18 18:05 rossb34