Lean  $LEAN_TAG$
AroonOscillator.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 using System;
18 
20 {
21  /// <summary>
22  /// The Aroon Oscillator is the difference between AroonUp and AroonDown. The value of this
23  /// indicator fluctuates between -100 and +100. An upward trend bias is present when the oscillator
24  /// is positive, and a negative trend bias is present when the oscillator is negative. AroonUp/Down
25  /// values over 75 identify strong trends in their respective direction.
26  /// </summary>
28  {
29  /// <summary>
30  /// Gets the AroonUp indicator
31  /// </summary>
33 
34  /// <summary>
35  /// Gets the AroonDown indicator
36  /// </summary>
38 
39  /// <summary>
40  /// Gets a flag indicating when this indicator is ready and fully initialized
41  /// </summary>
42  public override bool IsReady => AroonUp.IsReady && AroonDown.IsReady;
43 
44  /// <summary>
45  /// Required period, in data points, for the indicator to be ready and fully initialized.
46  /// </summary>
47  public int WarmUpPeriod { get; }
48 
49  /// <summary>
50  /// Creates a new AroonOscillator from the specified up/down periods.
51  /// </summary>
52  /// <param name="upPeriod">The lookback period to determine the highest high for the AroonDown</param>
53  /// <param name="downPeriod">The lookback period to determine the lowest low for the AroonUp</param>
54  public AroonOscillator(int upPeriod, int downPeriod)
55  : this($"AROON({upPeriod},{downPeriod})", upPeriod, downPeriod)
56  {
57  }
58 
59  /// <summary>
60  /// Creates a new AroonOscillator from the specified up/down periods.
61  /// </summary>
62  /// <param name="name">The name of this indicator</param>
63  /// <param name="upPeriod">The lookback period to determine the highest high for the AroonDown</param>
64  /// <param name="downPeriod">The lookback period to determine the lowest low for the AroonUp</param>
65  public AroonOscillator(string name, int upPeriod, int downPeriod)
66  : base(name)
67  {
68  var max = new Maximum(name + "_Max", upPeriod + 1);
69  AroonUp = new FunctionalIndicator<IndicatorDataPoint>(name + "_AroonUp",
70  input => ComputeAroonUp(upPeriod, max, input),
71  aroonUp => max.IsReady,
72  () => max.Reset()
73  );
74 
75  var min = new Minimum(name + "_Min", downPeriod + 1);
76  AroonDown = new FunctionalIndicator<IndicatorDataPoint>(name + "_AroonDown",
77  input => ComputeAroonDown(downPeriod, min, input),
78  aroonDown => min.IsReady,
79  () => min.Reset()
80  );
81 
82  WarmUpPeriod = 1 + Math.Max(upPeriod, downPeriod);
83  }
84 
85  /// <summary>
86  /// Computes the next value of this indicator from the given state
87  /// </summary>
88  /// <param name="input">The input given to the indicator</param>
89  /// <returns>A new value for this indicator</returns>
90  protected override decimal ComputeNextValue(IBaseDataBar input)
91  {
92  AroonUp.Update(input.Time, input.High);
93  AroonDown.Update(input.Time, input.Low);
94 
95  return AroonUp.Current.Value - AroonDown.Current.Value;
96  }
97 
98  /// <summary>
99  /// AroonUp = 100 * (period - {periods since max})/period
100  /// </summary>
101  /// <param name="upPeriod">The AroonUp period</param>
102  /// <param name="max">A Maximum indicator used to compute periods since max</param>
103  /// <param name="input">The next input data</param>
104  /// <returns>The AroonUp value</returns>
105  private static decimal ComputeAroonUp(int upPeriod, Maximum max, IndicatorDataPoint input)
106  {
107  max.Update(input);
108  return 100m * (upPeriod - max.PeriodsSinceMaximum) / upPeriod;
109  }
110 
111  /// <summary>
112  /// AroonDown = 100 * (period - {periods since min})/period
113  /// </summary>
114  /// <param name="downPeriod">The AroonDown period</param>
115  /// <param name="min">A Minimum indicator used to compute periods since min</param>
116  /// <param name="input">The next input data</param>
117  /// <returns>The AroonDown value</returns>
118  private static decimal ComputeAroonDown(int downPeriod, Minimum min, IndicatorDataPoint input)
119  {
120  min.Update(input);
121  return 100m * (downPeriod - min.PeriodsSinceMinimum) / downPeriod;
122  }
123 
124  /// <summary>
125  /// Resets this indicator and both sub-indicators (AroonUp and AroonDown)
126  /// </summary>
127  public override void Reset()
128  {
129  AroonUp.Reset();
130  AroonDown.Reset();
131  base.Reset();
132  }
133  }
134 }