Lean  $LEAN_TAG$
TriangularMovingAverage.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 Triangular Moving Average (TRIMA).
20  /// The Triangular Moving Average is calculated with the following formula:
21  /// (1) When the period is even, TRIMA(x,period)=SMA(SMA(x,period/2),(period/2)+1)
22  /// (2) When the period is odd, TRIMA(x,period)=SMA(SMA(x,(period+1)/2),(period+1)/2)
23  /// </summary>
25  {
26  private readonly int _period;
27  private readonly SimpleMovingAverage _sma1;
28  private readonly SimpleMovingAverage _sma2;
29 
30  /// <summary>
31  /// Initializes a new instance of the <see cref="TriangularMovingAverage"/> class using the specified name and period.
32  /// </summary>
33  /// <param name="name">The name of this indicator</param>
34  /// <param name="period">The period of the indicator</param>
35  public TriangularMovingAverage(string name, int period)
36  : base(name)
37  {
38  _period = period;
39 
40  var periodSma1 = period % 2 == 0 ? period / 2 : (period + 1) / 2;
41  var periodSma2 = period % 2 == 0 ? period / 2 + 1 : (period + 1) / 2;
42 
43  _sma1 = new SimpleMovingAverage(name + "_1", periodSma1);
44  _sma2 = new SimpleMovingAverage(name + "_2", periodSma2);
45  }
46 
47  /// <summary>
48  /// Initializes a new instance of the <see cref="TriangularMovingAverage"/> class using the specified period.
49  /// </summary>
50  /// <param name="period">The period of the indicator</param>
51  public TriangularMovingAverage(int period)
52  : this($"TRIMA({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 => Samples >= _period;
60 
61  /// <summary>
62  /// Required period, in data points, for the indicator to be ready and fully initialized.
63  /// </summary>
64  public int WarmUpPeriod => _period;
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  _sma1.Update(input);
74  _sma2.Update(_sma1.Current);
75 
76  return _sma2.Current.Value;
77  }
78 
79  /// <summary>
80  /// Resets this indicator to its initial state
81  /// </summary>
82  public override void Reset()
83  {
84  _sma1.Reset();
85  _sma2.Reset();
86  base.Reset();
87  }
88  }
89 }