Lean  $LEAN_TAG$
RegressionChannel.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 {
18  /// <summary>
19  /// The Regression Channel indicator extends the <see cref="LeastSquaresMovingAverage"/>
20  /// with the inclusion of two (upper and lower) channel lines that are distanced from
21  /// the linear regression line by a user defined number of standard deviations.
22  /// Reference: http://www.onlinetradingconcepts.com/TechnicalAnalysis/LinRegChannel.html
23  /// </summary>
25  {
26  /// <summary>
27  /// Gets the standard deviation
28  /// </summary>
29  private readonly IndicatorBase<IndicatorDataPoint> _standardDeviation;
30 
31  /// <summary>
32  /// Gets the linear regression
33  /// </summary>
35 
36  /// <summary>
37  /// Gets the upper channel (linear regression + k * stdDev)
38  /// </summary>
40 
41  /// <summary>
42  /// Gets the lower channel (linear regression - k * stdDev)
43  /// </summary>
45 
46  /// <summary>
47  /// The point where the regression line crosses the y-axis (price-axis)
48  /// </summary>
50 
51  /// <summary>
52  /// The regression line slope
53  /// </summary>
55 
56  /// <summary>
57  /// Gets a flag indicating when this indicator is ready and fully initialized
58  /// </summary>
59  public override bool IsReady => _standardDeviation.IsReady && LinearRegression.IsReady && UpperChannel.IsReady && LowerChannel.IsReady;
60 
61  /// <summary>
62  /// Required period, in data points, for the indicator to be ready and fully initialized.
63  /// </summary>
64  public int WarmUpPeriod { get; }
65 
66  /// <summary>
67  /// Initializes a new instance of the <see cref="RegressionChannel"/> class.
68  /// </summary>
69  /// <param name="name">The name of this indicator</param>
70  /// <param name="period">The number of data points to hold in the window</param>
71  /// <param name="k">The number of standard deviations specifying the distance between the linear regression and upper or lower channel lines</param>
72  public RegressionChannel(string name, int period, decimal k)
73  : base(name)
74  {
75  _standardDeviation = new StandardDeviation(period);
76  LinearRegression = new LeastSquaresMovingAverage(name + "_LinearRegression", period);
77  LowerChannel = LinearRegression.Minus(_standardDeviation.Times(k), name + "_LowerChannel");
78  UpperChannel = LinearRegression.Plus(_standardDeviation.Times(k), name + "_UpperChannel");
79  WarmUpPeriod = period;
80  }
81 
82  /// <summary>
83  /// Initializes a new instance of the <see cref="LeastSquaresMovingAverage"/> class.
84  /// </summary>
85  /// <param name="period">The number of data points to hold in the window.</param>
86  /// <param name="k">The number of standard deviations specifying the distance between the linear regression and upper or lower channel lines</param>
87  public RegressionChannel(int period, decimal k)
88  : this($"RC({period},{k})", period, k)
89  {
90  }
91 
92  /// <summary>
93  /// Computes the next value of this indicator from the given state
94  /// </summary>
95  /// <param name="input">The input given to the indicator</param>
96  /// <returns>
97  /// A new value for this indicator
98  /// </returns>
99  protected override decimal ComputeNextValue(IndicatorDataPoint input)
100  {
101  _standardDeviation.Update(input);
102  LinearRegression.Update(input);
103 
104  return LinearRegression.Current.Value;
105  }
106 
107  /// <summary>
108  /// Resets this indicator and all sub-indicators (StandardDeviation, LowerBand, MiddleBand, UpperBand)
109  /// </summary>
110  public override void Reset()
111  {
112  _standardDeviation.Reset();
114  LowerChannel.Reset();
115  UpperChannel.Reset();
116  base.Reset();
117  }
118  }
119 }