Supported Indicators

Keltner Channels

Introduction

This indicator creates a moving average (middle band) with an upper band and lower band fixed at k average True range multiples away from the middle band.

To view the implementation of this indicator, see the LEAN GitHub repository.

Using KCH Indicator

To create an automatic indicators for KeltnerChannels, call the KCH helper method from the QCAlgorithm class. The KCH method creates a KeltnerChannels object, hooks it up for automatic updates, and returns it so you can used it in your algorithm. In most cases, you should call the helper method in the Initialize method.

public class KeltnerChannelsAlgorithm : QCAlgorithm
{
    private Symbol _symbol;
    private KeltnerChannels _kch;

    public override void Initialize()
    {
        _symbol = AddEquity("SPY", Resolution.Daily).Symbol;
        _kch = KCH(_symbol, 20, 2, MovingAverageType.Simple);
    }

    public override void OnData(Slice data)
    {
        if (_kch.IsReady)
        {
            // The current value of _kch is represented by itself (_kch)
            // or _kch.Current.Value
            Plot("KeltnerChannels", "kch", _kch);
            // Plot all properties of kch
            Plot("KeltnerChannels", "middleband", _kch.MiddleBand);
            Plot("KeltnerChannels", "upperband", _kch.UpperBand);
            Plot("KeltnerChannels", "lowerband", _kch.LowerBand);
            Plot("KeltnerChannels", "averagetruerange", _kch.AverageTrueRange);
        }
    }
}
class KeltnerChannelsAlgorithm(QCAlgorithm):
    def Initialize(self) -> None:
        self.symbol = self.AddEquity("SPY", Resolution.Daily).Symbol
        self.kch = self.KCH(self.symbol, 20, 2, MovingAverageType.Simple)

    def OnData(self, slice: Slice) -> None:
        if self.kch.IsReady:
            # The current value of self.kch is represented by self.kch.Current.Value
            self.Plot("KeltnerChannels", "kch", self.kch.Current.Value)
            # Plot all attributes of self.kch
            self.Plot("KeltnerChannels", "middleband", self.kch.MiddleBand.Current.Value)
            self.Plot("KeltnerChannels", "upperband", self.kch.UpperBand.Current.Value)
            self.Plot("KeltnerChannels", "lowerband", self.kch.LowerBand.Current.Value)
            self.Plot("KeltnerChannels", "averagetruerange", self.kch.AverageTrueRange.Current.Value)

The following reference table describes the KCH method:

KCH()1/1

            KeltnerChannels QuantConnect.Algorithm.QCAlgorithm.KCH (
    Symbol                                symbol,
    Int32                                 period,
    Decimal                               k,
    *MovingAverageType                    movingAverageType,
    *Nullable<Resolution>           resolution,
    *Func<IBaseData, IBaseDataBar>  selector
   )
        

Creates a new Keltner Channels indicator. The indicator will be automatically updated on the given resolution.

If you don't provide a resolution, it defaults to the security resolution. If you provide a resolution, it must be greater than or equal to the resolution of the security. For instance, if you subscribe to hourly data for a security, you should update its indicator with data that spans 1 hour or longer.

For more information about the selector argument, see Alternative Price Fields.

For more information about plotting indicators, see Plotting Indicators.

The following table describes the MovingAverageType enumeration members:

To avoid parameter ambiguity, use the resolution argument to set the Resolution.

public class KeltnerChannelsAlgorithm : QCAlgorithm
{
    private Symbol _symbol;
    private KeltnerChannels _kch;

    public override void Initialize()
    {
        _symbol = AddEquity("SPY", Resolution.Hour).Symbol;
        _kch = KCH(_symbol, 20, 2, MovingAverageType.Simple, resolution: Resolution.Daily);
    }
}
class KeltnerChannelsAlgorithm(QCAlgorithm):
    def Initialize(self) -> None:
        self.symbol = self.AddEquity("SPY", Resolution.Hour).Symbol
        self.kch = self.KCH(self.symbol, 20, 2, MovingAverageType.Simple, resolution=Resolution.Daily)

You can manually create a KeltnerChannels indicator, so it doesn't automatically update. Manual indicators let you update their values with any data you choose.

Updating your indicator manually enables you to control when the indicator is updated and what data you use to update it. To manually update the indicator, call the Update method with a TradeBar or QuoteBar. The indicator will only be ready after you prime it with enough data.

public class KeltnerChannelsAlgorithm : QCAlgorithm
{
    private Symbol _symbol;
    private KeltnerChannels _kch;

    public override void Initialize()
    {
        _symbol = AddEquity("SPY", Resolution.Daily).Symbol;
        _kch = new KeltnerChannels(20, 2, MovingAverageType.Simple);
    }

    public override void OnData(Slice data)
    {
        if (data.Bars.TryGetValue(_symbol, out var bar))
        {      
            _kch.Update(bar);
        }
   
        if (_kch.IsReady)
        {
            // The current value of _kch is represented by itself (_kch)
            // or _kch.Current.Value
            Plot("KeltnerChannels", "kch", _kch);
            // Plot all properties of kch
            Plot("KeltnerChannels", "middleband", _kch.MiddleBand);
            Plot("KeltnerChannels", "upperband", _kch.UpperBand);
            Plot("KeltnerChannels", "lowerband", _kch.LowerBand);
            Plot("KeltnerChannels", "averagetruerange", _kch.AverageTrueRange);
        }
    }
}
class KeltnerChannelsAlgorithm(QCAlgorithm):
    def Initialize(self) -> None:
        self.symbol = self.AddEquity("SPY", Resolution.Daily).Symbol
        self.kch = KeltnerChannels(20, 2, MovingAverageType.Simple)

    def OnData(self, slice: Slice) -> None:
        bar = slice.Bars.get(self.symbol)
        if bar:
            self.kch.Update(bar)
        if self.kch.IsReady:
            # The current value of self.kch is represented by self.kch.Current.Value
            self.Plot("KeltnerChannels", "kch", self.kch.Current.Value)
            # Plot all attributes of self.kch
            self.Plot("KeltnerChannels", "middleband", self.kch.MiddleBand.Current.Value)
            self.Plot("KeltnerChannels", "upperband", self.kch.UpperBand.Current.Value)
            self.Plot("KeltnerChannels", "lowerband", self.kch.LowerBand.Current.Value)
            self.Plot("KeltnerChannels", "averagetruerange", self.kch.AverageTrueRange.Current.Value)

To register a manual indicator for automatic updates with the security data, call the RegisterIndicator method.

public class KeltnerChannelsAlgorithm : QCAlgorithm
{
    private Symbol _symbol;
    private KeltnerChannels _kch;

    public override void Initialize()
    {
        _symbol = AddEquity("SPY", Resolution.Daily).Symbol;
        _kch = new KeltnerChannels(20, 2, MovingAverageType.Simple);
        RegisterIndicator(_symbol, _kch, Resolution.Daily);
    }

    public override void OnData(Slice data)
    {
        if (_kch.IsReady)
        {
            // The current value of _kch is represented by itself (_kch)
            // or _kch.Current.Value
            Plot("KeltnerChannels", "kch", _kch);
            // Plot all properties of kch
            Plot("KeltnerChannels", "middleband", _kch.MiddleBand);
            Plot("KeltnerChannels", "upperband", _kch.UpperBand);
            Plot("KeltnerChannels", "lowerband", _kch.LowerBand);
            Plot("KeltnerChannels", "averagetruerange", _kch.AverageTrueRange);
        }
    }
}
class KeltnerChannelsAlgorithm(QCAlgorithm):
    def Initialize(self) -> None:
        self.symbol = self.AddEquity("SPY", Resolution.Daily).Symbol
        self.kch = KeltnerChannels(20, 2, MovingAverageType.Simple)
        self.RegisterIndicator(self.symbol, self.kch, Resolution.Daily)

    def OnData(self, slice: Slice) -> None:
        if self.kch.IsReady:
            # The current value of self.kch is represented by self.kch.Current.Value
            self.Plot("KeltnerChannels", "kch", self.kch.Current.Value)
            # Plot all attributes of self.kch
            self.Plot("KeltnerChannels", "middleband", self.kch.MiddleBand.Current.Value)
            self.Plot("KeltnerChannels", "upperband", self.kch.UpperBand.Current.Value)
            self.Plot("KeltnerChannels", "lowerband", self.kch.LowerBand.Current.Value)
            self.Plot("KeltnerChannels", "averagetruerange", self.kch.AverageTrueRange.Current.Value)

The following reference table describes the KeltnerChannels constructor:

KeltnerChannels()1/2

            KeltnerChannels QuantConnect.Indicators.KeltnerChannels (
    int                 period,
    decimal             k,
    *MovingAverageType  movingAverageType
   )
        

Initializes a new instance of the KeltnerChannels class.

KeltnerChannels()2/2

            KeltnerChannels QuantConnect.Indicators.KeltnerChannels (
    string              name,
    int                 period,
    decimal             k,
    *MovingAverageType  movingAverageType
   )
        

Initializes a new instance of the KeltnerChannels class.

Visualization

The following image shows plot values of selected properties of KeltnerChannels using the plotly library.

KeltnerChannels line plot.

You can also see our Videos. You can also get in touch with us via Discord.

Did you find this page helpful?

Contribute to the documentation: