Lean  $LEAN_TAG$
KaufmanEfficiencyRatio.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  /// This indicator computes the Kaufman Efficiency Ratio (KER).
22  /// The Kaufman Efficiency Ratio is calculated as explained here:
23  /// https://www.marketvolume.com/technicalanalysis/efficiencyratio.asp
24  /// </summary>
25  public class KaufmanEfficiencyRatio : WindowIndicator<IndicatorDataPoint>
26  {
27  private decimal _sumRoc1;
28  private decimal _periodRoc;
29  private decimal _trailingValue;
30 
31  /// <summary>
32  /// Gets a flag indicating when this indicator is ready and fully initialized
33  /// </summary>
34  public override bool IsReady => Samples >= Period;
35 
36  /// <summary>
37  /// Initializes a new instance of the <see cref="KaufmanEfficiencyRatio"/> class using the specified name and period.
38  /// </summary>
39  /// <param name="name">The name of this indicator</param>
40  /// <param name="period">The period of the Efficiency Ratio (ER)</param>
41  public KaufmanEfficiencyRatio(string name, int period)
42  : base(name, period + 1)
43  {
44  }
45 
46  /// <summary>
47  /// Initializes a new instance of the <see cref="KaufmanEfficiencyRatio"/> class using the specified period.
48  /// </summary>
49  /// <param name="period">The period of the Efficiency Ratio (ER)</param>
50  public KaufmanEfficiencyRatio(int period)
51  : this($"KER({period})", period)
52  {
53  }
54 
55  /// <summary>
56  /// Computes the next value of this indicator from the given state
57  /// </summary>
58  /// <param name="input">The input given to the indicator</param>
59  /// <param name="window">The window for the input history</param>
60  /// <returns>A new value for this indicator</returns>
62  {
63  if (Samples < Period)
64  {
65  if (Samples > 1)
66  {
67  _sumRoc1 += Math.Abs(input.Value - window[1].Value);
68  }
69 
70  return input.Value;
71  }
72 
73  if (Samples == Period)
74  {
75  _sumRoc1 += Math.Abs(input.Value - window[1].Value);
76  }
77 
78  var newTrailingValue = window[Period - 1];
79  _periodRoc = input.Value - newTrailingValue.Value;
80 
81  if (Samples > Period)
82  {
83  // Adjust sumROC1:
84  // - Remove trailing ROC1
85  // - Add new ROC1
86  _sumRoc1 -= Math.Abs(_trailingValue - newTrailingValue.Value);
87  _sumRoc1 += Math.Abs(input.Value - window[1].Value);
88  }
89 
90  _trailingValue = newTrailingValue.Value;
91 
92  // Calculate the efficiency ratio
93  return _sumRoc1 <= _periodRoc || _sumRoc1 == 0 ? 1m : Math.Abs(_periodRoc / _sumRoc1);
94  }
95 
96  /// <summary>
97  /// Resets this indicator to its initial state
98  /// </summary>
99  public override void Reset()
100  {
101  _sumRoc1 = 0;
102  _periodRoc = 0;
103  _trailingValue = 0;
104  base.Reset();
105  }
106  }
107 }