Lean  $LEAN_TAG$
CoppockCurve.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 System;
17 
19 {
20  /// <summary>
21  /// A momentum indicator developed by Edwin “Sedge” Coppock in October 1965.
22  /// The goal of this indicator is to identify long-term buying opportunities in the S&amp;P500 and Dow Industrials.
23  /// Source: http://stockcharts.com/school/doku.php?id=chart_school:technical_indicators:coppock_curve
24  /// </summary>
25  public class CoppockCurve : IndicatorBase<IndicatorDataPoint>, IIndicatorWarmUpPeriodProvider
26  {
27  private readonly RateOfChangePercent _longRoc;
28  private readonly LinearWeightedMovingAverage _lwma;
29  private readonly RateOfChangePercent _shortRoc;
30 
31  /// <summary>
32  /// Gets a flag indicating when this indicator is ready and fully initialized
33  /// </summary>
34  public override bool IsReady => _lwma.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="CoppockCurve" /> indicator with its default values.
43  /// </summary>
44  public CoppockCurve()
45  : this(11,14,10)
46  {
47  }
48 
49  /// <summary>
50  /// Initializes a new instance of the <see cref="CoppockCurve"/> indicator
51  /// </summary>
52  /// <param name="shortRocPeriod">The period for the short ROC</param>
53  /// <param name="longRocPeriod">The period for the long ROC</param>
54  /// <param name="lwmaPeriod">The period for the LWMA</param>
55  public CoppockCurve(int shortRocPeriod, int longRocPeriod, int lwmaPeriod)
56  : this($"CC({shortRocPeriod},{longRocPeriod},{lwmaPeriod})", shortRocPeriod, longRocPeriod, lwmaPeriod)
57  {
58  }
59 
60  /// <summary>
61  /// Initializes a new instance of the <see cref="CoppockCurve" /> indicator
62  /// </summary>
63  /// <param name="name">A name for the indicator</param>
64  /// <param name="shortRocPeriod">The period for the short ROC</param>
65  /// <param name="longRocPeriod">The period for the long ROC</param>
66  /// <param name="lwmaPeriod">The period for the LWMA</param>
67  public CoppockCurve(string name, int shortRocPeriod, int longRocPeriod, int lwmaPeriod)
68  : base(name)
69  {
70  _shortRoc = new RateOfChangePercent(shortRocPeriod);
71  _longRoc = new RateOfChangePercent(longRocPeriod);
72  _lwma = new LinearWeightedMovingAverage(lwmaPeriod);
73 
74  // Define our warmup
75  // LWMA does not get updated until ROC are warmed up and ready, so add our periods.
76  // Then minus 1 because on the same point ROC is ready LWMA will receive its first point.
77  WarmUpPeriod = Math.Max(_shortRoc.WarmUpPeriod, _longRoc.WarmUpPeriod) + lwmaPeriod - 1;
78  }
79 
80  /// <summary>
81  /// Resets this indicator to its initial state
82  /// </summary>
83  public override void Reset()
84  {
85  base.Reset();
86  _shortRoc.Reset();
87  _longRoc.Reset();
88  _lwma.Reset();
89  }
90 
91  /// <summary>
92  /// Computes the next value of this indicator from the given state
93  /// </summary>
94  /// <param name="input">The input given to the indicator</param>
95  /// <returns>A new value for this indicator</returns>
96  protected override decimal ComputeNextValue(IndicatorDataPoint input)
97  {
98  _shortRoc.Update(input);
99  _longRoc.Update(input);
100  if (!_longRoc.IsReady || !_shortRoc.IsReady)
101  {
102  return decimal.Zero;
103  }
104  _lwma.Update(input.Time, _shortRoc.Current.Value + _longRoc.Current.Value);
105  return _lwma.Current.Value;
106  }
107  }
108 }