Lean  $LEAN_TAG$
Trix.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 TRIX (1-period ROC of a Triple EMA)
20  /// The TRIX is calculated as explained here:
21  /// http://stockcharts.com/school/doku.php?id=chart_school:technical_indicators:trix
22  /// </summary>
24  {
25  private readonly int _period;
26  private readonly ExponentialMovingAverage _ema1;
27  private readonly ExponentialMovingAverage _ema2;
28  private readonly ExponentialMovingAverage _ema3;
29  private readonly RateOfChangePercent _roc;
30 
31  /// <summary>
32  /// Initializes a new instance of the <see cref="Trix"/> class using the specified name and period.
33  /// </summary>
34  /// <param name="name">The name of this indicator</param>
35  /// <param name="period">The period of the indicator</param>
36  public Trix(string name, int period)
37  : base(name)
38  {
39  _period = period;
40  _ema1 = new ExponentialMovingAverage(name + "_1", period);
41  _ema2 = new ExponentialMovingAverage(name + "_2", period);
42  _ema3 = new ExponentialMovingAverage(name + "_3", period);
43  _roc = new RateOfChangePercent(name + "_ROCP1", 1);
44  }
45 
46  /// <summary>
47  /// Initializes a new instance of the <see cref="Trix"/> class using the specified period.
48  /// </summary>
49  /// <param name="period">The period of the indicator</param>
50  public Trix(int period)
51  : this($"TRIX({period})", period)
52  {
53  }
54 
55  /// <summary>
56  /// Gets a flag indicating when this indicator is ready and fully initialized
57  /// </summary>
58  public override bool IsReady => _roc.IsReady;
59 
60  /// <summary>
61  /// Required period, in data points, for the indicator to be ready and fully initialized.
62  /// We have 3 EMAs chained on base period so every _period points starts the next EMA,
63  /// hence -1 on the multiplication, and finally the last ema updates our _roc which needs
64  /// to be warmed up before this indicator is warmed up.
65  /// </summary>
66  public int WarmUpPeriod => 3 * (_period - 1) + _roc.WarmUpPeriod;
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  _ema1.Update(input);
76 
77  if (_ema1.IsReady)
78  _ema2.Update(_ema1.Current);
79 
80  if (_ema2.IsReady)
81  _ema3.Update(_ema2.Current);
82 
83  if (_ema3.IsReady)
84  _roc.Update(_ema3.Current);
85 
86  return _roc.Current.Value;
87  }
88 
89  /// <summary>
90  /// Resets this indicator to its initial state
91  /// </summary>
92  public override void Reset()
93  {
94  _ema1.Reset();
95  _ema2.Reset();
96  _ema3.Reset();
97  _roc.Reset();
98  base.Reset();
99  }
100  }
101 }