Lean  $LEAN_TAG$
SmoothedOnBalanceVolume.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  /// The SmoothedOnBalanceVolume indicator is smoothed version of OnBalanceVolume
23  /// This indicator computes the OnBalanceVolume and then
24  /// smoothes it over a given period.
26  {
27  /// <summary>This indicator is used to smooth the OnBalanceVolume computation</summary>
28  /// <remarks>This is not exposed publicly since it is the same value as this indicator, meaning
29  /// that this '_smoother' computers the OnBalanceVolume directly, so exposing it publicly would be duplication</remarks>
30  private readonly IndicatorBase<IndicatorDataPoint> _smoother;
31 
32  /// <summary>
33  /// Gets the OnBalanceVolume which is the more volatile calculation to be smoothed by this indicator
34  /// </summary>
36 
37  /// <summary>
38  /// Gets a flag indicating when this indicator is ready and fully initialized
39  /// </summary>
40  public override bool IsReady => _smoother.IsReady;
41 
42  /// <summary>
43  /// Required period, in data points, for the indicator to be ready and fully initialized.
44  /// </summary>
45  public int WarmUpPeriod { get; }
46 
47  /// <summary>
48  /// Creates a new SmoothedOnBalanceVolume indicator using the specified period and moving average type
49  /// </summary>
50  /// <param name="name">The name of this indicator</param>
51  /// <param name="period">The smoothing period used to smooth the OnBalanceVolume values</param>
52  /// <param name="movingAverageType">The type of smoothing used to smooth the OnBalanceVolume values</param>
53  public SmoothedOnBalanceVolume(string name, int period, MovingAverageType movingAverageType = MovingAverageType.Simple)
54  : base(name)
55  {
56  WarmUpPeriod = period;
57 
59  _smoother = movingAverageType.AsIndicator($"{name}_{movingAverageType}", period);
60 
61  }
62 
63  /// <summary>
64  /// Creates a new SmoothedOnBalanceVolume indicator using the specified period and moving average type
65  /// </summary>
66  /// <param name="period">The smoothing period used to smooth the OnBalanceVolume values</param>
67  /// <param name="movingAverageType">The type of smoothing used to smooth the OnBalanceVolume values</param>
68  public SmoothedOnBalanceVolume(int period, MovingAverageType movingAverageType = MovingAverageType.Simple)
69  : this($"SOBV({period})", period, movingAverageType)
70  {
71  }
72 
73  /// <summary>
74  /// Computes the next value of this indicator from the given state
75  /// </summary>
76  /// <param name="input">The input given to the indicator</param>
77  /// <returns>A new value for this indicator</returns>
78  protected override decimal ComputeNextValue(IBaseDataBar input)
79  {
80  // compute the OnBalanceVolume
81  OnBalanceVolume.Update(input);
82 
83  if (_smoother.Update(input.EndTime, OnBalanceVolume.Current.Value)) // Send true range to our smoother and test if it's ready
84  {
85  return _smoother.Current.Value;
86  }
87  else
88  {
89  return 0m;
90  }
91 
92  }
93 
94  /// <summary>
95  /// Resets this indicator to its initial state
96  /// </summary>
97  public override void Reset()
98  {
99  _smoother.Reset();
101  base.Reset();
102  }
103  }
104 }