//+------------------------------------------------------------------+
//|                                                 ADX Smoothed.mq4 |
//|                      Copyright © 2007, MetaQuotes Software Corp. |
//|                                        http://www.metaquotes.net |
//+------------------------------------------------------------------+
#property copyright "Copyright © 2007, MetaQuotes Software Corp."
#property link      "http://www.metaquotes.net"
//----
#property indicator_separate_window
#property indicator_buffers 3
#property indicator_color1 DarkBlue
#property indicator_color2 FireBrick
#property indicator_color3 SlateGray
#property indicator_level1 20
//---- input parameters
extern int    per = 14;
extern double alpha1 = 0.25;
extern double alpha2 = 0.33;
extern int    PriceType = 0;
extern bool alertsOn          = false;
extern bool alertsOnCurrent   = false;
extern bool alertsOnCrossDown = false;
extern bool alertsOnCrossUp   = true;
extern bool alertsMessage     = true;
extern bool alertsSound       = false;
extern bool alertsEmail       = false;
//---- buffers
double DiPlusFinal[];
double DiMinusFinal[];
double ADXFinal[];
double DIPlusLead[];
double DIMinusLead[];
double ADXLead[];
//+------------------------------------------------------------------+
//| Custom indicator initialization function                         |
//+------------------------------------------------------------------+
int init()
  {
//---- indicators
   IndicatorBuffers(6);
   SetIndexStyle(0, DRAW_LINE);
   SetIndexBuffer(0, DiPlusFinal);
   SetIndexLabel(0, "Di Plus");
//----   
   SetIndexStyle(1, DRAW_LINE);
   SetIndexBuffer(1, DiMinusFinal);
   SetIndexLabel(1, "Di Minus");
//----
   SetIndexStyle(2, DRAW_LINE);
   SetIndexBuffer(2, ADXFinal);
   SetIndexLabel(2, "ADX");
//----   
   SetIndexBuffer(3, DIPlusLead);
   SetIndexBuffer(4, DIMinusLead);
   SetIndexBuffer(5, ADXLead);
//----   
   IndicatorDigits(2);
   IndicatorShortName("ADX(" + per + ")smothed");
//----
   return(0);
  }
//+------------------------------------------------------------------+
//| Custom indicator deinitialization function                       |
//+------------------------------------------------------------------+
int deinit()
  {
//----
   return(0);
  }
//+------------------------------------------------------------------+
//| Custom indicator iteration function                              |
//+------------------------------------------------------------------+
double trend[][2];
#define _dmim 0
#define _dmip 1


int start()
  {
   int counted_bars = IndicatorCounted();
   int i, r, k, limit;
   double DIPlus, DIMinus, ADX, DIPlus1, DIMinus1, ADX1;
//----
           if (ArrayRange(trend,0)!=Bars) ArrayResize(trend,Bars);

   if(counted_bars == 0) 
       limit = Bars - per - 1;
   if(counted_bars > 0)  
       limit = Bars - counted_bars;
   for(i = limit; i >= 0; i--)
     {
       DIPlus  = iADX(NULL, 0, per, PriceType, MODE_PLUSDI, i);
       DIMinus = iADX(NULL, 0, per, PriceType, MODE_MINUSDI, i);
       ADX     = iADX(NULL, 0, per, PriceType, MODE_MAIN, i);
       
       DIPlus1  = iADX(NULL, 0, per, PriceType, MODE_PLUSDI, i + 1);
       DIMinus1 = iADX(NULL, 0, per, PriceType, MODE_MINUSDI, i + 1);
       ADX1     = iADX(NULL, 0, per, PriceType, MODE_MAIN, i + 1);
       //----
       DIPlusLead[i]  = 2*DIPlus +(alpha1-2)*DIPlus1 +(1-alpha1)*DIPlusLead[i+1];
       DIMinusLead[i] = 2*DIMinus+(alpha1-2)*DIMinus1+(1-alpha1)*DIMinusLead[i+1];
       ADXLead[i]     = 2*ADX + (alpha1 - 2)*ADX1    +(1-alpha1)*ADXLead[i+1];
     }   
   for(i = limit, r=Bars-i-1; i >= 0; i--,r++)
     {
       DiPlusFinal[i] = alpha2*DIPlusLead[i] +(1- alpha2)*DiPlusFinal[i+1];
       DiMinusFinal[i]= alpha2*DIMinusLead[i]+(1- alpha2)*DiMinusFinal[i+1];
       ADXFinal[i]    = alpha2*ADXLead[i]    +(1-alpha2) * ADXFinal[i+1];
         trend[r][_dmim] = trend[r-1][_dmim];
         trend[r][_dmip] = trend[r-1][_dmip];
            if (DiPlusFinal[i] >ADXFinal[i]) trend[r][_dmip] =  1;
            if (DiPlusFinal[i] <ADXFinal[i]) trend[r][_dmip] = -1;
            if (DiMinusFinal[i]>ADXFinal[i]) trend[r][_dmim] =  1;
            if (DiMinusFinal[i]<ADXFinal[i]) trend[r][_dmim] = -1;
       
     }   
     manageAlerts();
//----
   return(0);
  }
//+------------------------------------------------------------------+

//-------------------------------------------------------------------
//
//-------------------------------------------------------------------
//
//
//
//
//

void manageAlerts()
{
   if (alertsOn)
   {
      if (alertsOnCurrent)
           int whichBar = 0;
      else     whichBar = 1; whichBar = Bars-whichBar-1;
      
      //
      //
      //
      //
      //
      
      static datetime prevTime1  = 0;
      static string   prevAlert1 = "";
         if (trend[whichBar][_dmip] != trend[whichBar-1][_dmip])
         {
            if (alertsOnCrossUp   && trend[whichBar][_dmip] ==  1) doAlert(prevTime1,prevAlert1," DMI+ crossed ADX up");
            if (alertsOnCrossDown && trend[whichBar][_dmip] == -1) doAlert(prevTime1,prevAlert1," DMI+ crossed ADX down");
         }
      static datetime prevTime2  = 0;
      static string   prevAlert2 = "";
         if (trend[whichBar][_dmim] != trend[whichBar-1][_dmim])
         {
            if (alertsOnCrossUp   && trend[whichBar][_dmim] ==  1) doAlert(prevTime2,prevAlert2," DMI- crossed ADX up");
            if (alertsOnCrossDown && trend[whichBar][_dmim] == -1) doAlert(prevTime2,prevAlert2," DMI- crossed ADX down");
         }
   }
}

//
//
//
//
//

void doAlert(datetime& previousTime, string& previousAlert, string doWhat)
{
   string message;
   
   if (previousAlert != doWhat || previousTime != Time[0]) {
       previousAlert  = doWhat;
       previousTime   = Time[0];

       //
       //
       //
       //
       //

       message =  Symbol()+" at "+TimeToStr(TimeLocal(),TIME_SECONDS)+doWhat;
          if (alertsMessage) Alert(message);
          if (alertsEmail)   SendMail(StringConcatenate(Symbol(),"Smoothed ADX "),message);
          if (alertsSound)   PlaySound("alert2.wav");
   }
}

