Supported Indicators
Heikin Ashi
Introduction
This indicator computes the Heikin-Ashi bar (HA) The Heikin-Ashi bar is calculated using the following formulas: HA_Close[0] = (Open[0] + High[0] + Low[0] + Close[0]) / 4 HA_Open[0] = (HA_Open[1] + HA_Close[1]) / 2 HA_High[0] = MAX(High[0], HA_Open[0], HA_Close[0]) HA_Low[0] = MIN(Low[0], HA_Open[0], HA_Close[0])
To view the implementation of this indicator, see the LEAN GitHub repository.
Using HeikinAshi Indicator
To create an automatic indicator for HeikinAshi, call the HeikinAshiheikin_ashi helper method from the QCAlgorithm class. The HeikinAshiheikin_ashi method creates a HeikinAshi 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 HeikinAshiAlgorithm : QCAlgorithm
{
private Symbol _symbol;
private HeikinAshi _heikinashi;
public override void Initialize()
{
_symbol = AddEquity("SPY", Resolution.Daily).Symbol;
_heikinashi = HeikinAshi(_symbol);
}
public override void OnData(Slice data)
{
if (_heikinashi.IsReady)
{
// The current value of _heikinashi is represented by itself (_heikinashi)
// or _heikinashi.Current.Value
Plot("HeikinAshi", "heikinashi", _heikinashi);
// Plot all properties of abands
Plot("HeikinAshi", "open", _heikinashi.Open);
Plot("HeikinAshi", "high", _heikinashi.High);
Plot("HeikinAshi", "low", _heikinashi.Low);
Plot("HeikinAshi", "close", _heikinashi.Close);
Plot("HeikinAshi", "volume", _heikinashi.Volume);
}
}
} class HeikinAshiAlgorithm(QCAlgorithm):
def initialize(self) -> None:
self._symbol = self.add_equity("SPY", Resolution.DAILY).symbol
self._heikin_ashi = self.heikin_ashi(self._symbol)
def on_data(self, slice: Slice) -> None:
if self._heikin_ashi.is_ready:
# The current value of self._heikin_ashi is represented by self._heikin_ashi.current.value
self.plot("HeikinAshi", "heikin_ashi", self._heikin_ashi.current.value)
# Plot all attributes of self._heikin_ashi
self.plot("HeikinAshi", "open", self._heikin_ashi.open.current.value)
self.plot("HeikinAshi", "high", self._heikin_ashi.high.current.value)
self.plot("HeikinAshi", "low", self._heikin_ashi.low.current.value)
self.plot("HeikinAshi", "close", self._heikin_ashi.close.current.value)
self.plot("HeikinAshi", "volume", self._heikin_ashi.volume.current.value)For more information about this method, see the QCAlgorithm classQCAlgorithm class.
You can manually create a HeikinAshi 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 HeikinAshiAlgorithm : QCAlgorithm
{
private Symbol _symbol;
private HeikinAshi _heikinashi;
public override void Initialize()
{
_symbol = AddEquity("SPY", Resolution.Daily).Symbol;
_heikinashi = new HeikinAshi();
}
public override void OnData(Slice data)
{
if (data.Bars.TryGetValue(_symbol, out var bar))
_heikinashi.Update(bar);
if (_heikinashi.IsReady)
{
// The current value of _heikinashi is represented by itself (_heikinashi)
// or _heikinashi.Current.Value
Plot("HeikinAshi", "heikinashi", _heikinashi);
// Plot all properties of abands
Plot("HeikinAshi", "open", _heikinashi.Open);
Plot("HeikinAshi", "high", _heikinashi.High);
Plot("HeikinAshi", "low", _heikinashi.Low);
Plot("HeikinAshi", "close", _heikinashi.Close);
Plot("HeikinAshi", "volume", _heikinashi.Volume);
}
}
} class HeikinAshiAlgorithm(QCAlgorithm):
def initialize(self) -> None:
self._symbol = self.add_equity("SPY", Resolution.DAILY).symbol
self._heikinashi = HeikinAshi()
def on_data(self, slice: Slice) -> None:
bar = slice.bars.get(self._symbol)
if bar:
self._heikinashi.update(bar)
if self._heikinashi.is_ready:
# The current value of self._heikinashi is represented by self._heikinashi.current.value
self.plot("HeikinAshi", "heikinashi", self._heikinashi.current.value)
# Plot all attributes of self._heikinashi
self.plot("HeikinAshi", "open", self._heikinashi.open.current.value)
self.plot("HeikinAshi", "high", self._heikinashi.high.current.value)
self.plot("HeikinAshi", "low", self._heikinashi.low.current.value)
self.plot("HeikinAshi", "close", self._heikinashi.close.current.value)
self.plot("HeikinAshi", "volume", self._heikinashi.volume.current.value)For more information about this indicator, see its referencereference.
Indicator History
To get the historical data of the HeikinAshi 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 HeikinAshiAlgorithm : QCAlgorithm
{
private Symbol _symbol;
private HeikinAshi _heikinashi;
public override void Initialize()
{
_symbol = AddEquity("SPY", Resolution.Daily).Symbol;
_heikinashi = HeikinAshi(_symbol);
var indicatorHistory = IndicatorHistory(_heikinashi, _symbol, 100, Resolution.Minute);
var timeSpanIndicatorHistory = IndicatorHistory(_heikinashi, _symbol, TimeSpan.FromDays(10), Resolution.Minute);
var timePeriodIndicatorHistory = IndicatorHistory(_heikinashi, _symbol, new DateTime(2024, 7, 1), new DateTime(2024, 7, 5), Resolution.Minute);
// Access all attributes of indicatorHistory
var open = indicatorHistory.Select(x => ((dynamic)x).Open).ToList();
var high = indicatorHistory.Select(x => ((dynamic)x).High).ToList();
var low = indicatorHistory.Select(x => ((dynamic)x).Low).ToList();
var close = indicatorHistory.Select(x => ((dynamic)x).Close).ToList();
var volume = indicatorHistory.Select(x => ((dynamic)x).Volume).ToList();
}
} class HeikinAshiAlgorithm(QCAlgorithm):
def initialize(self) -> None:
self._symbol = self.add_equity("SPY", Resolution.DAILY).symbol
self._heikin_ashi = self.heikin_ashi(self._symbol)
indicator_history = self.indicator_history(self._heikin_ashi, self._symbol, 100, Resolution.MINUTE)
timedelta_indicator_history = self.indicator_history(self._heikin_ashi, self._symbol, timedelta(days=10), Resolution.MINUTE)
time_period_indicator_history = self.indicator_history(self._heikin_ashi, 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
open = indicator_history_df["open"]
high = indicator_history_df["high"]
low = indicator_history_df["low"]
close = indicator_history_df["close"]
volume = indicator_history_df["volume"]