Lean  $LEAN_TAG$
ZeroLagExponentialMovingAverage.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  /// Represents the zero lag moving average indicator (ZLEMA)
22  /// ie a technical indicator that aims is to eliminate the inherent lag associated to all trend
23  /// following indicators which average a price over time.
24  /// </summary>
26  {
27  /// <summary>
28  /// An exponential moving average is used
29  /// </summary>
30  private readonly int _period;
31  private readonly ExponentialMovingAverage _ema;
32  private readonly Delay _delayedPrice;
33 
34  /// <summary>
35  /// Gets a flag indicating when this indicator is ready and fully initialized
36  /// </summary>
37  public override bool IsReady => _delayedPrice.IsReady && _ema.IsReady;
38 
39  /// <summary>
40  /// Required period, in data points, for the indicator to be ready and fully initialized.
41  /// </summary>
42  public override int WarmUpPeriod => _period + (int)Math.Floor(((float)_period) / 2);
43 
44  /// <summary>
45  /// Initializes a new instance of the ZeroLagMovingAverage class with the specified name and period
46  /// </summary>
47  /// <param name="name">The name of this indicator</param>
48  /// <param name="period">The period of the ZLEMA</param>
49  public ZeroLagExponentialMovingAverage(string name, int period)
50  : base(name, period)
51  {
52  _period = period;
53  _ema = new ExponentialMovingAverage(name + "_EMA", period);
54  _delayedPrice = new Delay((int)Math.Round((period - 1) / 2.0));
55  }
56 
57  /// <summary>
58  /// Initializes a new instance of the ZeroLagMovingAverage class with the default name and period
59  /// </summary>
60  /// <param name="period">The period of the ZLEMA</param>
62  : this($"ZLEMA({period})", period)
63  {
64  }
65 
66  /// <summary>
67  /// Resets this indicator to its initial state
68  /// </summary>
69  public override void Reset()
70  {
71  _ema.Reset();
72  _delayedPrice.Reset();
73  base.Reset();
74  }
75 
76  /// <summary>
77  /// Computes the next value for this indicator from the given state.
78  /// </summary>
79  /// <param name="window">The window of data held in this indicator</param>
80  /// <param name="input">The input value to this indicator on this time step</param>
81  /// <returns>A new value for this indicator</returns>
83  {
84  if (_delayedPrice.Update(input))
85  {
86  _ema.Update(input.Time, input.Value + (input.Value - _delayedPrice.Current));
87  return _ema.Current.Value;
88  }
89  return 0;
90  }
91  }
92 }