Lean  $LEAN_TAG$
KaufmanAdaptiveMovingAverage.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 Kaufman Adaptive Moving Average (KAMA).
20  /// The Kaufman Adaptive Moving Average is calculated as explained here:
21  /// http://stockcharts.com/school/doku.php?id=chart_school:technical_indicators:kaufman_s_adaptive_moving_average
22  /// </summary>
24  {
25  private readonly decimal _slowSmoothingFactor;
26  private readonly decimal _diffSmoothingFactor;
27  private decimal _prevKama;
28 
29  /// <summary>
30  /// Initializes a new instance of the <see cref="KaufmanAdaptiveMovingAverage"/> class using the specified name and period.
31  /// </summary>
32  /// <param name="name">The name of this indicator</param>
33  /// <param name="period">The period of the Efficiency Ratio (ER)</param>
34  /// <param name="fastEmaPeriod">The period of the fast EMA used to calculate the Smoothing Constant (SC)</param>
35  /// <param name="slowEmaPeriod">The period of the slow EMA used to calculate the Smoothing Constant (SC)</param>
36  public KaufmanAdaptiveMovingAverage(string name, int period, int fastEmaPeriod = 2, int slowEmaPeriod = 30)
37  : base(name, period)
38  {
39  // Smoothing factor of the slow EMA
40  _slowSmoothingFactor = 2m / (slowEmaPeriod + 1m);
41  // Difference between the smoothing factor of the fast and slow EMA
42  _diffSmoothingFactor = 2m / (fastEmaPeriod + 1m) - _slowSmoothingFactor;
43  }
44 
45  /// <summary>
46  /// Initializes a new instance of the <see cref="KaufmanAdaptiveMovingAverage"/> class using the specified period.
47  /// </summary>
48  /// <param name="period">The period of the Efficiency Ratio (ER)</param>
49  /// <param name="fastEmaPeriod">The period of the fast EMA used to calculate the Smoothing Constant (SC)</param>
50  /// <param name="slowEmaPeriod">The period of the slow EMA used to calculate the Smoothing Constant (SC)</param>
51  public KaufmanAdaptiveMovingAverage(int period, int fastEmaPeriod = 2, int slowEmaPeriod = 30)
52  : this($"KAMA({period},{fastEmaPeriod},{slowEmaPeriod})", period, fastEmaPeriod, slowEmaPeriod)
53  {
54  }
55 
56  /// <summary>
57  /// Computes the next value of this indicator from the given state
58  /// </summary>
59  /// <param name="input">The input given to the indicator</param>
60  /// <param name="window">The window for the input history</param>
61  /// <returns>A new value for this indicator</returns>
63  {
64  // Calculate the efficiency ratio
65  var efficiencyRatio = base.ComputeNextValue(window, input);
66 
67  if (Samples < Period)
68  {
69  return input.Value;
70  }
71 
72  if (Samples == Period)
73  {
74  // Calculate the first KAMA
75  // The yesterday price is used here as the previous KAMA.
76  _prevKama = window[1].Value;
77  }
78 
79  // Calculate the smoothing constant
80  var smoothingConstant = efficiencyRatio * _diffSmoothingFactor + _slowSmoothingFactor;
81  smoothingConstant *= smoothingConstant;
82 
83  // Calculate the KAMA like an EMA, using the
84  // smoothing constant as the adaptive factor.
85  _prevKama = (input.Value - _prevKama) * smoothingConstant + _prevKama;
86 
87  return _prevKama;
88  }
89 
90  /// <summary>
91  /// Resets this indicator to its initial state
92  /// </summary>
93  public override void Reset()
94  {
95  _prevKama = 0;
96  base.Reset();
97  }
98  }
99 }