//+------------------------------------------------------------------+
//| MODIFIED BY Avery T. Horton, Jr. 
//| aka TheRumpledOne therumpledone@gmail.com 
//|                                                  custom MACD.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_chart_window
#property  indicator_buffers 2
#property  indicator_color1  Blue
#property  indicator_color2  Red
//---- indicator parameters

extern bool use.Alert = true ;

extern int FastEMA=12;
extern int SlowEMA=26;
extern int SignalEMA=9;

//---- indicator buffers

double     TriggerUP[];
double     TriggerDN[];

int Chart_Period, digits ;
string mySymbol, tPeriod ;
//+------------------------------------------------------------------+
//| Custom indicator initialization function                         |
//+------------------------------------------------------------------+
int init()
  {
//---- drawing settings
   SetIndexStyle(0,DRAW_ARROW,EMPTY,3,Blue);
   SetIndexArrow(0,SYMBOL_ARROWUP);
   SetIndexStyle(1,DRAW_ARROW,EMPTY,3,Red);
   SetIndexArrow(1,SYMBOL_ARROWDOWN);

   SetIndexBuffer(0,TriggerUP);
   SetIndexBuffer(1,TriggerDN);

 Chart_Period = Period() ; 
 tPeriod      = TimeFrameToString( Chart_Period ) ;
 mySymbol     = Symbol() ;
 digits       = Digits ;
 
   return(0);
  }
//+------------------------------------------------------------------+
//| Moving Averages Convergence/Divergence                           |
//+------------------------------------------------------------------+
int start()
  {
   int i,MACDTrigger,SARTrigger,counted_bars=IndicatorCounted();
   double MACDHistCur,MACDHistPrev,Signal,MACD;
	double alpha = 2.0 / (SignalEMA + 1.0);
	double alpha_1 = 1.0 - alpha;
   
//---- last counted bar will be recounted
   if(counted_bars>0) counted_bars--;
   i=Bars-counted_bars;

   while(i>0) 
      {
      MACD=iMACD(NULL,0,FastEMA,SlowEMA,SignalEMA,PRICE_CLOSE,MODE_MAIN,i);
      Signal=iMACD(NULL,0,FastEMA,SlowEMA,SignalEMA,PRICE_CLOSE,MODE_SIGNAL,i);
      
      MACDHistPrev=MACDHistCur;
      MACDHistCur=MACD-Signal;

      if((MACDHistCur*MACDHistPrev)<0.0) MACDTrigger=((MACDHistCur<0.0)+1);
      SARTrigger=((iSAR(NULL,0,0.02,0.2,i)>Close[i])+1);
      if(MACDTrigger==SARTrigger)
         {
         if(MACDTrigger==1)
         { TriggerUP[i]=(High[i]-Low[i])/2+Low[i]+Point*10;
          if( use.Alert ) { Alert(mySymbol," "+ tPeriod, " ", DoubleToStr(TriggerUP[i],digits), " SARMACD BUY " ) ; }   
         }
         if(MACDTrigger==2) 
         { TriggerDN[i]=(Low[i]-Low[i])/2+Low[i]-Point*10;
          if( use.Alert ) { Alert(mySymbol," "+ tPeriod, " ", DoubleToStr(TriggerDN[i],digits), " SARMACD SELL" ) ; }   
         }        
         
         MACDTrigger=0;
         SARTrigger=0;
         } 
       
       
       
        
      i--;
      }        
   return(0);
  }
  

//+------------------------------------------------------------------+

 
string TimeFrameToString(int tf)
{
   string tfs;
   switch(tf) {
      case PERIOD_M1:  tfs="M1"  ; break;
      case PERIOD_M5:  tfs="M5"  ; break;
      case PERIOD_M15: tfs="M15" ; break;
      case PERIOD_M30: tfs="M30" ; break;
      case PERIOD_H1:  tfs="H1"  ; break;
      case PERIOD_H4:  tfs="H4"  ; break;
      case PERIOD_D1:  tfs="D1"  ; break;
      case PERIOD_W1:  tfs="W1"  ; break;
      case PERIOD_MN1: tfs="MN";
   }
   return(tfs);
}