Supported Indicators
Hilbert Transform
Introduction
This indicator computes the Hilbert Transform Indicator by John Ehlers. By using present and prior price differences, and some feedback, price values are split into their complex number components of real (inPhase) and imaginary (quadrature) parts.
To view the implementation of this indicator, see the LEAN GitHub repository.
Using HT Indicator
To create an automatic indicator for HilbertTransform, call the HTht helper method from the QCAlgorithm class. The HTht method creates a HilbertTransform object, hooks it up for automatic updates, and returns it so you can used it in your algorithm. In most cases, you should call the helper method in the Initializeinitialize method.
public class HilbertTransformAlgorithm : QCAlgorithm
{
private Symbol _symbol;
private HilbertTransform _ht;
public override void Initialize()
{
_symbol = AddEquity("SPY", Resolution.Daily).Symbol;
_ht = HT(_symbol, 7, 0.635m, 0.338m);
}
public override void OnData(Slice data)
{
if (_ht.IsReady)
{
// The current value of _ht is represented by itself (_ht)
// or _ht.Current.Value
Plot("HilbertTransform", "ht", _ht);
// Plot all properties of abands
Plot("HilbertTransform", "inphase", _ht.InPhase);
Plot("HilbertTransform", "quadrature", _ht.Quadrature);
}
}
} class HilbertTransformAlgorithm(QCAlgorithm):
def initialize(self) -> None:
self._symbol = self.add_equity("SPY", Resolution.DAILY).symbol
self._ht = self.ht(self._symbol, 7, 0.635, 0.338)
def on_data(self, slice: Slice) -> None:
if self._ht.is_ready:
# The current value of self._ht is represented by self._ht.current.value
self.plot("HilbertTransform", "ht", self._ht.current.value)
# Plot all attributes of self._ht
self.plot("HilbertTransform", "in_phase", self._ht.in_phase.current.value)
self.plot("HilbertTransform", "quadrature", self._ht.quadrature.current.value)For more information about this method, see the QCAlgorithm classQCAlgorithm class.
You can manually create a HilbertTransform indicator, so it doesn't automatically update. Manual indicators let you update their values with any data you choose.
Updating your indicator manually enables you to control when the indicator is updated and what data you use to update it. To manually update the indicator, call the Updateupdate method. The indicator will only be ready after you prime it with enough data.
public class HilbertTransformAlgorithm : QCAlgorithm
{
private Symbol _symbol;
private HilbertTransform _hilberttransform;
public override void Initialize()
{
_symbol = AddEquity("SPY", Resolution.Daily).Symbol;
_hilberttransform = new HilbertTransform(7, 0.635m, 0.338m);
}
public override void OnData(Slice data)
{
if (data.Bars.TryGetValue(_symbol, out var bar))
_hilberttransform.Update(bar.EndTime, bar.Close);
if (_hilberttransform.IsReady)
{
// The current value of _hilberttransform is represented by itself (_hilberttransform)
// or _hilberttransform.Current.Value
Plot("HilbertTransform", "hilberttransform", _hilberttransform);
// Plot all properties of abands
Plot("HilbertTransform", "inphase", _hilberttransform.InPhase);
Plot("HilbertTransform", "quadrature", _hilberttransform.Quadrature);
}
}
} class HilbertTransformAlgorithm(QCAlgorithm):
def initialize(self) -> None:
self._symbol = self.add_equity("SPY", Resolution.DAILY).symbol
self._hilberttransform = HilbertTransform(7, 0.635, 0.338)
def on_data(self, slice: Slice) -> None:
bar = slice.bars.get(self._symbol)
if bar:
self._hilberttransform.update(bar.end_time, bar.close)
if self._hilberttransform.is_ready:
# The current value of self._hilberttransform is represented by self._hilberttransform.current.value
self.plot("HilbertTransform", "hilberttransform", self._hilberttransform.current.value)
# Plot all attributes of self._hilberttransform
self.plot("HilbertTransform", "in_phase", self._hilberttransform.in_phase.current.value)
self.plot("HilbertTransform", "quadrature", self._hilberttransform.quadrature.current.value)For more information about this indicator, see its referencereference.
Indicator History
To get the historical data of the HilbertTransform indicator, call the IndicatorHistoryself.indicator_history method. This method resets your indicator, makes a history request, and updates the indicator with the historical data. Just like with regular history requests, the IndicatorHistoryindicator_history method supports time periods based on a trailing number of bars, a trailing period of time, or a defined period of time. If you don't provide a resolution argument, it defaults to match the resolution of the security subscription.
public class HilbertTransformAlgorithm : QCAlgorithm
{
private Symbol _symbol;
private HilbertTransform _ht;
public override void Initialize()
{
_symbol = AddEquity("SPY", Resolution.Daily).Symbol;
_ht = HT(_symbol, 7, 0.635m, 0.338m);
var indicatorHistory = IndicatorHistory(_ht, _symbol, 100, Resolution.Minute);
var timeSpanIndicatorHistory = IndicatorHistory(_ht, _symbol, TimeSpan.FromDays(10), Resolution.Minute);
var timePeriodIndicatorHistory = IndicatorHistory(_ht, _symbol, new DateTime(2024, 7, 1), new DateTime(2024, 7, 5), Resolution.Minute);
// Access all attributes of indicatorHistory
var inPhase = indicatorHistory.Select(x => ((dynamic)x).InPhase).ToList();
var quadrature = indicatorHistory.Select(x => ((dynamic)x).Quadrature).ToList();
}
} class HilbertTransformAlgorithm(QCAlgorithm):
def initialize(self) -> None:
self._symbol = self.add_equity("SPY", Resolution.DAILY).symbol
self._ht = self.ht(self._symbol, 7, 0.635, 0.338)
indicator_history = self.indicator_history(self._ht, self._symbol, 100, Resolution.MINUTE)
timedelta_indicator_history = self.indicator_history(self._ht, self._symbol, timedelta(days=10), Resolution.MINUTE)
time_period_indicator_history = self.indicator_history(self._ht, self._symbol, datetime(2024, 7, 1), datetime(2024, 7, 5), Resolution.MINUTE)
# Access all attributes of indicator_history
indicator_history_df = indicator_history.data_frame
in_phase = indicator_history_df["inphase"]
quadrature = indicator_history_df["quadrature"]