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 indicators for MovingAverageConvergenceDivergence, call the MACD helper method from the QCAlgorithm class. The MACD 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 macd
            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.AddEquity("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.IsReady:
            # 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)

The following reference table describes the MACD method:

MACD()1/1

            MovingAverageConvergenceDivergence QuantConnect.Algorithm.QCAlgorithm.MACD (
    Symbol                           symbol,
    Int32                            fastPeriod,
    Int32                            slowPeriod,
    Int32                            signalPeriod,
    *MovingAverageType               type,
    *Nullable<Resolution>      resolution,
    *Func<IBaseData, Decimal>  selector
   )
        

Creates a MACD indicator for the symbol. The indicator will be automatically updated on the given resolution.

If you don't provide a resolution, it defaults to the security resolution. If you provide a resolution, it must be greater than or equal to the resolution of the security. For instance, if you subscribe to hourly data for a security, you should update its indicator with data that spans 1 hour or longer.

For more information about the selector argument, see Alternative Price Fields.

For more information about plotting indicators, see Plotting Indicators.

The following table describes the MovingAverageType enumeration members:

To avoid parameter ambiguity, use the resolution argument to set the Resolution.

public class MovingAverageConvergenceDivergenceAlgorithm : QCAlgorithm
{
    private Symbol _symbol;
    private MovingAverageConvergenceDivergence _macd;

    public override void Initialize()
    {
        _symbol = AddEquity("SPY", Resolution.Hour).Symbol;
        _macd = MACD(_symbol, 12, 26, 9, MovingAverageType.Exponential, resolution: Resolution.Daily);
    }
}
class MovingAverageConvergenceDivergenceAlgorithm(QCAlgorithm):
    def Initialize(self) -> None:
        self._symbol = self.AddEquity("SPY", Resolution.Hour).Symbol
        self.macd = self.MACD(self.symbol, 12, 26, 9, MovingAverageType.Exponential, resolution=Resolution.Daily)

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 with time/number pair or an IndicatorDataPoint. The indicator will only be ready after you prime it with enough data.

public class MovingAverageConvergenceDivergenceAlgorithm : QCAlgorithm
{
    private Symbol _symbol;
    private MovingAverageConvergenceDivergence _macd;

    public override void Initialize()
    {
        _symbol = AddEquity("SPY", Resolution.Daily).Symbol;
        _macd = new MovingAverageConvergenceDivergence(12, 26, 9, MovingAverageType.Exponential);
    }

    public override void OnData(Slice data)
    {
        if (data.Bars.TryGetValue(_symbol, out var bar))
        {      
            _macd.Update(bar.EndTime, bar.Close);
        }
   
        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 macd
            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.AddEquity("SPY", Resolution.Daily).Symbol
        self.macd = MovingAverageConvergenceDivergence(12, 26, 9, MovingAverageType.Exponential)

    def on_data(self, slice: Slice) -> None:
        bar = slice.Bars.get(self.symbol)
        if bar:
            self.macd.Update(bar.EndTime, bar.Close)
        if self.macd.IsReady:
            # 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)

To register a manual indicator for automatic updates with the security data, call the RegisterIndicator method.

public class MovingAverageConvergenceDivergenceAlgorithm : QCAlgorithm
{
    private Symbol _symbol;
    private MovingAverageConvergenceDivergence _macd;

    public override void Initialize()
    {
        _symbol = AddEquity("SPY", Resolution.Daily).Symbol;
        _macd = new MovingAverageConvergenceDivergence(12, 26, 9, MovingAverageType.Exponential);
        RegisterIndicator(_symbol, _macd, Resolution.Daily);
    }

    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 macd
            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.AddEquity("SPY", Resolution.Daily).Symbol
        self.macd = MovingAverageConvergenceDivergence(12, 26, 9, MovingAverageType.Exponential)
        self.RegisterIndicator(self.symbol, self.macd, Resolution.Daily)

    def on_data(self, slice: Slice) -> None:
        if self.macd.IsReady:
            # 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)

The following reference table describes the MovingAverageConvergenceDivergence constructor:

MovingAverageConvergenceDivergence()1/2

            MovingAverageConvergenceDivergence QuantConnect.Indicators.MovingAverageConvergenceDivergence (
    int                 fastPeriod,
    int                 slowPeriod,
    int                 signalPeriod,
    *MovingAverageType  type
   )
        

Creates a new MACD with the specified parameters.

MovingAverageConvergenceDivergence()2/2

            MovingAverageConvergenceDivergence QuantConnect.Indicators.MovingAverageConvergenceDivergence (
    string              name,
    int                 fastPeriod,
    int                 slowPeriod,
    int                 signalPeriod,
    *MovingAverageType  type
   )
        

Creates a new MACD with the specified parameters.

Visualization

The following image shows plot values of selected properties of MovingAverageConvergenceDivergence using the plotly library.

MovingAverageConvergenceDivergence line plot.

You can also see our Videos. You can also get in touch with us via Discord.

Did you find this page helpful?

Contribute to the documentation: