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