//+------------------------------------------------------------------+
//|                                                    3rdCandle.mq4 |
//|                                                         CommGrow |
//|                                            http://www.google.com |
//+------------------------------------------------------------------+
#property copyright "Subu"
#property link      "http://www.google.com"
#property indicator_buffers 2
#property indicator_color1 clrBlue
#property indicator_color2 clrRed
#property indicator_width1 2
#property indicator_width2 2
#property indicator_chart_window

double UpArrow[];
double DownArrow[];
extern int ShiftArrow = -2;
extern bool FilterBullBearCandle = false;
extern bool   alertsOn           = false;
extern bool   alertsOnCurrent    = true;
extern bool   alertsMessage      = true;
extern bool   alertsSound        = false;
extern bool   alertsEmail        = false;
extern bool   alertsNotify       = false;
//+------------------------------------------------------------------+
//| Custom indicator initialization function                         |
//+------------------------------------------------------------------+
int init()
  {
//---- indicators
   SetIndexStyle(0, DRAW_ARROW, EMPTY,2);
   SetIndexArrow(0, 233);
   SetIndexBuffer(0, UpArrow);
   SetIndexEmptyValue(0,0.0);
   SetIndexShift(0,ShiftArrow);
   SetIndexStyle(1, DRAW_ARROW, EMPTY,2);
   SetIndexArrow(1, 234);
   SetIndexBuffer(1, DownArrow);
   SetIndexEmptyValue(1,0.0);
   SetIndexShift(1,ShiftArrow);
//----
   return(0);
  }
//+------------------------------------------------------------------+
//| Custom indicator deinitialization function                       |
//+------------------------------------------------------------------+
int deinit()
  {
//----
   
//----
   return(0);
  }
//+------------------------------------------------------------------+
//| Custom indicator iteration function                              |
//+------------------------------------------------------------------+
int start()
  {
   int    counted_bars=IndicatorCounted();
   int limit, i;
   if(counted_bars<0) return(-1);
//---- last counted bar will be recounted
   if(counted_bars>0) counted_bars--;

   limit=Bars-counted_bars;
//----
    for(i = 0; i <= limit; i++) {
      DownArrow[i] = 0;
      UpArrow[i] = 0;
   if(High[i+2]>High[i+1] && Low[i+2]>Low[i+1] && High[i+2]>High[i+3] && Low[i+2]>Low[i+3])
      if( Open[i+1]>Close[i+1] && Close[i+2] > Close[i+1])
         if(FilterBullBearCandle)
            {
               if( Open[i+2]>Close[i+2])
                  DownArrow[i] = High[i+2] +0.0003;//Low[i+2] + (High[i+2]-Low[i+2]);
             }
         else
             DownArrow[i] = High[i+2] +0.0003;//Low[i+2] + (High[i+2]-Low[i+2]);
   if(High[i+2]<High[i+1] && Low[i+2]<Low[i+1] && High[i+2]<High[i+3] && Low[i+2]<Low[i+3])
      if( Open[i+1]<Close[i+1] && Close[i+2] < Close[i+1])
          if(FilterBullBearCandle)
            {
               if( Open[i+2]<Close[i+2] ) 
                  UpArrow[i] = Low[i+2] - 0.0003;//High[i+2] - (High[i+2]-Low[i+2]);
            }
         else
            UpArrow[i] = Low[i+2] - 0.0003;//High[i+2] - (High[i+2]-Low[i+2]);
      }
//----
    manageAlerts();
   return(0);
  }
//+-------------------------------------&-----------------------------+

//-------------------------------------------------------------------
//                                                                  
//-------------------------------------------------------------------
//
//
//
//
//

void manageAlerts()
{
   if (alertsOn)
   {
      //if (alertsOnCurrent)
      //     int whichBar = 0;
      //else     whichBar = 1;
      int whichBar = 1; if (alertsOnCurrent) whichBar = 0;
      if (UpArrow[whichBar] != 0 || DownArrow[whichBar] != 0)
      {
         if (UpArrow[whichBar]   != 0 && UpArrow[whichBar+1]   == 0) doAlert(whichBar,"up");
         if (DownArrow[whichBar] != 0 && DownArrow[whichBar+1] == 0) doAlert(whichBar,"down");
      }
   }
}

//
//
//
//
//

void doAlert(int forBar, string doWhat)
{
      static string   previousAlert="nothing";
      static datetime previousTime;
      string message;
   
      if (previousAlert != doWhat || previousTime != Time[forBar]) {
          previousAlert  = doWhat;
          previousTime   = Time[forBar];

          //
          //
          //
          //
          //

          message = _Symbol+" at "+TimeToStr(TimeLocal(),TIME_SECONDS)+" 3rdCandle  "+doWhat;
             if (alertsMessage) Alert(message);
             if (alertsEmail)   SendMail(_Symbol+" 3rdCandle ",message);
             if (alertsSound)   PlaySound("alert2.wav");
             if (alertsNotify)  SendNotification(message);
      }
}
