Building Trading Strategies Indicators Moving Average Convergence-Divergence

Moving Average Convergence-Divergence

The Macd is a momentum indicator that shows the relationship between two ExponentialMovingAverages of prices. The Macd Line is calculated as the difference between a fast and a slow Ema, and the Signal Line is an Ema of the Macd Line itself. The Macd helps signal the start of new trends and potential reversals, with high values typically indicating overbought conditions and low values suggesting oversold ones. Divergence between the Macd and price action is also a key signal of the end of a current trend, particularly when Macd is at extremely high or low values.

Market Signals

  • A buy signal is indicated when the Macd Line crosses above the Signal Line.
  • A sell signal is suggested when the Macd Line crosses below the Signal Line.
  • Additionally, it is common to consider buying or selling when the Macd crosses above or below the zero line.

Calculation

The Macd is calculated using the following formulas:

Macd = Ema(Price, fastPeriod) - Ema(Price, slowPeriod)
Signal = Ema(Macd, signalPeriod)

Understanding the Macd's dynamics can be a vital part of a technical trader's strategy, aiding in the decision-making process by highlighting potential changes in market momentum.

    import { Bar, InstrumentProcessorBase } from '@botmain/common';
    import { Macd } from '@botmain/indicators';

    export class InstrumentProcessor extends InstrumentProcessorBase {
        macd = new Macd(14, 32, 10);

        ...

        onBarClose(bar: Bar) {
            //Add bar close to calculate new MACD value
            this.macd.add(bar.close);

            ...

            //Get indicator values
            const macdValue = this.macd.macdValue;
            const macdSignal = this.macd.signalValue;
            
            // Get previous values
            if(this.macd.historyCount >= 2) {
                const macdValue = this.macd.values[1].macdValue;
                const macdSignal = this.macd.values[1].signalValue;
            }
        }
    }