rquantstratttr

Why do Close and HLC have different methods to share data?


When I compare the 2 methods, SMA() needs close price as first parameter in xts format and DonchianChannel() needs HL in xts format. However the usage is Cl(mktdata) for SMA vs HLC(mktdata)[, 1:2] for DonchianChannel().

Why is this so? why cannot i just use HLC(mktdata)?

add.indicator(strategy = strategy.st,
                  name = "SMA",
                  arguments = list(x = quote(Cl(mktdata)),
                                   n = 30),
                  label = "nSlow")
add.indicator(strategy = strategy.st, 
                  # correct name of function:
                  name = "DonchianChannel",
                  arguments = list(HL = quote(HLC(mktdata)[, 1:2]), 
                                   n = 20),
                  label = "DCH"

)

Solution

  • HLC(mktdata) returns a 3 column xts object (High, Low and Close columns), but in DonchianChannel(HL = ), HL expects 2 columns, containing the high and low prices, or just 1 column (of say close prices). Not 3 columns.

    Look at the source code of the function and you'll see the # of columns guard at the start:

    DonchianChannel <- function (HL, n = 10, include.lag = FALSE) 
    {
        HL <- try.xts(HL, error = as.matrix)
        if (!(NCOL(HL) %in% c(1, 2))) {
            stop("Price series must be either High-Low, or Close/univariate.")
        }
    .........