Lean  $LEAN_TAG$
ExponentialMovingAverage.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  /// Represents the traditional exponential moving average indicator (EMA).
20  /// When the indicator is ready, the first value of the EMA is equivalent to the simple moving average.
21  /// After the first EMA value, the EMA value is a function of the previous EMA value.
22  /// Therefore, depending on the number of samples
23  /// you feed into the indicator, it can provide different EMA values for a single
24  /// security and lookback period. To make the indicator values consistent
25  /// across time, warm up the indicator with all the trailing security price history.
26  /// </summary>
28  {
29  private readonly decimal _k;
30  private readonly int _period;
31 
32  private readonly SimpleMovingAverage _initialValueSMA;
33 
34  /// <summary>
35  /// Required period, in data points, for the indicator to be ready and fully initialized.
36  /// </summary>
37  public int WarmUpPeriod => _period;
38 
39  /// <summary>
40  /// Initializes a new instance of the ExponentialMovingAverage class with the specified name and period
41  /// </summary>
42  /// <param name="name">The name of this indicator</param>
43  /// <param name="period">The period of the EMA</param>
44  public ExponentialMovingAverage(string name, int period)
45  : this(name, period, SmoothingFactorDefault(period))
46  {
47  }
48 
49  /// <summary>
50  /// Initializes a new instance of the ExponentialMovingAverage class with the specified name and period
51  /// </summary>
52  /// <param name="name">The name of this indicator</param>
53  /// <param name="period">The period of the EMA</param>
54  /// <param name="smoothingFactor">The percentage of data from the previous value to be carried into the next value</param>
55  public ExponentialMovingAverage(string name, int period, decimal smoothingFactor)
56  : base(name)
57  {
58  _period = period;
59  _k = smoothingFactor;
60  _initialValueSMA = new SimpleMovingAverage(period);
61  }
62 
63  /// <summary>
64  /// Initializes a new instance of the ExponentialMovingAverage class with the default name and period
65  /// </summary>
66  /// <param name="period">The period of the EMA</param>
67  public ExponentialMovingAverage(int period)
68  : this($"EMA({period})", period)
69  {
70  }
71 
72  /// <summary>
73  /// Initializes a new instance of the ExponentialMovingAverage class with the default name and period
74  /// </summary>
75  /// <param name="period">The period of the EMA</param>
76  /// <param name="smoothingFactor">The percentage of data from the previous value to be carried into the next value</param>
77  public ExponentialMovingAverage(int period, decimal smoothingFactor)
78  : this($"EMA({period},{smoothingFactor})", period, smoothingFactor)
79  {
80  }
81 
82  /// <summary>
83  /// Calculates the default smoothing factor for an ExponentialMovingAverage indicator
84  /// </summary>
85  /// <param name="period">The period of the EMA</param>
86  /// <returns>The default smoothing factor</returns>
87  public static decimal SmoothingFactorDefault(int period) => 2.0m / (1 + period);
88 
89  /// <summary>
90  /// Gets a flag indicating when this indicator is ready and fully initialized
91  /// </summary>
92  public override bool IsReady => Samples >= _period;
93 
94  /// <summary>
95  /// Resets this indicator to its initial state
96  /// </summary>
97  public override void Reset()
98  {
99  base.Reset();
100  _initialValueSMA.Reset();
101  }
102 
103  /// <summary>
104  /// Computes the next value of this indicator from the given state
105  /// </summary>
106  /// <param name="input">The input given to the indicator</param>
107  /// <returns>A new value for this indicator</returns>
108  protected override decimal ComputeNextValue(IndicatorDataPoint input)
109  {
110  // we need to compute the initial value for the EMA, which is the SMA of the first N samples
111  if (Samples <= _period)
112  {
113  _initialValueSMA.Update(input);
114  }
115 
116  if (!IsReady)
117  {
118  return 0;
119  }
120 
121  if (Samples == _period)
122  {
123  // first value is the SMA of the first period
124  return _initialValueSMA.Current.Value;
125  }
126 
127  return input.Value * _k + Current.Value * (1 - _k);
128  }
129  }
130 }