//+------------------------------------------------------------------+
//|                                                      tc_macd.mq4 |
//|                                   Copyright © 2010, Thomas Liles |
//|                                       http://www.trendchaser.org |
//|                        need something coded? tomhliles@yahoo.com |
//+------------------------------------------------------------------+
#property copyright "Copyright © 2010, Thomas Liles"
#property link      "http://www.trendchaser.org"
#property indicator_separate_window
#property indicator_buffers 3
#property indicator_color1 Silver
#property indicator_color2 Gray
#property indicator_color3 Red
//+------------------------------------------------------------------+
extern int    TimeFrame           = 0;
extern int    FastEMA=12;
extern int    SlowEMA=26;
extern int    SignalSMA=9; 
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; 
//+------------------------------------------------------------------+
bool long=true,short=true;
static double mac[];
static double mac2[];
static double sig[];
//+------------------------------------------------------------------+
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");
   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()
  {
   int counted_bars = IndicatorCounted();
   if(counted_bars < 0) return(-1);
   if(counted_bars > 0) counted_bars--;
   int limit = Bars - counted_bars;
   for(int i=limit; i>=0; i--)
     {
      int bar_shift = iBarShift(NULL,TimeFrame,Time[i]);
      if(short 
      &&iMACD(NULL,TimeFrame,FastEMA,SlowEMA,SignalSMA,PRICE_CLOSE,MODE_MAIN,bar_shift)>iMACD(NULL,TimeFrame,FastEMA,SlowEMA,SignalSMA,PRICE_CLOSE,MODE_MAIN,bar_shift+1)
      ){
      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
     &&iMACD(NULL,TimeFrame,FastEMA,SlowEMA,SignalSMA,PRICE_CLOSE,MODE_MAIN,bar_shift)<iMACD(NULL,TimeFrame,FastEMA,SlowEMA,SignalSMA,PRICE_CLOSE,MODE_MAIN,bar_shift+1)
     ){
     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);}
     }
  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);
}
   return(0);
  }
//+------------------------------------------------------------------+  