Lean
$LEAN_TAG$
MeanAbsoluteDeviation.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
System;
17
using
System.Linq;
18
19
namespace
QuantConnect.Indicators
20
{
21
/// <summary>
22
/// This indicator computes the n-period mean absolute deviation.
23
/// </summary>
24
public
class
MeanAbsoluteDeviation
:
WindowIndicator
<IndicatorDataPoint>,
IIndicatorWarmUpPeriodProvider
25
{
26
/// <summary>
27
/// Gets the mean used to compute the deviation
28
/// </summary>
29
public
IndicatorBase<IndicatorDataPoint>
Mean
{
get
; }
30
31
/// <summary>
32
/// Initializes a new instance of the MeanAbsoluteDeviation class with the specified period.
33
///
34
/// Evaluates the mean absolute deviation of samples in the lookback period.
35
/// </summary>
36
/// <param name="period">The sample size of the standard deviation</param>
37
public
MeanAbsoluteDeviation
(
int
period)
38
: this($
"MAD({period})"
, period)
39
{
40
}
41
42
/// <summary>
43
/// Initializes a new instance of the MeanAbsoluteDeviation class with the specified period.
44
///
45
/// Evaluates the mean absolute deviation of samples in the look-back period.
46
/// </summary>
47
/// <param name="name">The name of this indicator</param>
48
/// <param name="period">The sample size of the mean absolute deviation</param>
49
public
MeanAbsoluteDeviation
(
string
name,
int
period)
50
: base(name, period)
51
{
52
Mean
=
new
SimpleMovingAverage
($
"{name}_Mean"
, period);
53
}
54
55
/// <summary>
56
/// Gets a flag indicating when this indicator is ready and fully initialized
57
/// </summary>
58
public
override
bool
IsReady
=> Samples >=
Period
;
59
60
/// <summary>
61
/// Required period, in data points, for the indicator to be ready and fully initialized.
62
/// </summary>
63
public
int
WarmUpPeriod
=>
Period
;
64
65
/// <summary>
66
/// Computes the next value of this indicator from the given state
67
/// </summary>
68
/// <param name="input">The input given to the indicator</param>
69
/// <param name="window">The window for the input history</param>
70
/// <returns>A new value for this indicator</returns>
71
protected
override
decimal
ComputeNextValue
(
IReadOnlyWindow<IndicatorDataPoint>
window,
IndicatorDataPoint
input)
72
{
73
Mean
.Update(input);
74
return
Samples < 2 ? 0m : window.Average(v => Math.Abs(v.Value -
Mean
.Current.Value));
75
}
76
77
/// <summary>
78
/// Resets this indicator and its sub-indicator Mean to their initial state
79
/// </summary>
80
public
override
void
Reset
()
81
{
82
Mean
.Reset();
83
base.Reset();
84
}
85
}
86
}
Indicators
MeanAbsoluteDeviation.cs
Generated by
1.8.17