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 indicator for KeltnerChannels, call the KCHkch helper method from the QCAlgorithm class. The KCHkch 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 Initializeinitialize 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 abands
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.add_equity("SPY", Resolution.DAILY).symbol
self._kch = self.kch(self._symbol, 20, 2, MovingAverageType.SIMPLE)
def on_data(self, slice: Slice) -> None:
if self._kch.is_ready:
# 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", "middle_band", self._kch.middle_band.current.value)
self.plot("KeltnerChannels", "upper_band", self._kch.upper_band.current.value)
self.plot("KeltnerChannels", "lower_band", self._kch.lower_band.current.value)
self.plot("KeltnerChannels", "average_true_range", self._kch.average_true_range.current.value)For more information about this method, see the QCAlgorithm classQCAlgorithm class.
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 Updateupdate method. The indicator will only be ready after you prime it with enough data.
public class KeltnerChannelsAlgorithm : QCAlgorithm
{
private Symbol _symbol;
private KeltnerChannels _keltnerchannels;
public override void Initialize()
{
_symbol = AddEquity("SPY", Resolution.Daily).Symbol;
_keltnerchannels = new KeltnerChannels(20, 2, MovingAverageType.Simple);
}
public override void OnData(Slice data)
{
if (data.Bars.TryGetValue(_symbol, out var bar))
_keltnerchannels.Update(bar);
if (_keltnerchannels.IsReady)
{
// The current value of _keltnerchannels is represented by itself (_keltnerchannels)
// or _keltnerchannels.Current.Value
Plot("KeltnerChannels", "keltnerchannels", _keltnerchannels);
// Plot all properties of abands
Plot("KeltnerChannels", "middleband", _keltnerchannels.MiddleBand);
Plot("KeltnerChannels", "upperband", _keltnerchannels.UpperBand);
Plot("KeltnerChannels", "lowerband", _keltnerchannels.LowerBand);
Plot("KeltnerChannels", "averagetruerange", _keltnerchannels.AverageTrueRange);
}
}
} class KeltnerChannelsAlgorithm(QCAlgorithm):
def initialize(self) -> None:
self._symbol = self.add_equity("SPY", Resolution.DAILY).symbol
self._keltnerchannels = KeltnerChannels(20, 2, MovingAverageType.SIMPLE)
def on_data(self, slice: Slice) -> None:
bar = slice.bars.get(self._symbol)
if bar:
self._keltnerchannels.update(bar)
if self._keltnerchannels.is_ready:
# The current value of self._keltnerchannels is represented by self._keltnerchannels.current.value
self.plot("KeltnerChannels", "keltnerchannels", self._keltnerchannels.current.value)
# Plot all attributes of self._keltnerchannels
self.plot("KeltnerChannels", "middle_band", self._keltnerchannels.middle_band.current.value)
self.plot("KeltnerChannels", "upper_band", self._keltnerchannels.upper_band.current.value)
self.plot("KeltnerChannels", "lower_band", self._keltnerchannels.lower_band.current.value)
self.plot("KeltnerChannels", "average_true_range", self._keltnerchannels.average_true_range.current.value)For more information about this indicator, see its referencereference.
Indicator History
To get the historical data of the KeltnerChannels indicator, call the IndicatorHistoryself.indicator_history method. This method resets your indicator, makes a history request, and updates the indicator with the historical data. Just like with regular history requests, the IndicatorHistoryindicator_history method supports time periods based on a trailing number of bars, a trailing period of time, or a defined period of time. If you don't provide a resolution argument, it defaults to match the resolution of the security subscription.
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);
var indicatorHistory = IndicatorHistory(_kch, _symbol, 100, Resolution.Minute);
var timeSpanIndicatorHistory = IndicatorHistory(_kch, _symbol, TimeSpan.FromDays(10), Resolution.Minute);
var timePeriodIndicatorHistory = IndicatorHistory(_kch, _symbol, new DateTime(2024, 7, 1), new DateTime(2024, 7, 5), Resolution.Minute);
// Access all attributes of indicatorHistory
var middleBand = indicatorHistory.Select(x => ((dynamic)x).MiddleBand).ToList();
var upperBand = indicatorHistory.Select(x => ((dynamic)x).UpperBand).ToList();
var lowerBand = indicatorHistory.Select(x => ((dynamic)x).LowerBand).ToList();
var averageTrueRange = indicatorHistory.Select(x => ((dynamic)x).AverageTrueRange).ToList();
}
} class KeltnerChannelsAlgorithm(QCAlgorithm):
def initialize(self) -> None:
self._symbol = self.add_equity("SPY", Resolution.DAILY).symbol
self._kch = self.kch(self._symbol, 20, 2, MovingAverageType.SIMPLE)
indicator_history = self.indicator_history(self._kch, self._symbol, 100, Resolution.MINUTE)
timedelta_indicator_history = self.indicator_history(self._kch, self._symbol, timedelta(days=10), Resolution.MINUTE)
time_period_indicator_history = self.indicator_history(self._kch, self._symbol, datetime(2024, 7, 1), datetime(2024, 7, 5), Resolution.MINUTE)
# Access all attributes of indicator_history
indicator_history_df = indicator_history.data_frame
middle_band = indicator_history_df["middleband"]
upper_band = indicator_history_df["upperband"]
lower_band = indicator_history_df["lowerband"]
average_true_range = indicator_history_df["averagetruerange"]