//+------------------------------------------------------------------+
//|                                                       adxvma.mq4 |
//+------------------------------------------------------------------+
#property copyright "mladen"
#property link      "www.forex-tsd.com"


#property indicator_separate_window
#property indicator_buffers 1
#property indicator_color1  PaleVioletRed
#property indicator_width1  2

//
//
//
//
//

extern double AdxVmaPeriod = 10;
extern int    AdxVmaPrice  = PRICE_MEDIAN;
extern bool   TrendMode    = true;

//
//
//
//
//

double adxvma[];
double trend[];

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

int init()
{
   IndicatorBuffers(2);
   SetIndexBuffer(0,trend);
   SetIndexBuffer(1,adxvma);

      AdxVmaPeriod      = MathMax(AdxVmaPeriod,1);
   IndicatorShortName("AdxVma for EA ("+DoubleToStr(AdxVmaPeriod,2)+")");
   return(0);
}
 
//+------------------------------------------------------------------+
//|                                                                  |
//+------------------------------------------------------------------+
//
//
//
//
//

double  work[][6];
#define prc 0
#define pdm 1
#define mdm 2
#define pdi 3
#define mdi 4
#define out 5

//
//
//
//
//

int start()
{
   int counted_bars = IndicatorCounted();
   int i,r,limit;

   if(counted_bars<0) return(-1);
   if(counted_bars>0) counted_bars--;
           limit=MathMin(Bars-counted_bars,Bars-1);
           if (ArrayRange(work,0)!=Bars) ArrayResize(work,Bars);

   //
   //
   //
   //
   //
   
   for(i = limit, r=Bars-i-1; i >= 0; i--,r++)
   {
      work[r][prc] = iMA(NULL,0,1,0,MODE_SMA,AdxVmaPrice,i);

         double diff = work[r][prc]-work[r-1][prc];
         double tpdm = 0;
         double tmdm = 0;
               if (diff>0)
                     tpdm =  diff;
               else  tmdm = -diff;          
         work[r][pdm] = ((AdxVmaPeriod-1.0)*work[r-1][pdm]+tpdm)/AdxVmaPeriod;
         work[r][mdm] = ((AdxVmaPeriod-1.0)*work[r-1][mdm]+tmdm)/AdxVmaPeriod;

         //
         //
         //
         //
         //

         double trueRange = work[r][pdm]+work[r][mdm];
         double tpdi      = 0;
         double tmdi      = 0;
               if (trueRange>0)
               {
                  tpdi = work[r][pdm]/trueRange;
                  tmdi = work[r][mdm]/trueRange;
               }            
         work[r][pdi] = ((AdxVmaPeriod-1.0)*work[r-1][pdi]+tpdi)/AdxVmaPeriod;
         work[r][mdi] = ((AdxVmaPeriod-1.0)*work[r-1][mdi]+tmdi)/AdxVmaPeriod;
   
         //
         //
         //
         //
         //
                  
         double tout  = 0; if ((work[r][pdi]+work[r][mdi])>0) tout = MathAbs(work[r][pdi]-work[r][mdi])/(work[r][pdi]+work[r][mdi]);
         work[r][out] = ((AdxVmaPeriod-1.0)*work[r-1][out]+tout)/AdxVmaPeriod;

         //
         //
         //
         //
         //
                 
         double thi = MathMax(work[r][out],work[r-1][out]);
         double tlo = MathMin(work[r][out],work[r-1][out]);
            for (int j = 2; j<AdxVmaPeriod; j++)
            {
               thi = MathMax(work[r-j][out],thi);
               tlo = MathMin(work[r-j][out],tlo);
            }            
         double vi = 0; if ((thi-tlo)>0) vi = (work[r][out]-tlo)/(thi-tlo);

      //
      //
      //
      //
      //
         
         adxvma[i]  = ((AdxVmaPeriod-vi)*adxvma[i+1]+vi*work[r][prc])/AdxVmaPeriod;
            if (TrendMode)
                  trend[i] = trend[i+1];
            else  trend[i] = 0;
            if (adxvma[i]>adxvma[i+1]) trend[i] =  1;
            if (adxvma[i]<adxvma[i+1]) trend[i] = -1;
      }
   return(0);
}