//+------------------------------------------------------------------+
//|                                                  TrendArrows.mq4 |
//|                              Copyright © 2014, Ranzato Flaminio. |
//+------------------------------------------------------------------+
#property copyright "Copyright © 2014, Flaminio Ranzato."
#property link      "https://www.facebook.com/FlaminioRanzato"

#property indicator_chart_window
#property indicator_buffers 3
#property indicator_color1 CLR_NONE
#property indicator_color2 Magenta
#property indicator_color3 DodgerBlue

extern int  ADX_Period    = 14;
extern int  DeltaAdx      = 3;
extern int  Smoothing     = 3;
extern int  SignalGap     = 10;

extern bool AlMes = true; 
extern bool AlEm   = false;
extern bool AlSn   = false;
extern bool AlMb  = false;

int SignalBar = 1;
datetime TimeBar;

double b1[];
double b2[];
double b3[];

int init()  {
   SetIndexStyle(0,DRAW_NONE);
   SetIndexStyle(1,DRAW_ARROW,STYLE_SOLID,1);
   SetIndexStyle(2,DRAW_ARROW,STYLE_SOLID,1);
   SetIndexArrow(2,233);
   SetIndexArrow(1,234);
   SetIndexBuffer(0,b1);
   SetIndexBuffer(1,b2);
   SetIndexBuffer(2,b3);
   SetIndexEmptyValue(1,0);
   SetIndexEmptyValue(2,0);
   return(0);
}
int start() {
   int counted_bars=IndicatorCounted();
   int i,limit;
   double Delta,Signal;
   
   if (counted_bars<0) return(-1);
   if (counted_bars>0) counted_bars--;
   limit=Bars-1;
   if(counted_bars>=1) limit=Bars-counted_bars-1;
   if (limit<0) limit=0;
   
   for(i=0; i<limit; i++) {
     b1[i]=iADX(NULL,0,ADX_Period,PRICE_CLOSE,MODE_MAIN,i);
   }

   for (i=limit;i>=0;i--)   {
      Delta = iMAOnArray(b1,0,Smoothing,0,MODE_SMA,i-1)-iMAOnArray(b1,0,Smoothing,0,MODE_SMA,i);
      Signal = iADX(NULL,0,ADX_Period,PRICE_CLOSE,MODE_PLUSDI,i)-iADX(NULL,0,ADX_Period,PRICE_CLOSE,MODE_MINUSDI,i);
      
      if (Delta>DeltaAdx && Signal<0)
         b2[i]=High[i]+SignalGap*Point;
      if (Delta>DeltaAdx && Signal>0)
         b3[i]=Low[i]-SignalGap*Point;
   }
//-------------------------------------------------------------------+
 if(AlMes || AlEm || AlMb || AlSn)
  { 
   string message1 = (WindowExpertName()+" - "+Symbol()+"  "+PeriodString()+" - Signal Up");
   string message2 = (WindowExpertName()+" - "+Symbol()+"  "+PeriodString()+" - Signal Dn");
       
    if(TimeBar!=Time[0] && b3[SignalBar]!=0 && b3[SignalBar]!=EMPTY_VALUE)
     { 
        if (AlMes) Alert(message1);
        if (AlEm)   SendMail(Symbol()+" - "+WindowExpertName()+" - ",message1);
        if (AlMb)  SendNotification(message1);
        if (AlSn)   PlaySound("alert2.wav");
        TimeBar=Time[0];
     }
    if(TimeBar!=Time[0] && b2[SignalBar]!=0 && b2[SignalBar]!=EMPTY_VALUE)
     { 
        if (AlMes) Alert(message2);
        if (AlEm)   SendMail(Symbol()+" - "+WindowExpertName()+" - ",message2);
        if (AlMb)  SendNotification(message2);
        if (AlSn)   PlaySound("alert2.wav");
        TimeBar=Time[0];
    }
  }
//-------------------------------------------------------------------+
   return(0);
}
//-------------------------------------------------------------------+
string PeriodString()
{
    switch (_Period) 
     {
        case PERIOD_M1:  return("M1");
        case PERIOD_M5:  return("M5");
        case PERIOD_M15: return("M15");
        case PERIOD_M30: return("M30");
        case PERIOD_H1:  return("H1");
        case PERIOD_H4:  return("H4");
        case PERIOD_D1:  return("D1");
        case PERIOD_W1:  return("W1");
        case PERIOD_MN1: return("MN1");
     }    
   return("M" + string(_Period));
}
//-------------------------------------------------------------------+