Lean  $LEAN_TAG$
McGinleyDynamic.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;
17 
19 {
20  /// <summary>
21  /// Represents the McGinley Dynamic (MGD)
22  /// It is a type of moving average that was designed to track the market better
23  /// than existing moving average indicators.
24  /// It is a technical indicator that improves upon moving average lines by adjusting
25  /// for shifts in market speed.
26  /// </summary>
28  {
29  /// <summary>
30  /// A rolling sum for computing the average for the given period
31  /// </summary>
32  private readonly IndicatorBase<IndicatorDataPoint> _rollingSum;
33 
34  private readonly int _period;
35 
36  /// <summary>
37  /// Gets a flag indicating when this indicator is ready and fully initialized
38  /// </summary>
39  public override bool IsReady => _rollingSum.IsReady;
40 
41  /// <summary>
42  /// Required period, in data points, for the indicator to be ready and fully initialized.
43  /// </summary>
44  public override int WarmUpPeriod => Period;
45 
46  /// <summary>
47  /// Initializes a new instance of the McGinleyDynamic class with the specified name and period
48  /// </summary>
49  /// <param name="name">The name of this indicator</param>
50  /// <param name="period">The period of the McGinley Dynamic</param>
51  public McGinleyDynamic(string name, int period)
52  : base(name, period)
53  {
54  if (period == 0) throw new ArgumentException("Period can not be zero");
55  _period = period;
56  _rollingSum = new Sum(name + "_Sum", period);
57  }
58 
59  /// <summary>
60  /// Initializes a new instance of the McGinleyDynamic class with the default name and period
61  /// </summary>
62  /// <param name="period">The period of the McGinley Dynamic</param>
63  public McGinleyDynamic(int period)
64  : this($"MGD({period})", period)
65  {
66  }
67 
68  /// <summary>
69  /// Computes the next value for this indicator from the given state.
70  /// </summary>
71  /// <param name="window">The window of data held in this indicator</param>
72  /// <param name="input">The input value to this indicator on this time step</param>
73  /// <returns>A new value for this indicator</returns>
75  {
76  _rollingSum.Update(input.Time, input.Value);
77  if (!IsReady)
78  {
79  return 0;
80  }
81 
82  if (Samples == _period)
83  {
84  return _rollingSum.Current.Value / _period;
85  }
86 
87  if (Current.Value == 0 || input.Value == 0)
88  {
89  return Current.Value;
90  }
91 
92  var ratioValue = (double)input.Value.SafeDivision(Current.Value, 0);
93  if (ratioValue == 0) return Current.Value;
94  var denominator = _period * (decimal)Math.Pow(ratioValue, 4.0);
95  return Current.Value + (input.Value - Current.Value).SafeDivision(denominator, 0);
96  }
97 
98  /// <summary>
99  /// Resets this indicator to its initial state
100  /// </summary>
101  public override void Reset()
102  {
103  _rollingSum.Reset();
104  base.Reset();
105  }
106  }
107 }