Moving Average Convergence / Divergence (MACD)
Introduction
MACD uses moving averages to create a momentum oscillator. The resulting plot forms a line that oscillates above and below zero, without any upper or lower limits.
Formula
The formula for the MACD is the difference between a security's fast and slow Exponential Moving Averages (EMAs). The most popular lengths for the EMAs is 12 for the fast and 26 for the slow. Closing prices are used to form the moving averages. For the ES, Jack has tuned these EMAs to 5 and 13, respectively.
Usually, a 9-period EMA of the MACD itself is plotted along side to act as a trigger line. A bullish crossover occurs when MACD moves above the (typically 9-period) EMA of itself, and a bearish crossover occurs when MACD moves below. For the ES, Jack has tuned the trigger EMA to a length of 6.
The histogram is positive when MACD is above the (typicaly 9-period) EMA of itself and negative when MACD is below.
Code
For an automated trading system, the MACD Indicator is coded as follows:
This MACD Indicator calls a MACD Function, which is coded as follows:
The MACD Function calls an XAverage (exponential average) Function which is coded as follows:
Introduction
MACD uses moving averages to create a momentum oscillator. The resulting plot forms a line that oscillates above and below zero, without any upper or lower limits.
Formula
The formula for the MACD is the difference between a security's fast and slow Exponential Moving Averages (EMAs). The most popular lengths for the EMAs is 12 for the fast and 26 for the slow. Closing prices are used to form the moving averages. For the ES, Jack has tuned these EMAs to 5 and 13, respectively.
Usually, a 9-period EMA of the MACD itself is plotted along side to act as a trigger line. A bullish crossover occurs when MACD moves above the (typically 9-period) EMA of itself, and a bearish crossover occurs when MACD moves below. For the ES, Jack has tuned the trigger EMA to a length of 6.
The histogram is positive when MACD is above the (typicaly 9-period) EMA of itself and negative when MACD is below.
Code
For an automated trading system, the MACD Indicator is coded as follows:
Code:
inputs: FastLength( 5 ), SlowLength( 13 ), MACDLength( 6 ) ;
variables: var0( 0 ), var1( 0 ), var2( 0 ) ;
var0 = MACD( Close, FastLength, SlowLength ) ;
var1 = XAverage( var0, MACDLength ) ;
var2 = var0 - var1 ;
Plot1( var0, "MACD" ) ;
Plot2( var1, "MACDAvg" ) ;
Plot3( var2, "MACDDiff" ) ;
Plot4( 0, "ZeroLine" ) ;
condition1 = var2 crosses over 0 ;
if condition1 then
Alert( "Bullish alert" )
else
begin
condition1 = var2 crosses under 0 ;
if condition1 then
Alert( "Bearish alert" ) ;
end;
This MACD Indicator calls a MACD Function, which is coded as follows:
Code:
inputs: PriceValue( numericseries ), FastLen(numericsimple ), SlowLen( numericsimple ) ;
MACD = XAverage( PriceValue, FastLen ) - XAverage( PriceValue, SlowLen ) ;
The MACD Function calls an XAverage (exponential average) Function which is coded as follows:
Code:
inputs: PriceValue( numericseries ), Len(numericsimple ) ;
variables: var0( 2 / ( Len + 1 ) ) ;
if CurrentBar = 1 then
XAverage = PriceValue
else
XAverage = XAverage[1] + var0 * ( PriceValue - XAverage[1] ) ;