Lean  $LEAN_TAG$
HullMovingAverage.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;
17 
19 {
20  /// <summary>
21  /// Produces a Hull Moving Average as explained at http://www.alanhull.com/hull-moving-average/
22  /// and derived from the instructions for the Excel VBA code at http://finance4traders.blogspot.com/2009/06/how-to-calculate-hull-moving-average.html
23  /// </summary>
25  {
26  private readonly LinearWeightedMovingAverage _fastWma;
27  private readonly LinearWeightedMovingAverage _slowWma;
28  private readonly LinearWeightedMovingAverage _hullMa;
29 
30  /// <summary>
31  /// A Hull Moving Average
32  /// </summary>
33  /// <param name="name">string - a name for the indicator</param>
34  /// <param name="period">int - the number of periods to calculate the HMA - the period of the slower LWMA</param>
35  public HullMovingAverage(string name, int period)
36  : base(name)
37  {
38  if (period < 2) throw new ArgumentException("The Hull Moving Average period should be greater or equal to 2", nameof(period));
39  _slowWma = new LinearWeightedMovingAverage(period);
40  _fastWma = new LinearWeightedMovingAverage((int) Math.Round(period * 1d / 2));
41  var k = (int)Math.Round(Math.Sqrt(period));
42  _hullMa = new LinearWeightedMovingAverage(k);
43  WarmUpPeriod = period + k - 1;
44  }
45  /// <summary>
46  /// A Hull Moving Average.
47  /// </summary>
48  /// <param name="period">int - the number of periods over which to calculate the HMA - the length of the slower LWMA</param>
49  public HullMovingAverage(int period)
50  : this($"HMA({period})", period)
51  {
52  }
53 
54  /// <summary>
55  /// Resets this indicator to its initial state
56  /// </summary>
57  public override void Reset()
58  {
59  base.Reset();
60  _slowWma.Reset();
61  _fastWma.Reset();
62  _hullMa.Reset();
63  }
64 
65  /// <summary>
66  /// Gets a flag indicating when this indicator is ready and fully initialized
67  /// </summary>
68  public override bool IsReady => _hullMa.IsReady;
69 
70  /// <summary>
71  /// Required period, in data points, for the indicator to be ready and fully initialized.
72  /// </summary>
73  public int WarmUpPeriod { get; }
74 
75  /// <summary>
76  /// Computes the next value of this indicator from the given state
77  /// </summary>
78  /// <param name="input">The input given to the indicator</param>
79  /// <returns>
80  /// A new value for this indicator
81  /// </returns>
82  protected override decimal ComputeNextValue(IndicatorDataPoint input)
83  {
84  _fastWma.Update(input);
85  _slowWma.Update(input);
86  if (_fastWma.IsReady && _slowWma.IsReady)
87  {
88  _hullMa.Update(new IndicatorDataPoint(input.Time, 2 * _fastWma.Current.Value - _slowWma.Current.Value));
89  }
90  return _hullMa.Current.Value;
91  }
92  }
93 }