Moving Average Convergence Divergence (MACD)

Indicator that uses 2 different averages of prices to calculate a trend’s momentum, direction and strength.

Moving Average Convergence Divergence (MACD) is considered one of the most important indicators. It uses 2 different averages of prices to calculate a trend’s momentum, direction and strength.

Trading application

MACD and Signal Line oscillate through a 0 reference line. There are 2 kinds of events that suggest a buy or sell action. When MACD crosses up through the Signal Line, it is a buy signal. Conversely, if MACD crosses down through the Signal Line, it is seen as a sell signal.

We can also consider it a signal to buy when MACD crosses the 0 line from negative into positive values, and a signal to sell when it crosses the 0 line from positive to negative values. As long as MACD is above 0, we can consider the asset to be in a bullish trend, while MACD staying below zero indicates bearish conditions.

Divergences also reflect bullish or bearish conditions: we can consider it bearish if the asset’s price increases more than the MACD (e.g. reaching a new high while the indicator doesn’t), in the same way that it would be bullish to see the asset’s price decreasing more than the MACD does.

Calculation

MACD Line:

(n1PeriodEMAn2PeriodEMA)(n1 Period EMA - n2 Period EMA)

Signal Line:

n3PeriodEMAofMACDLinen3 Period EMA of MACD Line

EMA: Exponential Moving Average

Parameters

Input Parameters

Name

Type

Range of value

Description

fast

int

<1, int.MaxValue>

Number of bars to calculate the fast EMA inside MACD indicator.

slow

int

<1, int.MaxValue>

Number of bars to calculate the slow EMA.

smooth

int

<1, int.MaxValue>

The number of bars to calculate the EMA signal line.

Output Parameters

Indicator ouptuts an object of values.

Name

Type

Range of value

Description

Value

double

(0, double.MaxValue)

Output value of MACD indicator.

Average

double

(0, double.MaxValue)

Average of MACD indicator.

Difference

double

(0, double.MaxValue)

The difference of MACD indicator.

Examples

// Prints the current MACD value
macd = indicators.MACD(12, 26, 9).OnSeries(dailyBars.Close);
Log("The current MACD value is " + macd.Value.Value.ToString());
// Prints the current MACD average value
macd = indicators.MACD(12, 26, 9).OnSeries(dailyBars.Close);
Log("The current MACD average value is " + macd.Value.Average.ToString());
// Prints the current MACD difference value
macd = indicators.MACD(12, 26, 9).OnSeries(dailyBars.Close);
Log("The current MACD difference value is " + macd.Value.Difference.ToString());
// Prints the previous MACD value
macd = indicators.MACD(12, 26, 9).Keep(2).OnSeries(dailyBars.Close);
Log("The previous MACD value is " + macd.Values[1].Value.ToString());

Complete example

using Signals.DataSeries.Bars;
using Signals.Framework;
using Signals.Indicators.MACD;
public class MyStrategy : SingleMarketStrategy
{
private Bars dailyBars;
private MACD macd;
public override void Setup(DataMarketplace data, IndicatorsMarketplace indicators)
{
dailyBars = data.Bars(BarPeriodType.Day, 1).WithOffset(25);
macd = indicators.MACD(12, 26, 9).Keep(2).OnSeries(dailyBars.Close);
}
public override void RegisterActions()
{
OnUpdateOf(dailyBars).Do(() =>
{
// Prints the current MACD value
Log("The current MACD value is " + macd.Value.Value.ToString());
// Prints the previous MACD value
Log("The previous MACD value is " + macd.Values[1].Value.ToString());
// Prints the current MACD average value
Log("The current MACD average value is " + macd.Value.Average.ToString());
// Prints the current MACD difference value
Log("The current MACD difference value is " + macd.Value.Difference.ToString());
});
}
}