Lean  $LEAN_TAG$
RelativeMovingAverage.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  /// Represents the relative moving average indicator (RMA).
20  /// RMA = SMA(3 x Period) - SMA(2 x Period) + SMA(1 x Period) per formula:
21  /// https://www.hybrid-solutions.com/plugins/client-vtl-plugins/free/rma.html
22  /// </summary>
24  {
25  /// <summary>
26  /// Gets the Short Term SMA with 1 x Period of RMA
27  /// </summary>
29 
30  /// <summary>
31  /// Gets the Medium Term SMA with 2 x Period of RMA
32  /// </summary>
34 
35  /// <summary>
36  /// Gets the Long Term SMA with 3 x Period of RMA
37  /// </summary>
39 
40  /// <summary>
41  /// Gets a flag indicating when this indicator is ready and fully initialized
42  /// </summary>
43  public override bool IsReady => LongAverage.IsReady;
44 
45  /// <summary>
46  /// Required period, in data points, for the indicator to be ready and fully initialized.
47  /// </summary>
49 
50  /// <summary>
51  /// Initializes a new instance of the RelativeMovingAverage class with the specified name and period
52  /// </summary>
53  /// <param name="name">The name of this indicator</param>
54  /// <param name="period">The period of the RMA</param>
55  public RelativeMovingAverage(string name, int period)
56  : base(name)
57  {
58  ShortAverage = new SimpleMovingAverage(name + "_Short", period);
59  MediumAverage = new SimpleMovingAverage(name + "_Medium", period * 2);
60  LongAverage = new SimpleMovingAverage(name + "_Long", period * 3);
61  }
62 
63  /// <summary>
64  /// Initializes a new instance of the SimpleMovingAverage class with the default name and period
65  /// </summary>
66  /// <param name="period"></param>
67  public RelativeMovingAverage(int period)
68  : this($"RMA({period})", period)
69  {
70  }
71 
72  /// <summary>
73  /// Copmutes the next value for this indicator from the given state.
74  /// </summary>
75  /// <param name="input">The input value to this indicator on this time step</param>
76  /// <returns>A new value for this indicator</returns>
77  protected override decimal ComputeNextValue(IndicatorDataPoint input)
78  {
79  ShortAverage.Update(input);
80  MediumAverage.Update(input);
81  LongAverage.Update(input);
82 
83  return LongAverage.Current.Value - MediumAverage.Current.Value + ShortAverage.Current.Value;
84  }
85 
86  /// <summary>
87  /// Resets this indicator to its initial state
88  /// </summary>
89  public override void Reset()
90  {
91  base.Reset();
95  }
96  }
97 }