Hello all,
I am currently trying to use the output from the keltner channels built-in indicator in a RollingWingow, however this is appearing to be very difficult. Trying to use the direct value of the UpperBand from the indicator does not work as it is of type IBaseDataBar. Do try and circumvent this issue I tried passing the value into an IndicatorDataPoint class and then adding that to the rolling window:
class KeltnerVortexStandardAlgo(QCAlgorithm):
def Initialize(self):
self.TradedSymbol = "AAPL"
self.AddEquity(self.TradedSymbol, Resolution.Minute)
self.SetStartDate(2020, 6, 1) # Set Start Date
self.SetCash(100000) # Set Strategy Cash
# self.AddEquity("SPY", Resolution.Minute)
self.KCH(self.TradedSymbol, 20, 1.5, MovingAverageType.Simple, Resolution.Minute).Updated += self.OnKCHUpdated
self.UpperBandWin = RollingWindow[IndicatorDataPoint](2)
self.SetWarmUp(21, Resolution.Minute)
def OnKCHUpdated(self, sender, updated):
self.UpperBandWin.Add(IndicatorDataPoint(updated.Time, sender.UpperBand))
def OnData(self, data):
'''OnData event is the primary entry point for your algorithm. Each new data point will be pumped in here.
Arguments:
data: Slice object keyed by symbol containing the stock data
'''
if not self.UpperBandWin.IsReady: return
self.Debug(self.UpperBandWin[0])
# if not self.Portfolio.Invested:
# self.SetHoldings("SPY", 1)
This however causes the RollingWindow to just be filled with IndicatorDataPoint classes with the correct Time attribute but a Value of 0.0 as shown in the attached backtest:
Looking into the source code of the Keltner Channles indicator, it looks like this is happens when the MiddleBand's IsReady attribute is false:
/// <summary>
/// Initializes a new instance of the KeltnerChannels class
/// </summary>
/// <param name="name">The name of this indicator</param>
/// <param name="period">The period of the average true range and moving average (middle band)</param>
/// <param name="k">The number of multiples specifying the distance between the middle band and upper or lower bands</param>
/// <param name="movingAverageType">The type of moving average to be used</param>
public KeltnerChannels(string name, int period, decimal k, MovingAverageType movingAverageType = MovingAverageType.Simple)
: base(name)
{
WarmUpPeriod = period;
//Initialise ATR and SMA
AverageTrueRange = new AverageTrueRange(name + "_AverageTrueRange", period, MovingAverageType.Simple);
MiddleBand = movingAverageType.AsIndicator(name + "_MiddleBand", period);
//Compute Lower Band
LowerBand = new FunctionalIndicator<IBaseDataBar>(name + "_LowerBand",
input => MiddleBand.IsReady ? MiddleBand.Current.Value - AverageTrueRange.Current.Value * k : decimal.Zero,
lowerBand => MiddleBand.IsReady,
() => MiddleBand.Reset()
);
//Compute Upper Band
UpperBand = new FunctionalIndicator<IBaseDataBar>(name + "_UpperBand",
input => MiddleBand.IsReady ? MiddleBand.Current.Value + AverageTrueRange.Current.Value * k : decimal.Zero,
upperBand => MiddleBand.IsReady,
() => MiddleBand.Reset()
);
}
Is there any way to utilise the UpperBand attribute of the indicator in a rolling window?
Thanks.
Shile Wen
Hi Marcel,
The sender parameter is the indicator object, so we can access the upper band through this. I've shown this in the attached backtest.
Best,
Shile Wen
Marcel Madden
The material on this website is provided for informational purposes only and does not constitute an offer to sell, a solicitation to buy, or a recommendation or endorsement for any security or strategy, nor does it constitute an offer to provide investment advisory services by QuantConnect. In addition, the material offers no opinion with respect to the suitability of any security or specific investment. QuantConnect makes no guarantees as to the accuracy or completeness of the views expressed in the website. The views are subject to change, and may have become unreliable for various reasons, including changes in market conditions or economic circumstances. All investments involve risk, including loss of principal. You should consult with an investment professional before making any investment decisions.
To unlock posting to the community forums please complete at least 30% of Boot Camp.
You can continue your Boot Camp training progress from the terminal. We hope to see you in the community soon!