Lean  $LEAN_TAG$
WindowIndicator.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 
16 using QuantConnect.Data;
17 
19 {
20  /// <summary>
21  /// Represents an indicator that acts on a rolling window of data
22  /// </summary>
24  where T : IBaseData
25  {
26  // a window of data over a certain look back period
27  private readonly RollingWindow<T> _window;
28 
29  /// <summary>
30  /// Gets the period of this window indicator
31  /// </summary>
32  public int Period => _window.Size;
33 
34  /// <summary>
35  /// Initializes a new instance of the WindowIndicator class
36  /// </summary>
37  /// <param name="name">The name of this indicator</param>
38  /// <param name="period">The number of data points to hold in the window</param>
39  protected WindowIndicator(string name, int period)
40  : base(name)
41  {
42  _window = new RollingWindow<T>(period);
43  }
44 
45  /// <summary>
46  /// Gets a flag indicating when this indicator is ready and fully initialized
47  /// </summary>
48  public override bool IsReady => _window.IsReady;
49 
50  /// <summary>
51  /// Required period, in data points, to the indicator to be ready and fully initialized
52  /// </summary>
53  public virtual int WarmUpPeriod => Period;
54 
55  /// <summary>
56  /// Computes the next value of this indicator from the given state
57  /// </summary>
58  /// <param name="input">The input given to the indicator</param>
59  /// <returns>A new value for this indicator</returns>
60  protected override decimal ComputeNextValue(T input)
61  {
62  _window.Add(input);
63  return ComputeNextValue(_window, input);
64  }
65 
66  /// <summary>
67  /// Resets this indicator to its initial state
68  /// </summary>
69  public override void Reset()
70  {
71  base.Reset();
72  _window.Reset();
73  }
74 
75  /// <summary>
76  /// Computes the next value for this indicator from the given state.
77  /// </summary>
78  /// <param name="window">The window of data held in this indicator</param>
79  /// <param name="input">The input value to this indicator on this time step</param>
80  /// <returns>A new value for this indicator</returns>
81  protected abstract decimal ComputeNextValue(IReadOnlyWindow<T> window, T input);
82  }
83 }