//+------------------------------------------------------------------+
//|                                                         macd.mq4 |
//|                                  /////////////////////////////// |
//|                                    ///////////////////////////// |
//|                       ////////////////////////////////////////// |
//+------------------------------------------------------------------+
#property copyright "Copyright //"
#property link      "http://////"
#property indicator_separate_window
#property indicator_buffers 5
#property indicator_color1 DodgerBlue
#property indicator_color2 DarkOrange
#property indicator_color3 Red
#property indicator_color4 Blue
#property indicator_color5 Gray
//+------------------------------------------------------------------+
extern int    TimeFrame           = 1440;
extern int    FastEMA=12;
extern int    SlowEMA=26;
extern int    SignalSMA=9; 
extern string   signal_mode    = "1=signal line 2=0 3=slope"; 
extern int   signal    = 2; 
extern bool   classic_macd    = false; 
extern bool   play_sound    = false; 
extern bool   popup    = false;
extern bool   mail    = false;
extern bool   show_lines    = false; 
extern color  buy_color    = DodgerBlue; 
extern color  sell_color    = Orange; 
extern int    bars_back=1000;
//+------------------------------------------------------------------+
bool long=true,short=true;
int counted;
static double mac[];
static double mac2[];
static double sig[];
static double sig2[];
static double sig0[];
//+------------------------------------------------------------------+
int init()
  {
   IndicatorShortName("MACD("+FastEMA+","+SlowEMA+","+SignalSMA+")");
   SetIndexBuffer(0,mac);
   SetIndexStyle(0,DRAW_HISTOGRAM);
   SetIndexLabel(0,"MACD");
   SetIndexBuffer(1,mac2);
   SetIndexStyle(1,DRAW_HISTOGRAM);
   SetIndexLabel(1,"MACD");
   SetIndexBuffer(2,sig);
   SetIndexStyle(2,DRAW_LINE);
   SetIndexLabel(2,"Signal");
      SetIndexBuffer(3,sig2);
   SetIndexStyle(3,DRAW_LINE);
   SetIndexLabel(3,"Signal");
   
      SetIndexBuffer(4,sig0);
   SetIndexStyle(4,DRAW_HISTOGRAM);
   SetIndexLabel(4,"MACD");
   
   SetIndexDrawBegin(2,SignalSMA);
   SetLevelValue(0,0);
   return(0);
  }
 
//-------------------------------------------------------------------+
int deinit()
  {
     for(int i=Bars; i>=0; i--)
     {
     ObjectDelete(TimeFrame+"macd"+Time[i]);
     }
   return(0);
  }

//-------------------------------------------------------------------+
int start()
  {
   double up,dn;
   
   
//--count bars back once, then only re count the current bar each tick, 0 chance of repaint
if (bars_back>Bars)bars_back=Bars;
if (counted>0)counted--;
for(int i=bars_back-counted; i>-1; i--){counted++;

//--findind the bar on multi time frames, you can use more than one
int bar_shift = iBarShift(NULL,TimeFrame,Time[i]);
//int bar_shift2 = iBarShift(NULL,TimeFrame2,Time[i]);
//int bar_shift3 = iBarShift(NULL,TimeFrame3,Time[i]);


//--code below 
/*
draw your buffers below

example of using multi time frame
buffer0[i] = iClose(NULL,TimeFrame,bar_shift);  or buffer0[i] = iClose(NULL,TimeFrame,bar_shift+1);

example of using the current chart
buffer0[i] = iClose(NULL,0,i);   or  buffer0[i] = iClose(NULL,0,i+1); 
*/

      if (signal==1){up=iMACD(NULL,TimeFrame,FastEMA,SlowEMA,SignalSMA,PRICE_CLOSE,MODE_MAIN,bar_shift);dn=iMACD(NULL,TimeFrame,FastEMA,SlowEMA,SignalSMA,PRICE_CLOSE,MODE_SIGNAL,bar_shift);}
      if (signal==2){up=iMACD(NULL,TimeFrame,FastEMA,SlowEMA,SignalSMA,PRICE_CLOSE,MODE_MAIN,bar_shift);dn=0;}
      if (signal==3){up=iMACD(NULL,TimeFrame,FastEMA,SlowEMA,SignalSMA,PRICE_CLOSE,MODE_MAIN,bar_shift);dn=iMACD(NULL,TimeFrame,FastEMA,SlowEMA,SignalSMA,PRICE_CLOSE,MODE_MAIN,bar_shift+1);}
      if(short 
      &&up>dn
      ){
      long=true;
     short=false;
     if (play_sound&&i==0)PlaySound("alert.wav");
     if (popup&&i==0)Alert("macd signal", Close[0],"!!!");
     if (mail&&i==0)SendMail("macd signal", "Price "+DoubleToStr(Bid,Digits));
     if (ObjectFind(TimeFrame+"macd"+Time[i]) == -1&&show_lines ){
          ObjectCreate(TimeFrame+"macd"+Time[i], OBJ_VLINE, 0, 0, 0);
          ObjectSet(TimeFrame+"macd"+Time[i], OBJPROP_TIME1, Time[i]);
          ObjectSet(TimeFrame+"macd"+Time[i], OBJPROP_BACK, 1);
          ObjectSet(TimeFrame+"macd"+Time[i], OBJPROP_COLOR, buy_color);
          ObjectSet(TimeFrame+"macd"+Time[i], OBJPROP_STYLE, 3);}
     }
     if(long
     &&up<dn
     ){
     long=false;
     short=true;
     if (play_sound&&i==0)PlaySound("alert.wav");
     if (popup&&i==0)Alert("macd signal", Close[0],"!!!");
     if (mail&&i==0)SendMail("macd signal", "Price "+DoubleToStr(Bid,Digits));
     if (ObjectFind(TimeFrame+"macd"+Time[i]) == -1 &&show_lines){
          ObjectCreate(TimeFrame+"macd"+Time[i], OBJ_VLINE, 0, 0, 0);
          ObjectSet(TimeFrame+"macd"+Time[i], OBJPROP_TIME1, Time[i]);
          ObjectSet(TimeFrame+"macd"+Time[i], OBJPROP_BACK, 1);
          ObjectSet(TimeFrame+"macd"+Time[i], OBJPROP_COLOR, sell_color);
          ObjectSet(TimeFrame+"macd"+Time[i], OBJPROP_STYLE, 3);}
     }
     if (!classic_macd){
  mac[i]  = EMPTY_VALUE;
  mac2[i] = EMPTY_VALUE;
sig[i] = iMACD(NULL,TimeFrame,FastEMA,SlowEMA,SignalSMA,PRICE_CLOSE,MODE_SIGNAL,bar_shift);
if(long) mac[i] = iMACD(NULL,TimeFrame,FastEMA,SlowEMA,SignalSMA,PRICE_CLOSE,MODE_MAIN,bar_shift);
if(short)mac2[i] = iMACD(NULL,TimeFrame,FastEMA,SlowEMA,SignalSMA,PRICE_CLOSE,MODE_MAIN,bar_shift);
}
     if (classic_macd){
  mac[i]  = EMPTY_VALUE;
  mac2[i] = EMPTY_VALUE;
sig2[i] = iMACD(NULL,TimeFrame,FastEMA,SlowEMA,SignalSMA,PRICE_CLOSE,MODE_SIGNAL,bar_shift);
sig[i] = iMACD(NULL,TimeFrame,FastEMA,SlowEMA,SignalSMA,PRICE_CLOSE,MODE_MAIN,bar_shift);
if(long) mac[i] = (sig[i] - sig2[i]) * 2;
if(short)mac2[i] = (sig[i] - sig2[i]) * 2;
}
}//--code above, end of loop
return(0);
  }
//+------------------------------------------------------------------+  