Lean
$LEAN_TAG$
SimpleMovingAverage.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
namespace
QuantConnect.Indicators
17
{
18
/// <summary>
19
/// Represents the traditional simple moving average indicator (SMA)
20
/// </summary>
21
public
class
SimpleMovingAverage
:
WindowIndicator
<IndicatorDataPoint>,
IIndicatorWarmUpPeriodProvider
22
{
23
/// <summary>
24
/// A rolling sum for computing the average for the given period
25
/// </summary>
26
public
IndicatorBase<IndicatorDataPoint>
RollingSum
{
get
; }
27
28
/// <summary>
29
/// Gets a flag indicating when this indicator is ready and fully initialized
30
/// </summary>
31
public
override
bool
IsReady
=>
RollingSum
.IsReady;
32
33
/// <summary>
34
/// Resets this indicator to its initial state
35
/// </summary>
36
public
override
void
Reset
()
37
{
38
RollingSum
.Reset();
39
base.Reset();
40
}
41
42
/// <summary>
43
/// Initializes a new instance of the SimpleMovingAverage 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
SimpleMovingAverage
(
string
name,
int
period)
48
: base(name, period)
49
{
50
RollingSum
=
new
Sum
(name +
"_Sum"
, period);
51
}
52
53
/// <summary>
54
/// Initializes a new instance of the SimpleMovingAverage class with the default name and period
55
/// </summary>
56
/// <param name="period">The period of the SMA</param>
57
public
SimpleMovingAverage
(
int
period)
58
: this($
"SMA({period})"
, period)
59
{
60
}
61
62
/// <summary>
63
/// Computes the next value for this indicator from the given state.
64
/// </summary>
65
/// <param name="window">The window of data held in this indicator</param>
66
/// <param name="input">The input value to this indicator on this time step</param>
67
/// <returns>A new value for this indicator</returns>
68
protected
override
decimal
ComputeNextValue
(
IReadOnlyWindow<IndicatorDataPoint>
window,
IndicatorDataPoint
input)
69
{
70
RollingSum
.Update(input.
Time
, input.
Value
);
71
return
RollingSum
.Current.Value / window.
Count
;
72
}
73
74
/// <summary>
75
/// Required period, in data points, for the indicator to be ready and fully initialized.
76
/// </summary>
77
public
int
WarmUpPeriod
=>
Period
;
78
}
79
}
Indicators
SimpleMovingAverage.cs
Generated by
1.8.17