Lean  $LEAN_TAG$
AwesomeOscillator.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 Awesome Oscillator Indicator tracks the price midpoint-movement of a security. Specifically,
23  /// <para>
24  /// AO = MAfast[(H+L)/2] - MAslow[(H+L)/2]
25  /// </para>
26  /// where MAfast and MAslow denote simple moving averages wherein fast has a shorter period.
27  /// https://www.barchart.com/education/technical-indicators/awesome_oscillator
28  /// </summary>
30  {
31  /// <summary>
32  /// Gets the indicators slow period moving average.
33  /// </summary>
35 
36  /// <summary>
37  /// Gets the indicators fast period moving average.
38  /// </summary>
40 
41  /// <summary>
42  /// Gets a flag indicating when this indicator is ready and fully initialized
43  /// </summary>
44  public override bool IsReady => SlowAo.IsReady && FastAo.IsReady;
45 
46  /// <summary>
47  /// Required period, in data points, for the indicator to be ready and fully initialized.
48  /// </summary>
49  public int WarmUpPeriod { get; }
50 
51  /// <summary>
52  /// Creates a new Awesome Oscillator from the specified periods.
53  /// </summary>
54  /// <param name="fastPeriod">The period of the fast moving average associated with the AO</param>
55  /// <param name="slowPeriod">The period of the slow moving average associated with the AO</param>
56  /// <param name="type">The type of moving average used when computing the fast and slow term. Defaults to simple moving average.</param>
57  public AwesomeOscillator(int fastPeriod, int slowPeriod, MovingAverageType type=MovingAverageType.Simple)
58  : this($"AO({fastPeriod},{slowPeriod},{type})", fastPeriod, slowPeriod, type)
59  {
60  }
61 
62  /// <summary>
63  /// Creates a new Awesome Oscillator from the specified periods.
64  /// </summary>
65  /// <param name="name">The name of this indicator</param>
66  /// <param name="fastPeriod">The period of the fast moving average associated with the AO</param>
67  /// <param name="slowPeriod">The period of the slow moving average associated with the AO</param>
68  /// <param name="type">The type of moving average used when computing the fast and slow term. Defaults to simple moving average.</param>
69  public AwesomeOscillator(string name, int fastPeriod, int slowPeriod, MovingAverageType type=MovingAverageType.Simple)
70  : base(name)
71  {
72  SlowAo = type.AsIndicator(slowPeriod);
73  FastAo = type.AsIndicator(fastPeriod);
74  WarmUpPeriod = Math.Max(slowPeriod, fastPeriod);
75  }
76 
77  /// <summary>
78  /// Computes the next value of this indicator from the given state.
79  /// </summary>
80  /// <param name="input">The input given to the indicator</param>
81  /// <returns>A new value for this indicator</returns>
82  protected override decimal ComputeNextValue(IBaseDataBar input)
83  {
84  var presentValue = (input.High + input.Low) / 2;
85  SlowAo.Update(input.Time, presentValue);
86  FastAo.Update(input.Time, presentValue);
87 
88  return IsReady ? FastAo - SlowAo : 0m;
89  }
90 
91  /// <summary>
92  /// Resets this indicator
93  /// </summary>
94  public override void Reset()
95  {
96  FastAo.Reset();
97  SlowAo.Reset();
98  base.Reset();
99  }
100  }
101 }