Lean  $LEAN_TAG$
Sum.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 an indicator capable of tracking the sum for the given period
20  /// </summary>
21  public class Sum : WindowIndicator<IndicatorDataPoint>, IIndicatorWarmUpPeriodProvider
22  {
23  /// <summary>
24  /// The sum for the given period
25  /// </summary>
26  private decimal _sum;
27 
28  /// <summary>
29  /// Required period, in data points, for the indicator to be ready and fully initialized.
30  /// </summary>
31  public int WarmUpPeriod => Period;
32 
33  /// <summary>
34  /// Resets this indicator to its initial state
35  /// </summary>
36  public override void Reset()
37  {
38  _sum = 0.0m;
39  base.Reset();
40  }
41 
42  /// <summary>
43  /// Initializes a new instance of the Sum class with the specified name and period
44  /// </summary>
45  /// <param name="name">The name of this indicator</param>
46  /// <param name="period">The period of the SMA</param>
47  public Sum(string name, int period)
48  : base(name, period)
49  {
50  }
51 
52  /// <summary>
53  /// Initializes a new instance of the Sum class with the default name and period
54  /// </summary>
55  /// <param name="period">The period of the SMA</param>
56  public Sum(int period)
57  : this($"SUM({period})", period)
58  {
59  }
60 
61  /// <summary>
62  /// Computes the next value for this indicator from the given state.
63  /// </summary>
64  /// <param name="window">The window of data held in this indicator</param>
65  /// <param name="input">The input value to this indicator on this time step</param>
66  /// <returns>A new value for this indicator</returns>
68  {
69  _sum += input.Value;
70  if (window.Samples > window.Size)
71  {
72  _sum -= window.MostRecentlyRemoved.Value;
73  }
74  return _sum;
75  }
76  }
77 }