Lean  $LEAN_TAG$
TripleExponentialMovingAverage.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 Triple Exponential Moving Average (TEMA).
20  /// The Triple Exponential Moving Average is calculated with the following formula:
21  /// EMA1 = EMA(t,period)
22  /// EMA2 = EMA(EMA(t,period),period)
23  /// EMA3 = EMA(EMA(EMA(t,period),period),period)
24  /// TEMA = 3 * EMA1 - 3 * EMA2 + EMA3
25  /// </summary>
27  {
28  private readonly int _period;
29  private readonly ExponentialMovingAverage _ema1;
30  private readonly ExponentialMovingAverage _ema2;
31  private readonly ExponentialMovingAverage _ema3;
32 
33  /// <summary>
34  /// Initializes a new instance of the <see cref="TripleExponentialMovingAverage"/> class using the specified name and period.
35  /// </summary>
36  /// <param name="name">The name of this indicator</param>
37  /// <param name="period">The period of the TEMA</param>
38  public TripleExponentialMovingAverage(string name, int period)
39  : base(name)
40  {
41  _period = period;
42  _ema1 = new ExponentialMovingAverage(name + "_1", period);
43  _ema2 = new ExponentialMovingAverage(name + "_2", period);
44  _ema3 = new ExponentialMovingAverage(name + "_3", period);
45  }
46 
47  /// <summary>
48  /// Initializes a new instance of the <see cref="TripleExponentialMovingAverage"/> class using the specified period.
49  /// </summary>
50  /// <param name="period">The period of the TEMA</param>
51  public TripleExponentialMovingAverage(int period)
52  : this($"TEMA({period})", period)
53  {
54  }
55 
56  /// <summary>
57  /// Gets a flag indicating when this indicator is ready and fully initialized
58  /// </summary>
59  public override bool IsReady => _ema3.IsReady;
60 
61  /// <summary>
62  /// Required period, in data points, for the indicator to be ready and fully initialized.
63  /// </summary>
64  public int WarmUpPeriod => 3 * (_period - 1) + 1;
65 
66  /// <summary>
67  /// Computes the next value of this indicator from the given state
68  /// </summary>
69  /// <param name="input">The input given to the indicator</param>
70  /// <returns>A new value for this indicator</returns>
71  protected override decimal ComputeNextValue(IndicatorDataPoint input)
72  {
73  _ema1.Update(input);
74 
75  if (_ema1.IsReady)
76  _ema2.Update(_ema1.Current);
77 
78  if (_ema2.IsReady)
79  _ema3.Update(_ema2.Current);
80 
81  return IsReady ? 3m * _ema1.Current.Value - 3m * _ema2.Current.Value + _ema3.Current.Value : 0m;
82  }
83 
84  /// <summary>
85  /// Resets this indicator to its initial state
86  /// </summary>
87  public override void Reset()
88  {
89  _ema1.Reset();
90  _ema2.Reset();
91  _ema3.Reset();
92  base.Reset();
93  }
94  }
95 }