Lean  $LEAN_TAG$
AccumulationDistributionOscillator.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 Accumulation/Distribution Oscillator (ADOSC)
23  /// The Accumulation/Distribution Oscillator is calculated using the following formula:
24  /// ADOSC = EMA(fast,AD) - EMA(slow,AD)
25  /// </summary>
27  {
28  private readonly int _period;
29  private readonly AccumulationDistribution _ad;
30  private readonly ExponentialMovingAverage _emaFast;
31  private readonly ExponentialMovingAverage _emaSlow;
32 
33  /// <summary>
34  /// Initializes a new instance of the <see cref="AccumulationDistributionOscillator"/> class using the specified parameters
35  /// </summary>
36  /// <param name="fastPeriod">The fast moving average period</param>
37  /// <param name="slowPeriod">The slow moving average period</param>
38  public AccumulationDistributionOscillator(int fastPeriod, int slowPeriod)
39  : this($"ADOSC({fastPeriod},{slowPeriod})", fastPeriod, slowPeriod)
40  {
41  }
42 
43  /// <summary>
44  /// Initializes a new instance of the <see cref="AccumulationDistributionOscillator"/> class using the specified parameters
45  /// </summary>
46  /// <param name="name">The name of this indicator</param>
47  /// <param name="fastPeriod">The fast moving average period</param>
48  /// <param name="slowPeriod">The slow moving average period</param>
49  public AccumulationDistributionOscillator(string name, int fastPeriod, int slowPeriod)
50  : base(name)
51  {
52  _period = Math.Max(fastPeriod, slowPeriod);
53  _ad = new AccumulationDistribution(name + "_AD");
54  _emaFast = new ExponentialMovingAverage(name + "_Fast", fastPeriod);
55  _emaSlow = new ExponentialMovingAverage(name + "_Slow", slowPeriod);
56  }
57 
58  /// <summary>
59  /// Gets a flag indicating when this indicator is ready and fully initialized
60  /// </summary>
61  public override bool IsReady => Samples >= _period;
62 
63  /// <summary>
64  /// Required period, in data points, for the indicator to be ready and fully initialized.
65  /// </summary>
66  public int WarmUpPeriod => _period;
67 
68  /// <summary>
69  /// Computes the next value of this indicator from the given state
70  /// </summary>
71  /// <param name="input">The input given to the indicator</param>
72  /// <returns>A new value for this indicator</returns>
73  protected override decimal ComputeNextValue(TradeBar input)
74  {
75  _ad.Update(input);
76  _emaFast.Update(_ad.Current);
77  _emaSlow.Update(_ad.Current);
78 
79  return IsReady ? _emaFast.Current.Value - _emaSlow.Current.Value : 0m;
80  }
81 
82  /// <summary>
83  /// Resets this indicator to its initial state
84  /// </summary>
85  public override void Reset()
86  {
87  _ad.Reset();
88  _emaFast.Reset();
89  _emaSlow.Reset();
90  base.Reset();
91  }
92  }
93 }