//+------------------------------------------------------------------+
//| Custom MACD+OsMA.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 3
#property indicator_color1 Silver
#property indicator_color2 Red
#property indicator_color3 Silver

#define FLAT 0
#define UP 1
#define DOWN -1

//---- indicator parameters
extern int FastEMA=12;
extern int SlowEMA=26;
extern int SignalSMA=9;
extern int SignalCandle = 0;

extern string s = "-- Alert --";
extern string s1 = " 1. Main";
extern string s2 = " 2. Signal";
extern string s3 = " 3. Histogram";
extern int Show_Alert = 1;
extern bool Play_Sound = true;
extern bool Send_Mail = false;
extern string SoundFilename = "alert.wav";

int CurrentTrend = 0;

//---- indicator buffers
double ind_buffer1[];
double ind_buffer2[];
double ind_buffer3[];
//+------------------------------------------------------------------+
//| Custom indicator initialization function |
//+------------------------------------------------------------------+
int init()
{
//---- drawing settings
SetIndexStyle(0,DRAW_LINE,STYLE_SOLID,1);
SetIndexStyle(1,DRAW_LINE,STYLE_SOLID,1);
SetIndexStyle(2,DRAW_HISTOGRAM,STYLE_SOLID,1);
SetIndexDrawBegin(1,SignalSMA);
IndicatorDigits(MarketInfo(Symbol(),MODE_DIGITS)+1);
//---- indicator buffers mapping
if(!SetIndexBuffer(0,ind_buffer1) && !SetIndexBuffer(1,ind_buffer2) && !SetIndexBuffer(2,ind_buffer3))
Print("cannot set indicator buffers!");
//---- name for DataWindow and indicator subwindow label
IndicatorShortName("MACD("+FastEMA+","+SlowEMA+","+SignalSMA+")");
SetIndexLabel(0,"MACD");
SetIndexLabel(1,"Signal");
SetIndexLabel(2,"OsMA");
//---- initialization done
return(0);
}
//+------------------------------------------------------------------+
//| Moving Averages Convergence/Divergence |
//+------------------------------------------------------------------+
int start()
{
  int limit;
  int counted_bars=IndicatorCounted();
//---- check for possible errors
  if(counted_bars<0) return(-1);
//---- 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++)
    ind_buffer1[i]=iMA(NULL,0,FastEMA,0,MODE_EMA,PRICE_CLOSE,i)-iMA(NULL,0,SlowEMA,0,MODE_EMA,PRICE_CLOSE,i);
//---- signal line counted in the 2-nd buffer
  for(i=0; i<limit; i++)
    ind_buffer2[i]=iMAOnArray(ind_buffer1,Bars,SignalSMA,0,MODE_SMA,i);
  for(i=0; i<limit; i++)
    ind_buffer3[i]=iOsMA(NULL,0,FastEMA,SlowEMA,SignalSMA,PRICE_CLOSE,i);

  int Direction = GetDirection();
  if (Direction == FLAT) return(0);
  
  if (CurrentTrend != UP)
  {
     if (Direction == UP)
     {
         HandleAlert(UP);
         CurrentTrend = UP;

     }
  }

  if (CurrentTrend != DOWN)
  {
    if (Direction == DOWN)
    {
        HandleAlert(DOWN);
        CurrentTrend = DOWN;
    }
  }

//---- done
return(0);
} 

int GetDirection()
{
   switch (Show_Alert)
   {
     case 1 : if (ind_buffer1[SignalCandle + 1] < 0)
              {
                if (ind_buffer1[SignalCandle] > 0) return(UP);
              }
              if (ind_buffer1[SignalCandle + 1] > 0)
              {
                if (ind_buffer1[SignalCandle] < 0) return(DOWN);
              }
              break;
     case 2 : if (ind_buffer2[SignalCandle + 1] < 0)
              {
                if (ind_buffer2[SignalCandle] > 0) return(UP);
              }
              if (ind_buffer2[SignalCandle + 1] > 0)
              {
                if (ind_buffer2[SignalCandle] < 0) return(DOWN);
              }
              break;
     case 3 : if (ind_buffer3[SignalCandle + 1] < 0)
              {
                if (ind_buffer3[SignalCandle] > 0) return(UP);
              }
              if (ind_buffer3[SignalCandle + 1] > 0)
              {
                if (ind_buffer3[SignalCandle] < 0) return(DOWN);
              }
  }
  return(FLAT);
}

void HandleAlert(int direction)
{
   string msg = "MACD";
   
   switch (Show_Alert)
   {
      case 1 : msg = msg + " Main";
               break;
      case 2 : msg = msg + " Signal";
               break;
      case 3 : msg = msg + " Histogram";
   }

   if (Show_Alert > 0) DoAlert(msg, direction);
   if (Play_Sound) PlaySound(SoundFilename);
   if (Send_Mail)  DoMail(msg, direction);
}

void DoAlert(string msg, int direction)
{
   if (direction == UP) Alert(msg + " Cross 0 Up on " + Symbol() + " " + Period());
   if (direction == DOWN) Alert(msg + " Cross 0 Down on " + Symbol() + " " + Period());
}

void DoMail(string msg, int direction)
{
   if (direction == UP) SendMail(msg + " Cross 0 Up on " + Symbol() + " " + Period(), "");
   if (direction == DOWN) SendMail(msg + " Cross 0 Down on " + Symbol() + " " + Period(), "");
}

