Lean  $LEAN_TAG$
HilbertTransform.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.Diagnostics;
17 
18 namespace QuantConnect.Indicators;
19 
20 /// <summary>
21 /// This indicator computes the Hilbert Transform Indicator by John Ehlers.
22 /// By using present and prior price differences, and some feedback, price values are split into their complex number components
23 /// of real (inPhase) and imaginary (quadrature) parts.
24 /// <remark>Source: http://www.technicalanalysis.org.uk/moving-averages/Ehle.pdf</remark>
25 /// </summary>
26 public class HilbertTransform : Indicator, IIndicatorWarmUpPeriodProvider
27 {
28  private readonly int _length;
29  private readonly IndicatorBase<IndicatorDataPoint> _input;
30  private readonly IndicatorBase<IndicatorDataPoint> _prev;
31  private readonly IndicatorBase<IndicatorDataPoint> _detrendPrice;
32  private readonly IndicatorBase<IndicatorDataPoint> _detrendPriceDelay2;
33  private readonly IndicatorBase<IndicatorDataPoint> _detrendPriceDelay4;
34  private readonly IndicatorBase<IndicatorDataPoint> _inPhaseDelay3;
35  private readonly IndicatorBase<IndicatorDataPoint> _quadratureDelay2;
36 
37  /// <summary>
38  /// Real (inPhase) part of complex number component of price values
39  /// </summary>
40  public IndicatorBase<IndicatorDataPoint> InPhase { get; }
41 
42  /// <summary>
43  /// Imaginary (quadrature) part of complex number component of price values
44  /// </summary>
45  public IndicatorBase<IndicatorDataPoint> Quadrature { get; }
46 
47  /// <summary>
48  /// Required period, in data points, for the indicator to be ready and fully initialized.
49  /// </summary>
50  public int WarmUpPeriod => _length + 2;
51 
52  /// <summary>
53  /// Gets a flag indicating when this indicator is ready and fully initialized
54  /// </summary>
55  public override bool IsReady => Samples >= WarmUpPeriod;
56 
57  /// <summary>
58  /// Creates a new Hilbert Transform indicator
59  /// </summary>
60  /// <param name="name">The name of this indicator</param>
61  /// <param name="length">The length of the FIR filter used in the calculation of the Hilbert Transform.
62  /// This parameter determines the number of filter coefficients in the FIR filter.</param>
63  /// <param name="inPhaseMultiplicationFactor">The multiplication factor used in the calculation of the in-phase component of the Hilbert Transform.
64  /// This parameter adjusts the sensitivity and responsiveness of the transform to changes in the input signal.</param>
65  /// <param name="quadratureMultiplicationFactor">The multiplication factor used in the calculation of the quadrature component of the Hilbert Transform.
66  /// This parameter also adjusts the sensitivity and responsiveness of the transform to changes in the input signal.</param>
67  public HilbertTransform(string name, int length, decimal inPhaseMultiplicationFactor, decimal quadratureMultiplicationFactor)
68  : base(name)
69  {
70  _length = length;
71 
72  _input = new Identity(name + "_input");
73  _prev = new Delay(name + "_prev", length);
74  _detrendPrice = _input.Minus(_prev);
75  // 2nd and 4th order difference in detrended price, thus being the 1st and 3rd delay
76  _detrendPriceDelay2 = new Delay(name + "_detrendPriceDelay2", 1);
77  _detrendPriceDelay4 = new Delay(name + "_detrendPriceDelay4", 3);
78  // Update after InPhase & Quadrature property, so delay length -1
79  _inPhaseDelay3 = new Delay(name + "_inPhaseDelay3", 2);
80  _quadratureDelay2 = new Delay(name + "_quadratureDelay2", 1);
81 
82  InPhase = new FunctionalIndicator<IndicatorDataPoint>(name + "_inPhase",
83  _ =>
84  {
85  if (!InPhase!.IsReady)
86  {
87  return 0m;
88  }
89 
90  var v2Value = _detrendPriceDelay2.IsReady ? _detrendPriceDelay2.Current.Value : 0m;
91  var v4Value = _detrendPriceDelay4.IsReady ? _detrendPriceDelay4.Current.Value : 0m;
92  var inPhase3Value = _inPhaseDelay3.IsReady ? _inPhaseDelay3.Current.Value : 0m;
93  return 1.25m * (v4Value - v2Value * inPhaseMultiplicationFactor) + inPhase3Value * inPhaseMultiplicationFactor;
94  },
95  _ => Samples > length + 2,
96  () =>
97  {
98  _detrendPrice.Reset();
99  _detrendPriceDelay2.Reset();
100  _detrendPriceDelay4.Reset();
101  _inPhaseDelay3.Reset();
102  });
103 
104  Quadrature = new FunctionalIndicator<IndicatorDataPoint>(name + "_quad",
105  _ =>
106  {
107  if (!Quadrature!.IsReady)
108  {
109  return 0m;
110  }
111 
112  var v2Value = _detrendPriceDelay2.IsReady ? _detrendPriceDelay2.Current.Value : 0m;
113  var v1Value = _detrendPrice.IsReady ? _detrendPrice.Current.Value : 0m;
114  var quadrature2Value = _quadratureDelay2.IsReady ? _quadratureDelay2.Current.Value : 0m;
115  return v2Value - v1Value * quadratureMultiplicationFactor + quadrature2Value * quadratureMultiplicationFactor;
116  },
117  _ => Samples > length,
118  () =>
119  {
120  _detrendPrice.Reset();
121  _detrendPriceDelay2.Reset();
122  _quadratureDelay2.Reset();
123  });
124  }
125 
126  /// <summary>
127  /// Creates a new Hilbert Transform indicator with default name and default params
128  /// </summary>
129  /// <param name="length">The length of the FIR filter used in the calculation of the Hilbert Transform.
130  /// This parameter determines the number of filter coefficients in the FIR filter.</param>
131  /// <param name="inPhaseMultiplicationFactor">The multiplication factor used in the calculation of the in-phase component
132  /// of the Hilbert Transform. This parameter adjusts the sensitivity and responsiveness of
133  /// the transform to changes in the input signal.</param>
134  /// <param name="quadratureMultiplicationFactor">The multiplication factor used in the calculation of the quadrature component of
135  /// the Hilbert Transform. This parameter also adjusts the sensitivity and responsiveness of the
136  /// transform to changes in the input signal.</param>
137  public HilbertTransform(int length = 7, decimal inPhaseMultiplicationFactor = 0.635m, decimal quadratureMultiplicationFactor = 0.338m)
138  : this($"Hilbert({length}, {inPhaseMultiplicationFactor}, {quadratureMultiplicationFactor})", length, inPhaseMultiplicationFactor, quadratureMultiplicationFactor)
139  {
140  }
141 
142  /// <summary>
143  /// Computes the next value of this indicator from the given state
144  /// </summary>
145  /// <param name="input">The input given to the indicator</param>
146  /// <returns>A new value for this indicator</returns>
147  protected override decimal ComputeNextValue(IndicatorDataPoint input)
148  {
149  Debug.Assert(input != null, nameof(input) + " != null");
150 
151  _input.Update(input);
152  _prev.Update(input);
153 
154  if (_prev.IsReady)
155  {
156  _detrendPrice.Update(input);
157  }
158 
159  if (_detrendPrice.IsReady)
160  {
161  _detrendPriceDelay2.Update(_detrendPrice.Current);
162  _detrendPriceDelay4.Update(_detrendPrice.Current);
163  }
164 
165  InPhase.Update(input);
166  if (InPhase.IsReady)
167  {
168  _inPhaseDelay3.Update(InPhase.Current);
169  }
170 
171  Quadrature.Update(input);
172  if (Quadrature.IsReady)
173  {
174  _quadratureDelay2.Update(Quadrature.Current);
175  }
176 
177  return input.Value;
178  }
179 
180  /// <summary>
181  /// Resets this indicator to its initial state
182  /// </summary>
183  public override void Reset()
184  {
185  base.Reset();
186 
187  _input.Reset();
188  _prev.Reset();
189  _detrendPrice.Reset();
190  _detrendPriceDelay2.Reset();
191  _detrendPriceDelay4.Reset();
192  _inPhaseDelay3.Reset();
193  _quadratureDelay2.Reset();
194 
195  InPhase.Reset();
196  Quadrature.Reset();
197  }
198 }