Lean  $LEAN_TAG$
DoubleExponentialMovingAverage.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 {
18  /// <summary>
19  /// This indicator computes the Double Exponential Moving Average (DEMA).
20  /// The Double Exponential Moving Average is calculated with the following formula:
21  /// EMA2 = EMA(EMA(t,period),period)
22  /// DEMA = 2 * EMA(t,period) - EMA2
23  /// The Generalized DEMA (GD) is calculated with the following formula:
24  /// GD = (volumeFactor+1) * EMA(t,period) - volumeFactor * EMA2
25  /// </summary>
27  {
28  private readonly int _period;
29  private readonly decimal _volumeFactor;
30  private readonly ExponentialMovingAverage _ema1;
31  private readonly ExponentialMovingAverage _ema2;
32 
33  /// <summary>
34  /// Initializes a new instance of the <see cref="DoubleExponentialMovingAverage"/> class using the specified name and period.
35  /// </summary>
36  /// <param name="name">The name of this indicator</param>
37  /// <param name="period">The period of the DEMA</param>
38  /// <param name="volumeFactor">The volume factor of the DEMA (value must be in the [0,1] range, set to 1 for standard DEMA)</param>
39  public DoubleExponentialMovingAverage(string name, int period, decimal volumeFactor = 1m)
40  : base(name)
41  {
42  _period = period;
43  _volumeFactor = volumeFactor;
44  _ema1 = new ExponentialMovingAverage(name + "_1", period);
45  _ema2 = new ExponentialMovingAverage(name + "_2", period);
46  }
47 
48  /// <summary>
49  /// Initializes a new instance of the <see cref="DoubleExponentialMovingAverage"/> class using the specified period.
50  /// </summary>
51  /// <param name="period">The period of the DEMA</param>
52  /// <param name="volumeFactor">The volume factor of the DEMA (value must be in the [0,1] range, set to 1 for standard DEMA)</param>
53  public DoubleExponentialMovingAverage(int period, decimal volumeFactor = 1m)
54  : this($"DEMA({period})", period, volumeFactor)
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 => Samples > 2 * (_period - 1);
62 
63  /// <summary>
64  /// Required period, in data points, for the indicator to be ready and fully initialized.
65  /// </summary>
66  public int WarmUpPeriod => 1 + 2 * (_period - 1);
67 
68  /// <summary>
69  /// Computes the next value of this indicator from the given state
70  /// </summary>
71  /// <param name="input">The input given to the indicator</param>
72  /// <returns>A new value for this indicator</returns>
73  protected override decimal ComputeNextValue(IndicatorDataPoint input)
74  {
75  _ema1.Update(input);
76 
77  if (!_ema1.IsReady)
78  return _ema1.Current.Value;
79 
80  _ema2.Update(_ema1.Current);
81 
82  return (_volumeFactor + 1) * _ema1.Current.Value - _volumeFactor * _ema2.Current.Value;
83  }
84 
85  /// <summary>
86  /// Resets this indicator to its initial state
87  /// </summary>
88  public override void Reset()
89  {
90  _ema1.Reset();
91  _ema2.Reset();
92  base.Reset();
93  }
94  }
95 }