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  _period = period;
55  _rollingSum = new Sum(name + "_Sum", period);
56  }
57 
58  /// <summary>
59  /// Initializes a new instance of the McGinleyDynamic class with the default name and period
60  /// </summary>
61  /// <param name="period">The period of the McGinley Dynamic</param>
62  public McGinleyDynamic(int period)
63  : this($"MGD({period})", period)
64  {
65  }
66 
67  /// <summary>
68  /// Computes the next value for this indicator from the given state.
69  /// </summary>
70  /// <param name="window">The window of data held in this indicator</param>
71  /// <param name="input">The input value to this indicator on this time step</param>
72  /// <returns>A new value for this indicator</returns>
74  {
75  _rollingSum.Update(input.Time, input.Value);
76  if (!IsReady)
77  {
78  return 0;
79  }
80  else if (Samples == _period)
81  {
82  return _rollingSum.Current.Value / _period;
83  }
84  else
85  {
86  if ((Current.Value == 0) || (input.Value == 0) || (_period == 0))
87  {
88  return Current.Value;
89  }
90  return Current.Value + (input.Value - Current.Value) / (_period * (decimal)Math.Pow((double)(input.Value / Current.Value), 4.0));
91  }
92  }
93 
94  /// <summary>
95  /// Resets this indicator to its initial state
96  /// </summary>
97  public override void Reset()
98  {
99  _rollingSum.Reset();
100  base.Reset();
101  }
102  }
103 }