More Information about:
Link: 1) http://stockcharts.com/school/doku.php?id=chart_school:technical_indicators:moving_average_conve




Calculation:

The MACD is calculated by subtracting the value of a 26-period exponential moving average from a 12-period exponential moving average. A 9-period dotted simple moving average of the MACD (the signal line) is then plotted on top of the MACD.

MACD = EMA(CLOSE, 12)-EMA(CLOSE, 26)
SIGNAL = SMA(MACD, 9)

Where:

EMA - the Exponential Moving Average;
SMA - the Simple Moving Average;
SIGNAL - the signal line of the indicator.



Source Code "Moving Average Convergence/Divergence (MACD)":

function init()
{
   Macd.createParameter("FastEMA", 12);
   Macd.createParameter("SlowEMA", 26);
   Macd.createParameter("Signal", 9);

   Macd.addLevel(0, "lime");

   Macd.createBuffer("MACD");
   Macd.setBufferColor("MACD", 'blue');
   Macd.setBufferDrawStyle("MACD", DrawStyle.LINE);

   Macd.createBuffer("SMA");
   Macd.setBufferColor("SMA", 'red');
   Macd.setBufferDrawStyle("SMA", DrawStyle.LINE);

   Macd.setAutoChangeMaxMin("MACD");
}

function start()
{
   Macd.label = "Macd(" + Macd.parameter("FastEMA")
     + "," + Macd.parameter("SlowEMA")
     + "," + Macd.parameter("Signal") + ")";

   var num = Shared.numberOfQuotes();

   var emaSlowBuffer = Shared.ema(Macd.parameter("SlowEMA"), Shared.close());
   var emaFastBuffer = Shared.ema(Macd.parameter("FastEMA"), Shared.close());

   var bufMacd = new Array(num);
   for (var j = 0; j < num; ++j) {
    var fVal = emaFastBuffer[j] - emaSlowBuffer[j];
    bufMacd[j] = fVal;
   }

   var bufSma = Shared.ema(Macd.parameter("Signal"), bufMacd);

   Macd.setBufferData("MACD", bufMacd);
   Macd.setBufferData("SMA", bufSma);
}