Lean  $LEAN_TAG$
McClellanOscillator.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;
18 
20 {
21  /// <summary>
22  /// The McClellan Oscillator is a market breadth indicator which was
23  /// developed by Sherman and Marian McClellan. It is based on the
24  /// difference between the number of advancing and declining periods.
25  /// </summary>
27  {
28  private readonly IndicatorBase<IndicatorDataPoint> _averageDelta;
29 
30  /// <summary>
31  /// Fast period EMA of advance decline difference
32  /// </summary>
34 
35  /// <summary>
36  /// Slow period EMA of advance decline difference
37  /// </summary>
39 
40  /// <summary>
41  /// The number of advance assets minus the number of decline assets
42  /// </summary>
44 
45  /// <summary>
46  /// Gets a flag indicating when this indicator is ready and fully initialized
47  /// </summary>
48  public override bool IsReady => EMASlow.IsReady;
49 
50  /// <summary>
51  /// Required period, in data points, for the indicator to be ready and fully initialized.
52  /// </summary>
54 
55  /// <summary>
56  /// Initializes a new instance of the <see cref="McClellanOscillator"/> class
57  /// <param name="name">The name of the indicator</param>
58  /// <param name="fastPeriod">The fast period of EMA of advance decline difference</param>
59  /// <param name="slowPeriod">The slow period of EMA of advance decline difference</param>
60  /// </summary>
61  public McClellanOscillator(string name, int fastPeriod = 19, int slowPeriod = 39) : base(name)
62  {
63  if (fastPeriod > slowPeriod)
64  {
65  throw new ArgumentException("fastPeriod must be less than slowPeriod.");
66  }
67 
69  EMAFast = ADDifference.EMA(fastPeriod);
70  EMASlow = ADDifference.EMA(slowPeriod);
71  _averageDelta = EMAFast.Minus(EMASlow);
72  }
73 
74  /// <summary>
75  /// Initializes a new instance of the <see cref="McClellanOscillator"/> class
76  /// <param name="fastPeriod">The fast period of EMA of advance decline difference</param>
77  /// <param name="slowPeriod">The slow period of EMA of advance decline difference</param>
78  /// </summary>
79  public McClellanOscillator(int fastPeriod = 19, int slowPeriod = 39)
80  : this("McClellanOscillator", fastPeriod, slowPeriod)
81  {
82  }
83 
84  /// <summary>
85  /// Computes the next value of this indicator from the given state
86  /// </summary>
87  /// <param name="input">The input given to the indicator</param>
88  /// <returns>A new value for this indicator</returns>
89  protected override decimal ComputeNextValue(TradeBar input)
90  {
91  ADDifference.Update(input);
92 
93  return _averageDelta.Current.Value;
94  }
95 
96  /// <summary>
97  /// Resets this indicator to its initial state
98  /// </summary>
99  public override void Reset()
100  {
102  EMAFast.Reset();
103  EMASlow.Reset();
104  _averageDelta.Reset();
105 
106  base.Reset();
107  }
108 
109  /// <summary>
110  /// Add Tracking asset issue
111  /// </summary>
112  /// <param name="asset">the tracking asset issue</param>
113  public void Add(Symbol asset)
114  {
115  ADDifference.Add(asset);
116  }
117 
118  /// <summary>
119  /// Remove Tracking asset issue
120  /// </summary>
121  /// <param name="asset">the tracking asset issue</param>
122  public void Remove(Symbol asset)
123  {
124  ADDifference.Remove(asset);
125  }
126  }
127 }