MACD

Definition

MACD refers to Moving Average Convergence/Divergence. It’s an indicator that uses the difference between two exponential moving averages to create a signal for Buying and selling stocks.

MACD is the difference between a faster and a slower exponential moving averages. The typical values for the faster moving average is 12 and for the slower moving average is 26.

A second indicator is usually used with MACD and is called a signal line. Signal line is the EMA of the MACD Line itself.

Usage

When the MACD line crosses above MACD Line, it’s good Buying signal (or taking Long position). On the other side, when the MACD Line crosses below the signal line, it’s a good selling signal (or taking short position).

MACD on graph

On the graph above, MACD is the blue line. While the orange line is the signal line. MACD is usually displayed as a separate chart below the main price chart.

MACD is displayed in historgram below the chart. When MACD line is above signal line, histogram bars have positive values and are usually green. When MACD line is below signal line, histogram bars have negative values and are usually red.

When MACD Line and Signal line intersect, Histogram bar has zero value. Zero value is the intersection of both lines. Usually at zero value, it’s a good buy or sell signal. If the histogram moves from red to green, then MACD line is going above Signal line and it’s a good buy signal (or taking long position). If the histogram moves from green to red, then MACD line is going below Signal line and it’s a good sell signal (or taking short position.)

MACD in pine script

//@version=6

indicator("MACD", shorttitle="MACD", overlay=false)

fastLength = input.int(12, "Fast Length", 1)

slowLength = input.int(26, "Slow Length", 1)

signalLength = input.int(9, "Signal Length", 1)

fastMA = ta.ema(close, fastLength)

slowMA = ta.ema(close, slowLength)

macdLine = fastMA - slowMA

signalLine = ta.ema(macdLine, signalLength)

macdHistogram = macdLine - signalLine

plot(macdLine, color=color.rgb(0, 100, 255), title="MACD Line", linewidth=2)

plot(signalLine, color=color.rgb(255, 0, 0), title="Signal Line", linewidth=2)

plot(macdHistogram, style=plot.style_columns, color=macdHistogram >= 0 ? color.rgb(0, 150, 0, 50) : color.rgb(255, 0, 0, 50), title="MACD Histogram")

hline(0, "Zero Line", color=color.gray, linestyle=hline.style_dotted)d