//+------------------------------------------------------------------+
//|                                                   Custommacd.mq4 |
//|                      Copyright © 2009, MetaQuotes Software Corp. |
//|                                        http://www.metaquotes.net |
//+------------------------------------------------------------------+
#property  copyright "Anakin"
#property  link      ""

//---- indicator settings
#property  indicator_separate_window
#property  indicator_buffers 6
#property  indicator_color1 ForestGreen
#property  indicator_color2 Black
#property  indicator_color3 Aqua
#property  indicator_color4 DeepPink
#property  indicator_color5 IndianRed
#property  indicator_color6 LightGreen
#property  indicator_width1  1
#property  indicator_width2  1
#property  indicator_width3  1
#property  indicator_width4  1


//---- indicator parameters
extern int FastEMA=72;
extern int SlowEMA=156;
extern int SignalSMA=54;
extern int Price=PRICE_WEIGHTED;
extern int Mode=MODE_EMA;


//---- indicator buffers
double green_buffer[];
double red_buffer[];
double MACD[];
double Signal[];
double IRB[];
double LGB[];

//+------------------------------------------------------------------+
//| Custom indicator initialization function                         |
//+------------------------------------------------------------------+
int init()
  {
//---- drawing settings
   IndicatorBuffers(6);
   SetIndexStyle(0,DRAW_HISTOGRAM,0,2);
   SetIndexBuffer(0,green_buffer);
//----
   SetIndexStyle(1,DRAW_HISTOGRAM,0,2);
   SetIndexBuffer(1,red_buffer);

   SetIndexBuffer(2,MACD);
   SetIndexBuffer(3,Signal);
   
   SetIndexStyle(4,DRAW_HISTOGRAM,0,2);
   SetIndexBuffer(4,IRB);
  
   
   SetIndexStyle(5,DRAW_HISTOGRAM,0,2);
   SetIndexBuffer(5,LGB);
   


//---- sets drawing line empty value
   SetIndexEmptyValue(0, 0.0);
   SetIndexEmptyValue(1, 0.0);       
   SetIndexEmptyValue(2, 0.0);
   SetIndexEmptyValue(3, 0.0);
   SetIndexEmptyValue(4, 0.0); 
   SetIndexEmptyValue(5, 0.0);    
//----
   IndicatorDigits(Digits+1);
//---- name for DataWindow and indicator subwindow label
   IndicatorShortName("SilverMACD("+FastEMA+","+SlowEMA+","+SignalSMA+")");
   SetIndexLabel(0,"GreenMACD");      
   SetIndexLabel(1,"RedMACD");
   SetIndexLabel(2,"IndianRedMACD");
   SetIndexLabel(3,"LightGreenMACD");
//---- initialization done
   return(0);
  }
//+------------------------------------------------------------------+
//| Moving Averages Convergence/Divergence                           |
//+------------------------------------------------------------------+
int start()
  {
   int limit;   
   int counted_bars=IndicatorCounted();
   int i;
//---- check for possible errors
   if(counted_bars<0) return(-1);
//
   if(counted_bars>0) counted_bars--;
   limit=Bars-counted_bars;
   //Print("limit=",limit);
   if (counted_bars==0) limit=Bars-SlowEMA;
//---- Вычисляем массив MACD
   for(i=0; i<limit; i++)
      MACD[i]=iMA(NULL,0,FastEMA,0,Mode,Price,i)-iMA(NULL,0,SlowEMA,0,Mode,Price,i);
//---- Вычисляем сигнальную линию для MACD
//----We calculate signal line for MACD


   for(i=0; i<limit; i++)
      Signal[i]=iMAOnArray(MACD,0,SignalSMA,0,MODE_SMA,i);
//---- If MACD more or is equal signal, then the green otherwise red
// also set global variable

   for (i=0; i<limit; i++) 
   {
      if (MACD[i] >= Signal[i]) 
      {
        green_buffer[i] = MACD[i];
        IRB[i]=0.0;
        red_buffer[i] = 0.0;
        //UPTREND
        GlobalVariableSet(Symbol()+"_M"+Period()+"_macd_trend",1);
        //Alert("UPTREND");
        Print("SG MACD CHANGE: UPTREND");
      //  IRB[i]=0.0;
      }
      else 
      {
        green_buffer[i] = 0.0;
        LGB[i]=0.0;
        red_buffer[i] = MACD[i];
        //DOWNTREND
        GlobalVariableSet(Symbol()+"_M"+Period()+"_macd_trend",-1);
        //Alert("DOWNTREND");
        Print("SG MACD CHANGE: DOWNTREND");
      }
   }
   return(0);
  }