//+------------------------------------------------------------------+
//|                                                      MACD_CT.mq4 |
//|                      Copyright © 2004, MetaQuotes Software Corp. |
//|                                       http://www.metaquotes.net/ |
//+------------------------------------------------------------------+
#property  copyright "Copyright © 2004, MetaQuotes Software Corp."
#property  link      "http://www.metaquotes.net/"
//---- indicator settings
#property  indicator_separate_window
#property  indicator_buffers 4
#property  indicator_color1  0xFFFFFFFF
#property  indicator_color2  Red
#property  indicator_width1  0
#property  indicator_width2  1
#property  indicator_color3  0xFFFFFFFF
#property  indicator_color4  Orange
#property  indicator_width3  0
#property  indicator_width4  1
//---- indicator parameters
extern string MACD1 = "------------------1-----------------------";
extern string PRICE1 = "0:CLOSE 1:OPEN 2:HIGH 3:LOW";
extern string PRICE2 = "4:MEDIAN 5:TYPICAL 6:WEIGHTED";
extern int Price1=0;
extern int Fast1=12;
extern int Slow1=26;
extern string MA_MODE = "0:SMA 1:EMA 2:SMMA 3:LWMA";
extern int mode1F=0;
extern int Signal1=9;
extern int mode1S=0;
//---- indicator buffers
double     MacdBuffer1[];
double     SignalBuffer1[];
extern string MACD2 = "------------------2-----------------------";
extern int Price2=0;
extern int Fast2=48;
extern int Slow2=104;
extern int mode2F=0;
extern int Signal2=9;
extern int mode2S=0;
//---- indicator buffers
double     MacdBuffer2[];
double     SignalBuffer2[];


//+------------------------------------------------------------------+
//| Custom indicator initialization function                         |
//+------------------------------------------------------------------+
int init()
  {
//---- drawing settings
   SetIndexStyle(0,DRAW_LINE);
   SetIndexStyle(1,DRAW_LINE);
   SetIndexDrawBegin(1,Signal1);
   SetIndexStyle(2,DRAW_LINE);
   SetIndexStyle(3,DRAW_LINE);
   SetIndexDrawBegin(3,Signal2);
   IndicatorDigits(Digits+1);
//---- indicator buffers mapping
   SetIndexBuffer(0,MacdBuffer1);
   SetIndexBuffer(1,SignalBuffer1);
   SetIndexBuffer(2,MacdBuffer2);
   SetIndexBuffer(3,SignalBuffer2);
//---- name for DataWindow and indicator subwindow label
   IndicatorShortName("MACD("+Fast1+","+Slow1+","+Signal1+","+Fast2+","+Slow2+","+Signal2+")");
   SetIndexLabel(0,"MACD1");
   SetIndexLabel(1,"Signal1");
   SetIndexLabel(2,"MACD2");
   SetIndexLabel(3,"Signal2");
//---- initialization done
   return(0);
  }
//+------------------------------------------------------------------+
//| Moving Averages Convergence/Divergence                           |
//+------------------------------------------------------------------+
int start()
  {
   int limit;
   int counted_bars=IndicatorCounted();
//---- last counted bar will be recounted
   if(counted_bars>0) counted_bars--;
   limit=Bars-counted_bars;
//---- macd counted in the 1-st buffer
   for(int i=0; i<limit; i++)
      MacdBuffer1[i]=iMA(NULL,0,Fast1,0,mode1F,Price1,i)-iMA(NULL,0,Slow1,0,mode1F,Price1,i);
//---- signal line counted in the 2-nd buffer
   for(i=0; i<limit; i++)
      SignalBuffer1[i]=iMAOnArray(MacdBuffer1,Bars,Signal1,0,mode1S,i);
//---- macd counted in the 1-st buffer
   for(i=0; i<limit; i++)
      MacdBuffer2[i]=iMA(NULL,0,Fast2,0,mode2F,Price2,i)-iMA(NULL,0,Slow2,0,mode2F,Price2,i);
//---- signal line counted in the 2-nd buffer
   for(i=0; i<limit; i++)
      SignalBuffer2[i]=iMAOnArray(MacdBuffer2,Bars,Signal2,0,mode2S,i);
//---- done
   return(0);
  }
//+------------------------------------------------------------------+