Lean
$LEAN_TAG$
NormalizedAverageTrueRange.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
.
Market
;
17
18
namespace
QuantConnect.Indicators
19
{
20
/// <summary>
21
/// This indicator computes the Normalized Average True Range (NATR).
22
/// The Normalized Average True Range is calculated with the following formula:
23
/// NATR = (ATR(period) / Close) * 100
24
/// </summary>
25
public
class
NormalizedAverageTrueRange
:
BarIndicator
,
IIndicatorWarmUpPeriodProvider
26
{
27
private
readonly
int
_period;
28
private
readonly
TrueRange
_tr;
29
private
readonly
AverageTrueRange
_atr;
30
private
decimal _lastAtrValue;
31
32
/// <summary>
33
/// Initializes a new instance of the <see cref="NormalizedAverageTrueRange"/> class using the specified name and period.
34
/// </summary>
35
/// <param name="name">The name of this indicator</param>
36
/// <param name="period">The period of the NATR</param>
37
public
NormalizedAverageTrueRange
(
string
name,
int
period) :
38
base(name)
39
{
40
_period = period;
41
_tr =
new
TrueRange
(name +
"_TR"
);
42
_atr =
new
AverageTrueRange
(name +
"_ATR"
, period,
MovingAverageType
.Simple);
43
}
44
45
/// <summary>
46
/// Initializes a new instance of the <see cref="NormalizedAverageTrueRange"/> class using the specified period.
47
/// </summary>
48
/// <param name="period">The period of the NATR</param>
49
public
NormalizedAverageTrueRange
(
int
period)
50
: this($
"NATR({period})"
, period)
51
{
52
}
53
54
/// <summary>
55
/// Gets a flag indicating when this indicator is ready and fully initialized
56
/// </summary>
57
public
override
bool
IsReady
=> Samples > _period;
58
59
/// <summary>
60
/// Required period, in data points, for the indicator to be ready and fully initialized.
61
/// </summary>
62
public
int
WarmUpPeriod
=> _period + 1;
63
64
/// <summary>
65
/// Computes the next value of this indicator from the given state
66
/// </summary>
67
/// <param name="input">The input given to the indicator</param>
68
/// <returns>A new value for this indicator</returns>
69
protected
override
decimal
ComputeNextValue
(
IBaseDataBar
input)
70
{
71
_tr.Update(input);
72
73
if
(!
IsReady
)
74
{
75
_atr.Update(input);
76
return
input.
Close
!= 0 ? _atr.Current.Value / input.
Close
* 100 : 0m;
77
}
78
79
if
(Samples == _period + 1)
80
{
81
// first output value is SMA of TrueRange
82
_atr.Update(input);
83
_lastAtrValue = _atr.Current.Value;
84
}
85
else
86
{
87
// next TrueRange values are smoothed using Wilder's approach
88
_lastAtrValue = (_lastAtrValue * (_period - 1) + _tr.Current.Value) / _period;
89
}
90
91
return
input.
Close
!= 0 ? _lastAtrValue / input.
Close
* 100 : 0m;
92
}
93
94
/// <summary>
95
/// Resets this indicator to its initial state
96
/// </summary>
97
public
override
void
Reset
()
98
{
99
_tr.Reset();
100
_atr.Reset();
101
base.Reset();
102
}
103
}
104
}
Indicators
NormalizedAverageTrueRange.cs
Generated by
1.8.17