Lean  $LEAN_TAG$
UltimateOscillator.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  /// This indicator computes the Ultimate Oscillator (ULTOSC)
23  /// The Ultimate Oscillator is calculated as explained here:
24  /// http://stockcharts.com/school/doku.php?id=chart_school:technical_indicators:ultimate_oscillator
25  /// </summary>
27  {
28  private readonly int _period;
29  private IBaseDataBar _previousInput;
30  private readonly TrueRange _trueRange;
31  private readonly Sum _sumBuyingPressure1;
32  private readonly Sum _sumBuyingPressure2;
33  private readonly Sum _sumBuyingPressure3;
34  private readonly Sum _sumTrueRange1;
35  private readonly Sum _sumTrueRange2;
36  private readonly Sum _sumTrueRange3;
37 
38  /// <summary>
39  /// Initializes a new instance of the <see cref="UltimateOscillator"/> class using the specified parameters
40  /// </summary>
41  /// <param name="period1">The first period</param>
42  /// <param name="period2">The second period</param>
43  /// <param name="period3">The third period</param>
44  public UltimateOscillator(int period1, int period2, int period3)
45  : this($"ULTOSC({period1},{period2},{period3})", period1, period2, period3)
46  {
47  }
48 
49  /// <summary>
50  /// Initializes a new instance of the <see cref="UltimateOscillator"/> class using the specified parameters
51  /// </summary>
52  /// <param name="name">The name of this indicator</param>
53  /// <param name="period1">The first period</param>
54  /// <param name="period2">The second period</param>
55  /// <param name="period3">The third period</param>
56  public UltimateOscillator(string name, int period1, int period2, int period3)
57  : base(name)
58  {
59  _period = Math.Max(Math.Max(period1, period2), period3);
60  _trueRange = new TrueRange(name + "_TR");
61  _sumBuyingPressure1 = new Sum(name + "_BP1", period1);
62  _sumBuyingPressure2 = new Sum(name + "_BP2", period2);
63  _sumBuyingPressure3 = new Sum(name + "_BP3", period3);
64  _sumTrueRange1 = new Sum(name + "_TR1", period1);
65  _sumTrueRange2 = new Sum(name + "_TR2", period2);
66  _sumTrueRange3 = new Sum(name + "_TR3", period3);
67  }
68 
69  /// <summary>
70  /// Gets a flag indicating when this indicator is ready and fully initialized
71  /// </summary>
72  public override bool IsReady => Samples > _period;
73 
74  /// <summary>
75  /// Required period, in data points, for the indicator to be ready and fully initialized.
76  /// </summary>
77  public int WarmUpPeriod => _period + 1;
78 
79  /// <summary>
80  /// Computes the next value of this indicator from the given state
81  /// </summary>
82  /// <param name="input">The input given to the indicator</param>
83  /// <returns>A new value for this indicator</returns>
84  protected override decimal ComputeNextValue(IBaseDataBar input)
85  {
86  _trueRange.Update(input);
87 
88  if (Samples == 1)
89  {
90  _previousInput = input;
91  return 50m;
92  }
93 
94  var buyingPressure = new IndicatorDataPoint { Value = input.Close - Math.Min(input.Low, _previousInput.Close) };
95 
96  _sumBuyingPressure1.Update(buyingPressure);
97  _sumBuyingPressure2.Update(buyingPressure);
98  _sumBuyingPressure3.Update(buyingPressure);
99 
100  _sumTrueRange1.Update(_trueRange.Current);
101  _sumTrueRange2.Update(_trueRange.Current);
102  _sumTrueRange3.Update(_trueRange.Current);
103 
104  _previousInput = input;
105 
106  if (!IsReady)
107  return 50m;
108 
109  if (_sumTrueRange1.Current.Value == 0
110  || _sumTrueRange2.Current.Value == 0
111  || _sumTrueRange3.Current.Value == 0)
112  {
113  return Current.Value;
114  }
115 
116  var average1 = _sumBuyingPressure1.Current.Value / _sumTrueRange1.Current.Value;
117  var average2 = _sumBuyingPressure2.Current.Value / _sumTrueRange2.Current.Value;
118  var average3 = _sumBuyingPressure3.Current.Value / _sumTrueRange3.Current.Value;
119 
120  return 100m * (4 * average1 + 2 * average2 + average3) / 7;
121  }
122 
123  /// <summary>
124  /// Resets this indicator to its initial state
125  /// </summary>
126  public override void Reset()
127  {
128  _previousInput = null;
129  _trueRange.Reset();
130  _sumBuyingPressure1.Reset();
131  _sumBuyingPressure2.Reset();
132  _sumBuyingPressure3.Reset();
133  _sumTrueRange1.Reset();
134  _sumTrueRange2.Reset();
135  _sumTrueRange3.Reset();
136  base.Reset();
137  }
138  }
139 }