Syntax
public MACD(DataSeries source, string description)
public static MACD Series(DataSeries source)
Parameter Description
source | The 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:
- See the Divergence indicator to create a MACD Histogram using drag and drop.
- To customize the MACD two periods:
- See Help > WealthScript Programming Guide > Indicators > Custom Indicators > How to: Create a MACD with Custom Periods''
- Install the Community Indicators library and use MACDEx - the MACD with custom parameters.
Interpretation
- MACD crossing above zero is considered bullish, and crossing below zero bearish. Secondly, when MACD turns up from below zero it is considered bullish. When it turns down from above zero this is considered bearish.
- Enter a long position and close any short positions when the MACD fast line crosses from below to above the signal line. The further below the zero line the stronger the signal.
- Enter a short position and close any long positions when the MACD fast line crosses from above to below the signal line. The further above the zero line the stronger the signal.
- Divergence between the MACD and the price action is a strong signal when it confirms the crossover signals.
- During trading ranges the MACD will whipsaw, the fast line crosses back and forth across the signal line. Avoid trading or cut your losses very quickly.
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" );
}
}
}
}