joshuaulrich / TTR

Technical analysis and other functions to construct technical trading rules with R
GNU General Public License v2.0
325 stars 102 forks source link

Add Keltner Channels #106

Closed joshuaulrich closed 3 years ago

joshuaulrich commented 3 years ago

Thanks to Nick Procyk for the feature request, code, and documentation!

keltnerChannels <-
function (HLC, n = 20, maType, atr = 2, ...)
{
    atrHLC <- HLC
    HLC <- try.xts(HLC, error = as.matrix)
    if (NCOL(HLC) == 3) {
        if (is.xts(HLC)) {
            xa <- xcoredata(HLC)
            HLC <- xts(apply(HLC, 1, mean), index(HLC))
            xcoredata(HLC) <- xa
        }
        else {
            HLC <- apply(HLC, 1, mean)
        }
    }
    else if (NCOL(HLC) != 1) {
        stop("Price series must be either High-Low-Close, or Close/univariate.")
    }
    maArgs <- list(n = n, ...)
    if (missing(maType)) {
        maType <- "EMA"
    }
    mavg <- do.call(maType, c(list(HLC), maArgs))
    avgtruerange <- ATR(atrHLC, n = n)  

    up <- mavg + atr * avgtruerange[,2]
    dn <- mavg - atr * avgtruerange[,2]

    res <- cbind(dn, mavg, up)
    colnames(res) <- c("dn", "mavg", "up")
    reclass(res, HLC)
}

Keltner Channels

Description

Keltner Channels are volatility-based envelopes set above and below a moving average. This indicator is similar to Bollinger Bands, but Keltner Channels use the Average True Range (ATR) to set channel distance. Keltner Channels are a trend following indicator, and can also be used to identify overbought and oversold levels when there is no trend.

Chester Keltner is credited with the original version of Keltner Channels in his 1960 book. Linda Bradford Raschke introduced the newer version of Keltner Channels in the 1980s.

Usage

KChannels(HLC, n = 20, maType, atr = 2, ...)

Arguments

HLC Object that is coercible to xts or matrix and contains High-Low-Close prices. If only a univariate series is given, it will be used. See details. n Number of periods for moving average. maType A function or a string naming the function to be called. atr The number of average true range distances to apply. ... Other arguments to be passed to the maType function.

Details

Keltner Channels consist of three lines:

The middle band is generally a 20-period EMA of the typical price ([high + low + close]/3). The upper and lower bands are multiples of average true range (usually 2) above and below the MA.

The middle band is usually calculated using the typical price, but if a univariate series (e.g. Close, Weighted Close, Median Price, etc.) is provided, it will be used instead.

Value

A object of the same class as HLC or a matrix (if try.xts fails) containing the columns:

dn lower Keltner Channel. mavg The middle moving average. up The upper Keltner Channel.

Author(s)

Nick Procyk, Joshua Ulrich

References

The following site(s) were used to code/document this indicator: https://school.stockcharts.com/doku.php?id=technical_indicators:keltner_channels https://www.linnsoft.com/techind/keltner-channels-keltu-keltd https://www.investopedia.com/terms/k/keltnerchannel.asp

See Also

See EMA, SMA, etc. for moving average options; and note Warning section.

Example

data(ttrc)
kc <- KChannel(ttrc[,c("High","Low","Close")] )