Lean  $LEAN_TAG$
McClellanSummationIndex.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 
17 
19 {
20  /// <summary>
21  /// The McClellan Summation Index (MSI) is a market breadth indicator that is based on the rolling average of difference
22  /// between the number of advancing and declining issues on a stock exchange. It is generally considered as is
23  /// a long-term version of the <see cref="McClellanOscillator"/>
24  /// </summary>
26  {
27  /// <summary>
28  /// The McClellan Summation Index value
29  /// </summary>
30  /// <remarks>Protected for testing</remarks>
31  protected IndicatorDataPoint Summation { get; }
32 
33  /// <summary>
34  /// The McClellan Oscillator is a market breadth indicator which was developed by Sherman and Marian McClellan. It is based on the difference between the number of advancing and declining periods.
35  /// </summary>
37 
38  /// <summary>
39  /// Gets a flag indicating when this indicator is ready and fully initialized
40  /// </summary>
41  public override bool IsReady => McClellanOscillator.IsReady;
42 
43  /// <summary>
44  /// Required period, in data points, for the indicator to be ready and fully initialized.
45  /// </summary>
47 
48  /// <summary>
49  /// Initializes a new instance of the <see cref="McClellanSummationIndex"/> class
50  /// <param name="name">The name of the indicator</param>
51  /// <param name="fastPeriod">The fast period of EMA of advance decline difference</param>
52  /// <param name="slowPeriod">The slow period of EMA of advance decline difference</param>
53  /// </summary>
54  public McClellanSummationIndex(string name, int fastPeriod = 19, int slowPeriod = 39) : base(name)
55  {
56  Summation = new();
57  McClellanOscillator = new McClellanOscillator(fastPeriod, slowPeriod);
58  McClellanOscillator.Updated += (_, updated) =>
59  {
60  // Update only when new indicator data point was consolidated
61  if (updated.EndTime != Summation.Time)
62  {
63  Summation.Time = updated.EndTime;
64  Summation.Value += updated.Value;
65  }
66  };
67  }
68 
69  /// <summary>
70  /// Initializes a new instance of the <see cref="McClellanSummationIndex"/> class
71  /// <param name="fastPeriod">The fast period of EMA of advance decline difference</param>
72  /// <param name="slowPeriod">The slow period of EMA of advance decline difference</param>
73  /// </summary>
74  public McClellanSummationIndex(int fastPeriod = 19, int slowPeriod = 39)
75  : this("McClellanSummationIndex", fastPeriod, slowPeriod)
76  {
77  }
78 
79  /// <summary>
80  /// Computes the next value of this indicator from the given state
81  /// </summary>
82  /// <param name="input">The input given to the indicator</param>
83  /// <returns>A new value for this indicator</returns>
84  protected override decimal ComputeNextValue(TradeBar input)
85  {
86  McClellanOscillator.Update(input);
87 
88  return Summation + McClellanOscillator.Current.Value;
89  }
90 
91  /// <summary>
92  /// Resets this indicator to its initial state
93  /// </summary>
94  public override void Reset()
95  {
97  base.Reset();
98  }
99 
100  /// <summary>
101  /// Add Tracking asset issue
102  /// </summary>
103  /// <param name="asset">the tracking asset issue</param>
104  public void Add(Symbol asset)
105  {
106  McClellanOscillator.Add(asset);
107  }
108 
109  /// <summary>
110  /// Remove Tracking asset issue
111  /// </summary>
112  /// <param name="asset">the tracking asset issue</param>
113  public void Remove(Symbol asset)
114  {
116  }
117  }
118 }