Lean  $LEAN_TAG$
ChandeMomentumOscillator.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  /// This indicator computes the Chande Momentum Oscillator (CMO).
20  /// CMO calculation is mostly identical to RSI.
21  /// The only difference is in the last step of calculation:
22  /// RSI = gain / (gain+loss)
23  /// CMO = (gain-loss) / (gain+loss)
24  /// </summary>
26  {
27  private decimal _prevValue;
28  private decimal _prevGain;
29  private decimal _prevLoss;
30 
31  /// <summary>
32  /// Initializes a new instance of the <see cref="ChandeMomentumOscillator"/> class using the specified period.
33  /// </summary>
34  /// <param name="period">The period of the indicator</param>
35  public ChandeMomentumOscillator(int period)
36  : this($"CMO({period})", period)
37  {
38  }
39 
40  /// <summary>
41  /// Initializes a new instance of the <see cref="ChandeMomentumOscillator"/> class using the specified name and period.
42  /// </summary>
43  /// <param name="name">The name of this indicator</param>
44  /// <param name="period">The period of the indicator</param>
45  public ChandeMomentumOscillator(string name, int period)
46  : base(name, period)
47  {
48  }
49 
50  /// <summary>
51  /// Gets a flag indicating when this indicator is ready and fully initialized
52  /// </summary>
53  public override bool IsReady => Samples > Period;
54 
55  /// <summary>
56  /// Required period, in data points, for the indicator to be ready and fully initialized.
57  /// </summary>
58  public int WarmUpPeriod => 1 + Period;
59 
60  /// <summary>
61  /// Computes the next value of this indicator from the given state
62  /// </summary>
63  /// <param name="input">The input given to the indicator</param>
64  /// <param name="window">The window for the input history</param>
65  /// <returns>A new value for this indicator</returns>
67  {
68  if (Samples == 1)
69  {
70  _prevValue = input.Value;
71  return 0m;
72  }
73 
74  var difference = input.Value - _prevValue;
75 
76  _prevValue = input.Value;
77 
78  if (Samples > Period + 1)
79  {
80  _prevLoss *= (Period - 1);
81  _prevGain *= (Period - 1);
82  }
83 
84  if (difference < 0)
85  _prevLoss -= difference;
86  else
87  _prevGain += difference;
88 
89  if (!IsReady)
90  return 0m;
91 
92  _prevLoss /= Period;
93  _prevGain /= Period;
94 
95  var sum = _prevGain + _prevLoss;
96  return sum != 0 ? 100m * ((_prevGain - _prevLoss) / sum) : 0m;
97  }
98 
99  /// <summary>
100  /// Resets this indicator to its initial state
101  /// </summary>
102  public override void Reset()
103  {
104  _prevValue = 0;
105  _prevGain = 0;
106  _prevLoss = 0;
107  base.Reset();
108  }
109  }
110 }