//+------------------------------------------------------------------+
//|                                                        DV(2).mq4 |
//|                                                                  |
//| DV(2) originally developed by David Varadi                       |
//+------------------------------------------------------------------+
#property copyright "mladen"
#property link      "mladenfx@gmail.com"

#property indicator_separate_window
#property indicator_buffers 1
#property indicator_color1  DeepSkyBlue
#property indicator_width1  2
#property indicator_minimum 0
#property indicator_maximum 100
#property indicator_level1  50
#property indicator_levelcolor DimGray

//
//
//
//
//

extern int DviPeriod = 252;
extern int DviPrice  = PRICE_CLOSE;

//
//
//
//
//

double dvi[];
double xf[];
double xg[];
double xh[];
double xc[];
double xd[];
double xe[];
double prices[];

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

int init()
{
   IndicatorBuffers(8);
      SetIndexBuffer(0,dvi);
      SetIndexBuffer(1,xf);
      SetIndexBuffer(2,xg);
      SetIndexBuffer(3,xh);
      SetIndexBuffer(4,xc);
      SetIndexBuffer(5,xd);
      SetIndexBuffer(6,xe);
      SetIndexBuffer(7,prices);
   IndicatorShortName("DVI");
   return(0);
}

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

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

   if(counted_bars<0) return(-1);
   if(counted_bars>0) counted_bars--;
         limit = MathMin(Bars-counted_bars,Bars-3);

   //
   //
   //
   //
   //

   for (i=limit; i>=0; i--)
   {
      prices[i] = iMA(NULL,0,1,0,MODE_SMA,DviPrice,i);
      xf[i]=xf[i+1];
            if (prices[i]>prices[i+1]) xf[i] =  1;
            if (prices[i]<prices[i+1]) xf[i] = -1;
              
      xg[i] = (sum(xf,10,i)+sum(xf,100,i)/10.0)/2.0;
      xh[i] =  sma(xg,2,i);
      
            double stretch = percentRank(xh,DviPeriod,i);

      xc[i] = prices[i]/sma(prices,3,i)-1;
      xd[i] = (sma(xc,5,i)+sma(xc,100,i)/10.0)/2.0;
      xe[i] =  sma(xd,5,i);

            double magnitude = percentRank(xe,DviPeriod,i);

      dvi[i] = 0.8*magnitude+0.2*stretch;            
   }

   //
   //
   //
   //
   //
      
   return(0);      
}


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

double percentRank(double& array[], double length, int i)
{
   double count = 0;
      for (int k=0; k<length; k++) if (array[i]>array[i+k+1]) count++;
   return(100.0*count/length);
}
double sma(double& array[], int length, int i)
{
   double sum = array[i];
      for (int k=1; k<length; k++) sum += array[i+k];
   return(sum/length);
}
double sum(double& array[], int length, int i)
{
   double sum = array[i];
      for (int k=1; k<length; k++) sum += array[i+k];
   return(sum);
}