MACD

Modified on 2017/08/28 18:16 by Eugene — Categorized as: Standard Indicators

Syntax

public MACD(DataSeries source, string description) public static MACD Series(DataSeries source)

Parameter Description

sourceThe source DataSeries

Description

MACD returns the Moving Average Convergence Divergence indicator. MACD is a momentum oscillator, yet its primary use is to trade trends. Although it is an oscillator is not used as an overbought or oversold indicator. It appears on the chart as two lines which oscillates without boundaries. The crossover of the two lines give trading signals similar to a two moving average system.

The two lines are called, MACD Line or fast line and MACD Signal or slow line. The MACD line is displayed as a solid line on the chart, and the MACD signal line is displayed as a dashed line on the chart. The popular MACD Histogram is derived from the difference between the MACD and its signal line.

Tips:

Interpretation


Calculation

An approximated MACD can be constructed by subtracting the value of a 26 day Exponential Moving Average (EMA) from a 12 period EMA. The shorter EMA is constantly converging toward, and diverging away from, the longer EMA. This causes MACD to oscillate around the zero level.

MACD line = EMA(12, close) - EMA(26, close), and
MACD Signal = EMA(9, MACD Line)

where,

EMA= Exponential Moving Average
MACD line = MACD fast line, displayed as a solid line on the chart
MACD Signal = MACD signal line or slow line, displayed as a dashed line on the chart

Wealth-Lab's classical MACD calculation is based on 2 EMAs with exponents 0.075 and 0.15. A 26 period EMA has an exponent of 0.074074 and the 12 has 0.153846. If you want to use approximate MACD instead of the classical indicator you can use MACDEx, a custom indicator that lets you provide 2 periods for EMA.

Example

using System; using System.Collections.Generic; using System.Text; using System.Drawing; using WealthLab; using WealthLab.Indicators;

namespace WealthLab.Strategies { public class MyStrategy : WealthScript { protected override void Execute() { /* This system buys a new position whenever MACD crosses the signal line from below 0. It sells all open positions when MACD crosses below the signal line from above 0. The trading loop starts at Bar 60 in order to give the 26-period EMA used in the MACD calculation time to stabilize. */ DataSeries macd = MACD.Series( Close ); DataSeries macdSignal = EMA.Series( macd, 9, EMACalculation.Modern ); DataSeries hist = macd - macdSignal; hist.Description = "MACD Histogram"; ChartPane MACDPane = CreatePane( 50, true, true ); PlotSeries( MACDPane, hist, Color.Brown, LineStyle.Histogram, 2 ); PlotSeries( MACDPane, macd, Color.Red, LineStyle.Solid, 1 ); PlotSeries( MACDPane, macdSignal, Color.Black, LineStyle.Solid, 1 ); for(int bar = 26 * 3; bar < Bars.Count; bar++) { if( CrossOver( bar, macd, macdSignal ) & ( macdbar < 0 ) ) BuyAtMarket( bar+1 ); if( CrossUnder( bar, macd, macdSignal ) & ( macdbar > 0 ) ) SellAtMarket( bar+1, Position.AllPositions, "MACD" ); } } } }