Lean
$LEAN_TAG$
DonchianChannel.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
using
System;
18
19
namespace
QuantConnect.Indicators
20
{
21
/// <summary>
22
/// This indicator computes the upper and lower band of the Donchian Channel.
23
/// The upper band is computed by finding the highest high over the given period.
24
/// The lower band is computed by finding the lowest low over the given period.
25
/// The primary output value of the indicator is the mean of the upper and lower band for
26
/// the given timeframe.
27
/// </summary>
28
public
class
DonchianChannel
:
BarIndicator
,
IIndicatorWarmUpPeriodProvider
29
{
30
private
IBaseDataBar
_previousInput;
31
32
/// <summary>
33
/// Gets the upper band of the Donchian Channel.
34
/// </summary>
35
public
IndicatorBase<IndicatorDataPoint>
UpperBand
{
get
; }
36
37
/// <summary>
38
/// Gets the lower band of the Donchian Channel.
39
/// </summary>
40
public
IndicatorBase<IndicatorDataPoint>
LowerBand
{
get
; }
41
42
/// <summary>
43
/// Initializes a new instance of the <see cref="DonchianChannel"/> class.
44
/// </summary>
45
/// <param name="period">The period for both the upper and lower channels.</param>
46
public
DonchianChannel
(
int
period)
47
: this(period, period)
48
{
49
}
50
51
/// <summary>
52
/// Initializes a new instance of the <see cref="DonchianChannel"/> class.
53
/// </summary>
54
/// <param name="upperPeriod">The period for the upper channel.</param>
55
/// <param name="lowerPeriod">The period for the lower channel</param>
56
public
DonchianChannel
(
int
upperPeriod,
int
lowerPeriod)
57
: this($
"DCH({lowerPeriod},{lowerPeriod})"
, upperPeriod, lowerPeriod)
58
{
59
}
60
61
/// <summary>
62
/// Initializes a new instance of the <see cref="DonchianChannel"/> class.
63
/// </summary>
64
/// <param name="name">The name.</param>
65
/// <param name="period">The period for both the upper and lower channels.</param>
66
public
DonchianChannel
(
string
name,
int
period)
67
: this(name, period, period)
68
{
69
}
70
71
/// <summary>
72
/// Initializes a new instance of the <see cref="DonchianChannel"/> class.
73
/// </summary>
74
/// <param name="name">The name.</param>
75
/// <param name="upperPeriod">The period for the upper channel.</param>
76
/// <param name="lowerPeriod">The period for the lower channel</param>
77
public
DonchianChannel
(
string
name,
int
upperPeriod,
int
lowerPeriod)
78
: base(name)
79
{
80
WarmUpPeriod
= Math.Max(upperPeriod, lowerPeriod);
81
UpperBand
=
new
Maximum
(name +
"_UpperBand"
, upperPeriod);
82
LowerBand
=
new
Minimum
(name +
"_LowerBand"
, lowerPeriod);
83
}
84
85
/// <summary>
86
/// Gets a flag indicating when this indicator is ready and fully initialized
87
/// </summary>
88
public
override
bool
IsReady
=>
UpperBand
.IsReady &&
LowerBand
.IsReady;
89
90
/// <summary>
91
/// Required period, in data points, for the indicator to be ready and fully initialized.
92
/// </summary>
93
public
int
WarmUpPeriod
{
get
; }
94
95
/// <summary>
96
/// Computes the next value of this indicator from the given state
97
/// </summary>
98
/// <param name="input">The input given to the indicator</param>
99
/// <returns>A new value for this indicator, which by convention is the mean value of the upper band and lower band.</returns>
100
protected
override
decimal
ComputeNextValue
(
IBaseDataBar
input)
101
{
102
UpperBand
.Update(input.
Time
, input.
High
);
103
LowerBand
.Update(input.
Time
, input.
Low
);
104
return
(
UpperBand
+
LowerBand
) / 2;
105
}
106
107
/// <summary>
108
/// Resets this indicator to its initial state
109
/// </summary>
110
public
override
void
Reset
()
111
{
112
base.Reset();
113
UpperBand
.Reset();
114
LowerBand
.Reset();
115
}
116
}
117
}
Indicators
DonchianChannel.cs
Generated by
1.8.17