Lean  $LEAN_TAG$
MassIndex.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 Mass Index uses the high-low range to identify trend reversals based on range expansions.
22  /// In this sense, the Mass Index is a volatility indicator that does not have a directional
23  /// bias. Instead, the Mass Index identifies range bulges that can foreshadow a reversal of the
24  /// current trend. Developed by Donald Dorsey.
25  /// </summary>
26  /// <seealso cref="TradeBarIndicator"/>
28  {
29  private readonly ExponentialMovingAverage _ema1;
30  private readonly ExponentialMovingAverage _ema2;
31  private readonly Sum _sum;
32 
33  /// <summary>
34  /// Initializes a new instance of the <see cref="MassIndex"/> class.
35  /// </summary>
36  /// <param name="name">The name for this instance.</param>
37  /// <param name="emaPeriod">The period used by both EMA.</param>
38  /// <param name="sumPeriod">The sum period.</param>
39  public MassIndex(string name, int emaPeriod, int sumPeriod)
40  : base(name)
41  {
42  _ema1 = new ExponentialMovingAverage(emaPeriod);
43  _ema2 = _ema1.EMA(emaPeriod);
44  _sum = new Sum(sumPeriod);
45  WarmUpPeriod = 2 * (emaPeriod - 1) + sumPeriod;
46  }
47 
48  /// <summary>
49  /// Initializes a new instance of the <see cref="MassIndex"/> class.
50  /// </summary>
51  /// <param name="emaPeriod">The period used by both EMA.</param>
52  /// <param name="sumPeriod">The sum period.</param>
53  public MassIndex(int emaPeriod = 9, int sumPeriod = 25)
54  : this($"MASS({emaPeriod},{sumPeriod})", emaPeriod, sumPeriod)
55  {
56  }
57 
58  /// <summary>
59  /// Gets a flag indicating when this indicator is ready and fully initialized
60  /// </summary>
61  public override bool IsReady => _sum.IsReady;
62 
63  /// <summary>
64  /// Required period, in data points, for the indicator to be ready and fully initialized.
65  /// </summary>
66  public int WarmUpPeriod { get; }
67 
68  /// <summary>
69  /// Resets this indicator to its initial state
70  /// </summary>
71  public override void Reset()
72  {
73  base.Reset();
74  _ema1.Reset();
75  _ema2.Reset();
76  _sum.Reset();
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>
84  /// A new value for this indicator
85  /// </returns>
86  protected override decimal ComputeNextValue(TradeBar input)
87  {
88  _ema1.Update(input.Time, input.High - input.Low);
89  if (_ema2.IsReady)
90  {
91  _sum.Update(input.Time, _ema1.Current.Value.SafeDivision(_ema2.Current.Value));
92  }
93 
94  if (!_sum.IsReady)
95  {
96  return _sum.Period;
97  }
98  return _sum.Current.Value;
99  }
100  }
101 }