Search code examples
rquantmod

R Keltner Channels


Is there any package for R that already implements Keltner Channels, what I'm trying to find out is in which channel the daily close price is positioned (-3, -2, -1, 1, 2, 3)

I found the ATR function in TTR but I'm not sure if there is a way to use it, what I'm trying to get is something similar to what the link describes, I only need the values, not the graph, so that I can compare the daily closing price vs the channels

Keltner Channels : StockCharts.com

Keltner Channels

Any help pointing me on the right direction is appreciated


Solution

  • This kind of template code will help you get what you specifically want. Choose your own n values etc.

    library(quantmod)
    getSymbols("AAPL")
    
    get_Keltner_channels <- function(ohlc, n_EMA = 10, n_ATR = 14, multiplier = 2) {
        mid <- EMA(Cl(ohlc), n_EMA)    
        hi <- mid + multiplier * ATR(HLC = HLC(ohlc), n = n_ATR)$atr
        lo <- mid - multiplier * ATR(HLC = HLC(ohlc), n = n_ATR)$atr
        keltner <- cbind(lo, mid, hi)
        colnames(keltner) <- c("Kelt_lo", "Kelt_mid", "Kelt_hi")
        keltner
    }
    
    out <- get_Keltner_channels(AAPL)
    
    # > tail(out)
    # Kelt_lo Kelt_mid   Kelt_hi
    # 2016-07-15 94.26621 97.09092  99.91563
    # 2016-07-18 94.74742 97.58893 100.43045
    # 2016-07-19 95.27084 98.00367 100.73651
    # 2016-07-20 95.71888 98.35937 100.99986
    # 2016-07-21 95.83500 98.55403 101.27306
    # 2016-07-22 95.88848 98.57330 101.25811