Supported Indicators
Moving Average Convergence Divergence
Introduction
This indicator creates two moving averages defined on a base indicator and produces the difference between the fast and slow averages.
To view the implementation of this indicator, see the LEAN GitHub repository.
Using MACD Indicator
To create an automatic indicator for MovingAverageConvergenceDivergence, call the MACDmacd helper method from the QCAlgorithm class. The MACDmacd method creates a MovingAverageConvergenceDivergence 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 MovingAverageConvergenceDivergenceAlgorithm : QCAlgorithm
{
private Symbol _symbol;
private MovingAverageConvergenceDivergence _macd;
public override void Initialize()
{
_symbol = AddEquity("SPY", Resolution.Daily).Symbol;
_macd = MACD(_symbol, 12, 26, 9, MovingAverageType.Exponential);
}
public override void OnData(Slice data)
{
if (_macd.IsReady)
{
// The current value of _macd is represented by itself (_macd)
// or _macd.Current.Value
Plot("MovingAverageConvergenceDivergence", "macd", _macd);
// Plot all properties of abands
Plot("MovingAverageConvergenceDivergence", "fast", _macd.Fast);
Plot("MovingAverageConvergenceDivergence", "slow", _macd.Slow);
Plot("MovingAverageConvergenceDivergence", "signal", _macd.Signal);
Plot("MovingAverageConvergenceDivergence", "histogram", _macd.Histogram);
}
}
} class MovingAverageConvergenceDivergenceAlgorithm(QCAlgorithm):
def initialize(self) -> None:
self._symbol = self.add_equity("SPY", Resolution.DAILY).symbol
self._macd = self.macd(self._symbol, 12, 26, 9, MovingAverageType.EXPONENTIAL)
def on_data(self, slice: Slice) -> None:
if self._macd.is_ready:
# The current value of self._macd is represented by self._macd.current.value
self.plot("MovingAverageConvergenceDivergence", "macd", self._macd.current.value)
# Plot all attributes of self._macd
self.plot("MovingAverageConvergenceDivergence", "fast", self._macd.fast.current.value)
self.plot("MovingAverageConvergenceDivergence", "slow", self._macd.slow.current.value)
self.plot("MovingAverageConvergenceDivergence", "signal", self._macd.signal.current.value)
self.plot("MovingAverageConvergenceDivergence", "histogram", self._macd.histogram.current.value)For more information about this method, see the QCAlgorithm classQCAlgorithm class.
You can manually create a MovingAverageConvergenceDivergence 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 MovingAverageConvergenceDivergenceAlgorithm : QCAlgorithm
{
private Symbol _symbol;
private MovingAverageConvergenceDivergence _movingaverageconvergencedivergence;
public override void Initialize()
{
_symbol = AddEquity("SPY", Resolution.Daily).Symbol;
_movingaverageconvergencedivergence = new MovingAverageConvergenceDivergence(12, 26, 9, MovingAverageType.Exponential);
}
public override void OnData(Slice data)
{
if (data.Bars.TryGetValue(_symbol, out var bar))
_movingaverageconvergencedivergence.Update(bar.EndTime, bar.Close);
if (_movingaverageconvergencedivergence.IsReady)
{
// The current value of _movingaverageconvergencedivergence is represented by itself (_movingaverageconvergencedivergence)
// or _movingaverageconvergencedivergence.Current.Value
Plot("MovingAverageConvergenceDivergence", "movingaverageconvergencedivergence", _movingaverageconvergencedivergence);
// Plot all properties of abands
Plot("MovingAverageConvergenceDivergence", "fast", _movingaverageconvergencedivergence.Fast);
Plot("MovingAverageConvergenceDivergence", "slow", _movingaverageconvergencedivergence.Slow);
Plot("MovingAverageConvergenceDivergence", "signal", _movingaverageconvergencedivergence.Signal);
Plot("MovingAverageConvergenceDivergence", "histogram", _movingaverageconvergencedivergence.Histogram);
}
}
} class MovingAverageConvergenceDivergenceAlgorithm(QCAlgorithm):
def initialize(self) -> None:
self._symbol = self.add_equity("SPY", Resolution.DAILY).symbol
self._movingaverageconvergencedivergence = MovingAverageConvergenceDivergence(12, 26, 9, MovingAverageType.EXPONENTIAL)
def on_data(self, slice: Slice) -> None:
bar = slice.bars.get(self._symbol)
if bar:
self._movingaverageconvergencedivergence.update(bar.end_time, bar.close)
if self._movingaverageconvergencedivergence.is_ready:
# The current value of self._movingaverageconvergencedivergence is represented by self._movingaverageconvergencedivergence.current.value
self.plot("MovingAverageConvergenceDivergence", "movingaverageconvergencedivergence", self._movingaverageconvergencedivergence.current.value)
# Plot all attributes of self._movingaverageconvergencedivergence
self.plot("MovingAverageConvergenceDivergence", "fast", self._movingaverageconvergencedivergence.fast.current.value)
self.plot("MovingAverageConvergenceDivergence", "slow", self._movingaverageconvergencedivergence.slow.current.value)
self.plot("MovingAverageConvergenceDivergence", "signal", self._movingaverageconvergencedivergence.signal.current.value)
self.plot("MovingAverageConvergenceDivergence", "histogram", self._movingaverageconvergencedivergence.histogram.current.value)For more information about this indicator, see its referencereference.
Indicator History
To get the historical data of the MovingAverageConvergenceDivergence 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 MovingAverageConvergenceDivergenceAlgorithm : QCAlgorithm
{
private Symbol _symbol;
private MovingAverageConvergenceDivergence _macd;
public override void Initialize()
{
_symbol = AddEquity("SPY", Resolution.Daily).Symbol;
_macd = MACD(_symbol, 12, 26, 9, MovingAverageType.Exponential);
var indicatorHistory = IndicatorHistory(_macd, _symbol, 100, Resolution.Minute);
var timeSpanIndicatorHistory = IndicatorHistory(_macd, _symbol, TimeSpan.FromDays(10), Resolution.Minute);
var timePeriodIndicatorHistory = IndicatorHistory(_macd, _symbol, new DateTime(2024, 7, 1), new DateTime(2024, 7, 5), Resolution.Minute);
// Access all attributes of indicatorHistory
var fast = indicatorHistory.Select(x => ((dynamic)x).Fast).ToList();
var slow = indicatorHistory.Select(x => ((dynamic)x).Slow).ToList();
var signal = indicatorHistory.Select(x => ((dynamic)x).Signal).ToList();
var histogram = indicatorHistory.Select(x => ((dynamic)x).Histogram).ToList();
}
} class MovingAverageConvergenceDivergenceAlgorithm(QCAlgorithm):
def initialize(self) -> None:
self._symbol = self.add_equity("SPY", Resolution.DAILY).symbol
self._macd = self.macd(self._symbol, 12, 26, 9, MovingAverageType.EXPONENTIAL)
indicator_history = self.indicator_history(self._macd, self._symbol, 100, Resolution.MINUTE)
timedelta_indicator_history = self.indicator_history(self._macd, self._symbol, timedelta(days=10), Resolution.MINUTE)
time_period_indicator_history = self.indicator_history(self._macd, 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
fast = indicator_history_df["fast"]
slow = indicator_history_df["slow"]
signal = indicator_history_df["signal"]
histogram = indicator_history_df["histogram"]