Lean  $LEAN_TAG$
DetrendedPriceOscillator.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 Detrended Price Oscillator is an indicator designed to remove trend from price
20  /// and make it easier to identify cycles.
21  /// DPO does not extend to the last date because it is based on a displaced moving average.
22  /// Is estimated as Price {X/2 + 1} periods ago less the X-period simple moving average.
23  /// E.g.DPO(20) equals price 11 days ago less the 20-day SMA.
24  /// </summary>
25  /// <seealso cref="IndicatorBase{IndicatorDataPoint}" />
27  {
28  private readonly Delay _priceLag;
29  private readonly SimpleMovingAverage _sma;
30 
31  /// <summary>
32  /// Gets a flag indicating when this indicator is ready and fully initialized
33  /// </summary>
34  public override bool IsReady => _sma.IsReady && _priceLag.IsReady;
35 
36  /// <summary>
37  /// Required period, in data points, for the indicator to be ready and fully initialized.
38  /// </summary>
39  public int WarmUpPeriod { get; }
40 
41  /// <summary>
42  /// Initializes a new instance of the <see cref="DetrendedPriceOscillator" /> class.
43  /// </summary>
44  /// <param name="name">The name for the indicator.</param>
45  /// <param name="period">The number of periods to calculate the DPO.</param>
46  public DetrendedPriceOscillator(string name, int period)
47  : base(name)
48  {
49  var lagPeriod = period / 2 + 1;
50  _priceLag = new Delay(lagPeriod);
51  _sma = new SimpleMovingAverage(period);
52  WarmUpPeriod = period;
53  }
54 
55  /// <summary>
56  /// Initializes a new instance of the <see cref="DetrendedPriceOscillator" /> class.
57  /// </summary>
58  /// <param name="period">The number of periods to calculate the DPO.</param>
59  public DetrendedPriceOscillator(int period)
60  : this($"DPO({period})", period)
61  {
62  }
63 
64  /// <summary>
65  /// Resets this indicator to its initial state
66  /// </summary>
67  public override void Reset()
68  {
69  base.Reset();
70  _priceLag.Reset();
71  _sma.Reset();
72  }
73 
74  /// <summary>
75  /// Computes the next value of this indicator from the given state
76  /// </summary>
77  /// <param name="input">The input given to the indicator</param>
78  /// <returns>
79  /// A new value for this indicator
80  /// </returns>
81  protected override decimal ComputeNextValue(IndicatorDataPoint input)
82  {
83  _priceLag.Update(input);
84  _sma.Update(input);
85  return _priceLag.Current.Value - _sma.Current.Value;
86  }
87  }
88 }