//-------ANG3110@latchess.com------- 
//****************************************
#property indicator_separate_window
#property indicator_buffers 6
#property indicator_color1 LightSlateGray
#property indicator_color2 DodgerBlue
#property indicator_color3 SandyBrown
#property indicator_color4 SandyBrown
#property indicator_color5 Lavender
#property indicator_color6 Lavender
#property indicator_width2 2
#property indicator_width3 2
#property indicator_width4 2
#property indicator_style5 STYLE_DOT
#property indicator_style6 STYLE_DOT
#property indicator_level1 0
//=====================================
extern int p=14;
extern int Level=25;
extern int SmoothLength=5;
extern int SmoothPhase=0;

//
//
//
//
//

extern bool  alertsOn             = false;
extern bool  alertsOnCurrent      = true;
extern bool  alertsMessage        = true;
extern bool  alertsSound          = false;
extern bool  alertsEmail          = false;

//=====================================
double ADX[],ADXLa[],ADXLb[],DI[],levh[],levl[],trend[];
double dh,dl,tr,H,L,num1,num2,num3,dhk,dlk,div;
double zdh,zdhp,zdl,zdlp,zdx,zdxp,zdhl,si;
double temp,ki;
int fs,fss;
double saved[][3];
//*********************************************************
int init()
{
   IndicatorShortName("ADXm "+"("+p+")");
   //--------------------
   IndicatorBuffers(7);
   SetIndexBuffer(0,DI);
   SetIndexBuffer(1,ADX);
   SetIndexBuffer(2,ADXLa);
   SetIndexBuffer(3,ADXLb);
   SetIndexBuffer(4,levh);
   SetIndexBuffer(5,levl);
   SetIndexBuffer(6,trend);
   //--------------------
   return(0);
}
//*********************************************************
#define  _zdh 0
#define  _zdl 1
#define  _zdx 2
int start()
{
   int cbi,i,r,IC=Bars-IndicatorCounted()-1;
   if (ArrayRange(saved,0) != Bars) ArrayResize(saved,Bars);
   if (IC<10) cbi=IC; else
   {
      cbi=Bars-2;
      ki=2.0/(p+1);
      ADX[cbi+1]=0.0; ADXLa[cbi+1]=0.0; ADXLb[cbi+1]=0.0; 
      zdhp=0.0; zdh=0.0; zdlp=0.0; zdl=0.0; zdxp=0.0; zdx=0.0;
      fs=0; fss=0; 
   }
   if (trend[cbi]==-1) ClearPoint(cbi,ADXLa,ADXLb);
   //-------------------------------------------------
   for (i=cbi, r=Bars-i-1; i>=0; i--,r++)
   {
      zdhp = saved[r-1][_zdh];
      zdlp = saved[r-1][_zdl];
      zdxp = saved[r-1][_zdx];
      
      H=High[i]; L=Low[i];
      
      dh=H-High[i+1];
      dl=Low[i+1]-L;
      
      if(dh<0) dh=0; 
      if(dl<0) dl=0;
      
      if(dh==dl) {dh=0; dl=0;} else if(dh<dl) dh=0; else if(dl<dh) dl=0;
      
      num1=MathAbs(H-L);
      num2=MathAbs(H-Close[i+1]);
      num3=MathAbs(L-Close[i+1]);
      
      tr=MathMax(MathMax(num1,num2),num3);
      
      if(tr==0) {dhk=0; dlk=0;} else {dhk=100.0*dh/tr; dlk=100.0*dl/tr;}
      
      zdh=zdhp+(dhk-zdhp)*ki;
      zdl=zdlp+(dlk-zdlp)*ki;
      
      zdhl=zdh-zdl; DI[i]=zdhl;
      
      div=MathAbs(zdh+zdl);
      if(div==0.00) temp=0; else temp=100*(MathAbs(zdhl)/div);
      
      if (zdh>zdl) fs=1;
      if (zdh<zdl) fs=2;
      
      if (fs==2) temp=-temp;
      
      zdx=zdxp+(temp-zdxp)*ki;

         if (SmoothLength>1)
               ADX[i]=iSmooth(zdx,SmoothLength,SmoothPhase,i);
         else  ADX[i]=zdx;
  
         trend[i] = trend[i+1];
            if (ADX[i]>ADX[i+1]) trend[i] =  1;
            if (ADX[i]<ADX[i+1]) trend[i] = -1;
            ADXLa[i] = EMPTY_VALUE;
            ADXLb[i] = EMPTY_VALUE;
            if (trend[i]==-1) PlotPoint(i,ADXLa,ADXLb,ADX);
//      if (ADX[i]>ADX[i+1]) fss=1;
//      if (ADX[i]<ADX[i+1]) {if (fss==1) ADXL[i+1]=ADX[i+1]; fss=2;} 
//      if (fss==2) ADXL[i]=ADX[i]; else ADXL[i]=0.0;  
      
      levh[i]=Level;
      levl[i]=-Level;
      saved[r][_zdh]=zdh;
      saved[r][_zdl]=zdl;
      saved[r][_zdx]=zdx;
   }
   
   if (alertsOn)
   {
      if (alertsOnCurrent)
           int whichBar = 0;
      else     whichBar = 1;
      if (trend[whichBar] != trend[whichBar+1])
      if (trend[i] == 1)
            doAlert("up");
      else  doAlert("down");
   }
   
   return(0);
}

//+------------------------------------------------------------------+
//|                                                                  |
//+------------------------------------------------------------------+
//
//
//
//
//

void ClearPoint(int i,double& first[],double& second[])
{
   if ((second[i]  != EMPTY_VALUE) && (second[i+1] != EMPTY_VALUE))
        second[i+1] = EMPTY_VALUE;
   else
      if ((first[i] != EMPTY_VALUE) && (first[i+1] != EMPTY_VALUE) && (first[i+2] == EMPTY_VALUE))
          first[i+1] = EMPTY_VALUE;
}

//
//
//
//
//

void PlotPoint(int i,double& first[],double& second[],double& from[])
{
   if (first[i+1] == EMPTY_VALUE)
      {
      if (first[i+2] == EMPTY_VALUE) {
          first[i]    = from[i];
          first[i+1]  = from[i+1];
          second[i]   = EMPTY_VALUE;
         }
      else {
          second[i]   = from[i];
          second[i+1] = from[i+1];
          first[i]    = EMPTY_VALUE;
         }
      }
   else
      {
         first[i]   = from[i];
         second[i]  = EMPTY_VALUE;
      }
}

//+------------------------------------------------------------------
//|                                                                 
//+------------------------------------------------------------------
//
//
//
//
//

double wrk[][10];

#define bsmax  5
#define bsmin  6
#define volty  7
#define vsum   8
#define avolty 9

//
//
//
//
//

double iSmooth(double price, double length, double phase, int i, int s=0)
{
   if (ArrayRange(wrk,0) != Bars) ArrayResize(wrk,Bars);
   
   int r = Bars-i-1; 
      if (r==0) { for(int k=0; k<7; k++) wrk[0][k+s]= price; for(; k<10; k++) wrk[0][k+s]= 0; return(price); }

   //
   //
   //
   //
   //
   
      double len1 = MathMax(MathLog(MathSqrt(0.5*(length-1)))/MathLog(2.0)+2.0,0);
      double pow1 = MathMax(len1-2.0,0.5);
      double del1 = price - wrk[r-1][bsmax+s];
      double del2 = price - wrk[r-1][bsmin+s];
	
         wrk[r][volty+s] = 0;
               if(MathAbs(del1) > MathAbs(del2)) wrk[r][volty+s] = MathAbs(del1); 
               if(MathAbs(del1) < MathAbs(del2)) wrk[r][volty+s] = MathAbs(del2); 
         wrk[r][vsum+s] =	wrk[r-1][vsum+s] + 0.1*(wrk[r][volty+s]-wrk[r-10][volty+s]);
   
         //
         //
         //
         //
         //
      
         double avgLen = MathMin(MathMax(4.0*length,30),150);
            if (r<avgLen)
            {
               double avg = wrk[r][vsum+s];  for (k=1; k<avgLen && (r-k)>=0 ; k++) avg += wrk[r-k][vsum+s];
                                                                                   avg /= k;
            }
            else avg = (wrk[r-1][avolty+s]*avgLen-wrk[r-toInt(avgLen)][vsum+s]+wrk[r][vsum+s])/avgLen;
            
         //
         //
         //
         //
         //
                                                           
         wrk[r][avolty+s] = avg;                                           
            if (wrk[r][avolty+s] > 0)
               double dVolty = wrk[r][volty+s]/wrk[r][avolty+s]; else dVolty = 0;   
	               if (dVolty > MathPow(len1,1.0/pow1)) dVolty = MathPow(len1,1.0/pow1);
                  if (dVolty < 1)                      dVolty = 1.0;

      //
      //
      //
      //
      //
	        
   	double pow2 = MathPow(dVolty, pow1);
      double len2 = MathSqrt(0.5*(length-1))*len1;
      double Kv   = MathPow(len2/(len2+1), MathSqrt(pow2));		
	
         if (del1 > 0) wrk[r][bsmax+s] = price; else wrk[r][bsmax+s] = price - Kv*del1;
         if (del2 < 0) wrk[r][bsmin+s] = price; else wrk[r][bsmin+s] = price - Kv*del2;

   //
   //
   //
   //
   //

      double R     = MathMax(MathMin(phase,100),-100)/100.0 + 1.5;
      double beta  = 0.45*(length-1)/(0.45*(length-1)+2);
      double alpha = MathPow(beta,pow2);

         wrk[r][0+s] = price + alpha*(wrk[r-1][0+s]-price);
         wrk[r][1+s] = (price - wrk[r][0+s])*(1-beta) + beta*wrk[r-1][1+s];
         wrk[r][2+s] = (wrk[r][0+s] + R*wrk[r][1+s]);
         wrk[r][3+s] = (wrk[r][2+s] - wrk[r-1][4+s])*MathPow((1-alpha),2) + MathPow(alpha,2)*wrk[r-1][3+s];
         wrk[r][4+s] = (wrk[r-1][4+s] + wrk[r][3+s]); 

   //
   //
   //
   //
   //

   return(wrk[r][4+s]);
}
int toInt(double value) { return(value); }   

//+------------------------------------------------------------------+
//|                                                                  |
//+------------------------------------------------------------------+  
//
//
//
//
//

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

          //
          //
          //
          //
          //

          message =  StringConcatenate(Symbol()," at ",TimeToStr(TimeLocal(),TIME_SECONDS)," ADXM trend changed to ",doWhat);
             if (alertsMessage) Alert(message);
             if (alertsEmail)   SendMail(StringConcatenate(Symbol(),"ADXM "),message);
             if (alertsSound)   PlaySound("alert2.wav");
      }
}