/* * QUANTCONNECT.COM - Democratizing Finance, Empowering Individuals. * Lean Algorithmic Trading Engine v2.0. Copyright 2014 QuantConnect Corporation. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ using System; using QuantConnect.Data.Market; namespace QuantConnect.Indicators { /// /// The SqueezeMomentum indicator calculates whether the market is in a "squeeze" condition, /// determined by comparing Bollinger Bands to Keltner Channels. When the Bollinger Bands are /// inside the Keltner Channels, the indicator returns 1 (squeeze on). Otherwise, it returns -1 (squeeze off). /// public class SqueezeMomentum : BarIndicator, IIndicatorWarmUpPeriodProvider { /// /// The Bollinger Bands indicator used to calculate the upper, lower, and middle bands. /// public BollingerBands BollingerBands { get; } /// /// The Keltner Channels indicator used to calculate the upper, lower, and middle channels. /// public KeltnerChannels KeltnerChannels { get; } /// /// Initializes a new instance of the class. /// /// The name of the indicator. /// The period used for the Bollinger Bands calculation. /// The multiplier for the Bollinger Bands width. /// The period used for the Average True Range (ATR) calculation in Keltner Channels. /// The multiplier applied to the ATR for calculating Keltner Channels. public SqueezeMomentum(string name, int bollingerPeriod, decimal bollingerMultiplier, int keltnerPeriod, decimal keltnerMultiplier) : base(name) { BollingerBands = new BollingerBands(bollingerPeriod, bollingerMultiplier); KeltnerChannels = new KeltnerChannels(keltnerPeriod, keltnerMultiplier, MovingAverageType.Exponential); WarmUpPeriod = Math.Max(bollingerPeriod, keltnerPeriod); } /// /// Gets the warm-up period required for the indicator to be ready. /// This is determined by the warm-up period of the Bollinger Bands indicator. /// public int WarmUpPeriod { get; } /// /// Indicates whether the indicator is ready and has enough data for computation. /// The indicator is ready when both the Bollinger Bands and the Average True Range are ready. /// public override bool IsReady => BollingerBands.IsReady && KeltnerChannels.IsReady; /// /// Computes the next value of the indicator based on the input data bar. /// /// The input data bar. /// /// Returns 1 if the Bollinger Bands are inside the Keltner Channels (squeeze on), /// or -1 if the Bollinger Bands are outside the Keltner Channels (squeeze off). /// protected override decimal ComputeNextValue(IBaseDataBar input) { BollingerBands.Update(input); KeltnerChannels.Update(input); if (!IsReady) { return decimal.Zero; } // Calculate Bollinger Bands upper, lower var bbUpper = BollingerBands.UpperBand.Current.Value; var bbLower = BollingerBands.LowerBand.Current.Value; // Calculate Keltner Channels upper and lower bounds var kcUpper = KeltnerChannels.UpperBand.Current.Value; var kcLower = KeltnerChannels.LowerBand.Current.Value; // Determine if the squeeze condition is on or off return (kcUpper > bbUpper && kcLower < bbLower) ? 1m : -1m; } /// /// Resets the state of the indicator, including all sub-indicators. /// public override void Reset() { BollingerBands.Reset(); KeltnerChannels.Reset(); base.Reset(); } } }