Lean  $LEAN_TAG$
RelativeVigorIndexSignal.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  /// The signal for the Relative Vigor Index, itself an indicator.
20  /// </summary>
22  {
23  private readonly RollingWindow<IndicatorDataPoint> _rollingRvi;
24 
25  /// <summary>
26  /// Initializes the signal term.
27  /// </summary>
28  /// <param name="name"></param>
29  protected internal RelativeVigorIndexSignal(string name)
30  : base(name) // Accessibility set to prevent out-of-scope use
31  {
32  WarmUpPeriod = 3;
33  _rollingRvi = new RollingWindow<IndicatorDataPoint>(3);
34  }
35 
36  /// <summary>
37  /// Gets a flag indicating when this indicator is ready and fully initialized
38  /// </summary>
39  public override bool IsReady => _rollingRvi.IsReady;
40 
41  /// <summary>
42  /// Resets this indicator to its initial state
43  /// </summary>
44  public int WarmUpPeriod { get; }
45 
46  /// <summary>
47  /// Computes the next value of this indicator from the given state
48  /// </summary>
49  /// <param name="input">The input given to the indicator</param>
50  /// <returns>A new value for this indicator</returns>
51  protected override decimal ComputeNextValue(IndicatorDataPoint input)
52  {
53  if (IsReady)
54  {
55  var output = (input.Value + 2 * (_rollingRvi[0] + _rollingRvi[1]) + _rollingRvi[2]) / 6;
56  _rollingRvi.Add(input);
57  return output;
58  }
59 
60  _rollingRvi.Add(input);
61  return 0m;
62  }
63 
64  /// <summary>
65  /// Resets this indicator to its initial state
66  /// </summary>
67  public override void Reset()
68  {
69  base.Reset();
70  _rollingRvi.Reset();
71  }
72  }
73 }