Lean  $LEAN_TAG$
DerivativeOscillator.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  /// Represents the Derivative Oscillator Indicator, utilizing
20  /// a moving average convergence-divergence (MACD) histogram to a double-smoothed relative strength index (RSI).
21  /// </summary>
23  {
24  private readonly RelativeStrengthIndex _rsi;
25  private readonly ExponentialMovingAverage _smoothedRsi;
26  private readonly ExponentialMovingAverage _doubleSmoothedRsi;
27  private readonly SimpleMovingAverage _signalLine;
28 
29  /// <summary>
30  /// Gets a flag indicating when this indicator is ready and fully initialized
31  /// </summary>
32  public override bool IsReady => _signalLine.IsReady;
33 
34  /// <summary>
35  /// Required period, in data points, for the indicator to be ready and fully initialized.
36  /// </summary>
37  public int WarmUpPeriod { get; }
38 
39  /// <summary>
40  /// Initializes a new instance of the IndicatorDerivativeOscillator class with the specified name and periods.
41  /// </summary>
42  /// <param name="name">The name of the indicator</param>
43  /// <param name="rsiPeriod">The period for the RSI calculation</param>
44  /// <param name="smoothingRsiPeriod">The period for the smoothing RSI</param>
45  /// <param name="doubleSmoothingRsiPeriod">The period for the double smoothing RSI</param>
46  /// <param name="signalLinePeriod">The period for the signal line</param>
47  public DerivativeOscillator(string name, int rsiPeriod, int smoothingRsiPeriod, int doubleSmoothingRsiPeriod, int signalLinePeriod) : base(name)
48  {
49  _rsi = new RelativeStrengthIndex($"{name}_RSI", rsiPeriod);
50  _smoothedRsi = new ExponentialMovingAverage($"{name}_SmoothedRSI", smoothingRsiPeriod).Of(_rsi);
51  _doubleSmoothedRsi = new ExponentialMovingAverage($"{name}_DoubleSmoothedRSI", doubleSmoothingRsiPeriod).Of(_smoothedRsi);
52  _signalLine = new SimpleMovingAverage($"{name}_SignalLine", signalLinePeriod).Of(_doubleSmoothedRsi);
53  WarmUpPeriod = (rsiPeriod + smoothingRsiPeriod + doubleSmoothingRsiPeriod + signalLinePeriod - 3) + 1;
54  }
55 
56  /// <summary>
57  /// Computes the next value for the derivative oscillator indicator from the given state
58  /// </summary>
59  /// <param name="input">The input value to this indicator on this time step</param>
60  /// <returns>A new value for this indicator</returns>
61  protected override decimal ComputeNextValue(IndicatorDataPoint input)
62  {
63  // Chaining updates all of the other indicators
64  _rsi.Update(input);
65 
66  if (!IsReady)
67  {
68  return 0;
69  }
70 
71  return _doubleSmoothedRsi.Current.Value - _signalLine.Current.Value;
72  }
73 
74  /// <summary>
75  /// Resets this indicator to its initial state
76  /// </summary>
77  public override void Reset()
78  {
79  _rsi.Reset();
80  _smoothedRsi.Reset();
81  _doubleSmoothedRsi.Reset();
82  _signalLine.Reset();
83  base.Reset();
84  }
85  }
86 }