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