Lean  $LEAN_TAG$
SqueezeMomentum.cs
1 /*
2  * QUANTCONNECT.COM - Democratizing Finance, Empowering Individuals.
3  * Lean Algorithmic Trading Engine v2.0. Copyright 2014 QuantConnect Corporation.
4  *
5  * Licensed under the Apache License, Version 2.0 (the "License");
6  * you may not use this file except in compliance with the License.
7  * You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0
8  *
9  * Unless required by applicable law or agreed to in writing, software
10  * distributed under the License is distributed on an "AS IS" BASIS,
11  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12  * See the License for the specific language governing permissions and
13  * limitations under the License.
14 */
15 
16 using System;
18 
20 {
21  /// <summary>
22  /// The SqueezeMomentum indicator calculates whether the market is in a "squeeze" condition,
23  /// determined by comparing Bollinger Bands to Keltner Channels. When the Bollinger Bands are
24  /// inside the Keltner Channels, the indicator returns 1 (squeeze on). Otherwise, it returns -1 (squeeze off).
25  /// </summary>
27  {
28  /// <summary>
29  /// The Bollinger Bands indicator used to calculate the upper, lower, and middle bands.
30  /// </summary>
32 
33  /// <summary>
34  /// The Keltner Channels indicator used to calculate the upper, lower, and middle channels.
35  /// </summary>
37 
38  /// <summary>
39  /// Initializes a new instance of the <see cref="SqueezeMomentum"/> class.
40  /// </summary>
41  /// <param name="name">The name of the indicator.</param>
42  /// <param name="bollingerPeriod">The period used for the Bollinger Bands calculation.</param>
43  /// <param name="bollingerMultiplier">The multiplier for the Bollinger Bands width.</param>
44  /// <param name="keltnerPeriod">The period used for the Average True Range (ATR) calculation in Keltner Channels.</param>
45  /// <param name="keltnerMultiplier">The multiplier applied to the ATR for calculating Keltner Channels.</param>
46  public SqueezeMomentum(string name, int bollingerPeriod, decimal bollingerMultiplier, int keltnerPeriod, decimal keltnerMultiplier) : base(name)
47  {
48  BollingerBands = new BollingerBands(bollingerPeriod, bollingerMultiplier);
49  KeltnerChannels = new KeltnerChannels(keltnerPeriod, keltnerMultiplier, MovingAverageType.Exponential);
50  WarmUpPeriod = Math.Max(bollingerPeriod, keltnerPeriod);
51  }
52 
53  /// <summary>
54  /// Gets the warm-up period required for the indicator to be ready.
55  /// This is determined by the warm-up period of the Bollinger Bands indicator.
56  /// </summary>
57  public int WarmUpPeriod { get; }
58 
59  /// <summary>
60  /// Indicates whether the indicator is ready and has enough data for computation.
61  /// The indicator is ready when both the Bollinger Bands and the Average True Range are ready.
62  /// </summary>
64 
65  /// <summary>
66  /// Computes the next value of the indicator based on the input data bar.
67  /// </summary>
68  /// <param name="input">The input data bar.</param>
69  /// <returns>
70  /// Returns 1 if the Bollinger Bands are inside the Keltner Channels (squeeze on),
71  /// or -1 if the Bollinger Bands are outside the Keltner Channels (squeeze off).
72  /// </returns>
73  protected override decimal ComputeNextValue(IBaseDataBar input)
74  {
75  BollingerBands.Update(input);
76  KeltnerChannels.Update(input);
77  if (!IsReady)
78  {
79  return decimal.Zero;
80  }
81 
82  // Calculate Bollinger Bands upper, lower
83  var bbUpper = BollingerBands.UpperBand.Current.Value;
84  var bbLower = BollingerBands.LowerBand.Current.Value;
85 
86  // Calculate Keltner Channels upper and lower bounds
87  var kcUpper = KeltnerChannels.UpperBand.Current.Value;
88  var kcLower = KeltnerChannels.LowerBand.Current.Value;
89 
90  // Determine if the squeeze condition is on or off
91  return (kcUpper > bbUpper && kcLower < bbLower) ? 1m : -1m;
92  }
93 
94  /// <summary>
95  /// Resets the state of the indicator, including all sub-indicators.
96  /// </summary>
97  public override void Reset()
98  {
101  base.Reset();
102  }
103  }
104 }