Lean  $LEAN_TAG$
DeMarkerIndicator.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;
18 
20 {
21  /// <summary>
22  /// In the DeMarker strategy, for some period of size N, set:
23  /// <para>
24  /// DeMax = High - Previous High, and
25  /// DeMin = Previous Low - Low
26  /// </para>
27  /// where, in the prior, if either term is less than zero (DeMax or DeMin), set it to zero.
28  /// We can now define the indicator itself, DEM, as:
29  ///<para>
30  /// DEM = MA(DeMax)/(MA(DeMax)+MA(DeMin))
31  ///</para>
32  /// where MA denotes a Moving Average of period N.
33  ///
34  /// https://www.investopedia.com/terms/d/demarkerindicator.asp
35  /// </summary>
37  {
38  private readonly IndicatorBase<IndicatorDataPoint> _maxMA;
39  private readonly IndicatorBase<IndicatorDataPoint> _minMA;
40  private decimal _lastHigh;
41  private decimal _lastLow;
42 
43  /// <summary>
44  /// Initializes a new instance of the DeMarkerIndicator class with the specified period
45  /// </summary>
46  /// <param name="period">The period of the DeMarker Indicator</param>
47  /// <param name="type">The type of moving average to use in calculations</param>
48  public DeMarkerIndicator(int period, MovingAverageType type = MovingAverageType.Simple)
49  : this($"DEM({period},{type})", period, type)
50  {
51  }
52 
53  /// <summary>
54  /// Initializes a new instance of the DeMarkerIndicator class with the specified name and period
55  /// </summary>
56  /// <param name="name">The name of this indicator</param>
57  /// <param name="period">The period of the DeMarker Indicator</param>
58  /// <param name="type">The type of moving average to use in calculations</param>
59  public DeMarkerIndicator(string name, int period, MovingAverageType type = MovingAverageType.Simple)
60  : base(name)
61  {
62  _lastHigh = 0m;
63  _lastLow = 0m;
64  WarmUpPeriod = period;
65  _maxMA = type.AsIndicator(period);
66  _minMA = type.AsIndicator(period);
67  }
68 
69  /// <summary>
70  /// Gets a flag indicating when this indicator is ready and fully initialized
71  /// </summary>
72  public override bool IsReady => _maxMA.IsReady && _minMA.IsReady;
73 
74  /// <summary>
75  /// Required period, in data points, for the indicator to be ready and fully initialized.
76  /// </summary>
77  public int WarmUpPeriod { get; }
78 
79  /// <summary>
80  /// Resets this indicator to its initial state
81  /// </summary>
82  public override void Reset()
83  {
84  _lastHigh = 0m;
85  _lastLow = 0m;
86  _maxMA.Reset();
87  _minMA.Reset();
88  base.Reset();
89  }
90 
91 
92  /// <summary>
93  /// Computes the next value of this indicator from the given state
94  /// </summary>
95  /// <param name="input">The input given to the indicator</param>
96  /// <returns>A new value for this indicator</returns>
97  protected override decimal ComputeNextValue(IBaseDataBar input)
98  {
99  var deMax = 0m;
100  var deMin = 0m;
101  if (Samples > 1)
102  {
103  // By default, DeMin and DeMax must be 0m initially
104  deMax = Math.Max(input.High - _lastHigh, 0);
105  deMin = Math.Max(_lastLow - input.Low, 0);
106  }
107 
108  _maxMA.Update(input.Time, deMax);
109  _minMA.Update(input.Time, deMin);
110  _lastHigh = input.High;
111  _lastLow = input.Low;
112 
113  if (!IsReady)
114  {
115  return 0m;
116  }
117 
118  var currentValue = _maxMA + _minMA;
119  return currentValue > 0m ? _maxMA / currentValue : 0m;
120  }
121  }
122 }