//+------------------------------------------------------------------+
//|                                                       vortex.mq4 |
//+------------------------------------------------------------------+
#property copyright "mladen"
#property link      "mladenfx@gmail.com"

#property indicator_separate_window
#property indicator_buffers 2
#property indicator_color1  DeepSkyBlue
#property indicator_color2  Red
#property indicator_width1  2
#property indicator_width2  2

//
//
//
//
//

extern string TimeFrame      = "current time frame";
extern int    VortexPeriod   = 14;
extern bool   ShowDifference = false;
extern bool   Interpolate    = true;

//
//
//
//
//

double vorUp[];
double vorDn[];
double work[][6];

//
//
//
//
//

string IndicatorFileName;
int    timeFrame;
bool   calculateValue;
bool   returnBars;


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

int init()
{
   SetIndexBuffer(0,vorUp);
   SetIndexBuffer(1,vorDn);
   if (ShowDifference)
      {
         SetIndexStyle(0,DRAW_HISTOGRAM);
         SetIndexStyle(1,DRAW_HISTOGRAM);
      }
   else      
      {
         SetIndexStyle(0,DRAW_LINE);
         SetIndexStyle(1,DRAW_LINE);
      }
   
      //
      //
      //
      //
      //
      
         IndicatorFileName = WindowExpertName();
         calculateValue    = (TimeFrame=="calculateValue"); if (calculateValue) return(0);
         returnBars        = (TimeFrame=="returnBars");     if (returnBars)     return(0);
         timeFrame         = stringToTimeFrame(TimeFrame);

      //
      //
      //
      //
      //
         
   IndicatorShortName(timeFrameToString(timeFrame)+" Vortex ("+VortexPeriod+")");         
   return(0);
}

int deinit()
{
   return(0);
}

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

#define _rng 0
#define _vmp 1
#define _vmm 2
#define _vop 3
#define _vom 4
#define _dif 5

//
//
//
//
//

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

   if(counted_bars < 0)   return(-1);
   if(counted_bars > 0)   counted_bars--;
           limit = MathMin(Bars-counted_bars,Bars-1);
           if (returnBars) { vorUp[0] = limit+1; return(0); }
           
   //
   //
   //
   //
   //

   if (calculateValue || timeFrame==Period())
   {
      if (ArrayRange(work,0) != Bars) ArrayResize(work,Bars);
   
      //
      //
      //
      //
      //
      
      for(i = limit, r = Bars-i-1; i >= 0; i--,r++)
      {
         work[r][_rng] = MathMax(High[i],Close[i+1])-MathMin(Low[i],Close[i+1]);
         work[r][_vmp] = MathAbs(High[i] - Low[i+1]);
         work[r][_vmm] = MathAbs(Low[i] - High[i+1]);
         work[r][_vop] = EMPTY_VALUE;
         work[r][_vom] = EMPTY_VALUE;

         if ((Bars-i)<VortexPeriod) continue;
         
         //
         //
         //
         //
         //
         
            double vmpSum = 0;
            double vmmSum = 0;
            double rngSum = 0;
               for (int k=0; k<VortexPeriod; k++)
               {
                  vmpSum += work[r-k][_vmp];
                  vmmSum += work[r-k][_vmm];
                  rngSum += work[r-k][_rng];
               }
               if (rngSum !=0)
               {
                  work[r][_vop] = vmpSum/rngSum;
                  work[r][_vom] = vmmSum/rngSum;
               }
            work[r][_dif] = work[r][_vop]-work[r][_vom];
            
            //
            //
            //
            //
            //
            
            if (ShowDifference)
            {
               vorUp[i] = EMPTY_VALUE;
               vorDn[i] = EMPTY_VALUE;
                  if (work[r][_dif]>0) vorUp[i]=work[r][_dif];
                  if (work[r][_dif]<0) vorDn[i]=work[r][_dif];
            }
            else
            {
               vorUp[i] = work[r][_vop];
               vorDn[i] = work[r][_vom];
            }
      }
      return(0);
   }         
   
   //
   //
   //
   //
   //
   
   limit = MathMax(limit,MathMin(Bars,iCustom(NULL,timeFrame,IndicatorFileName,"returnBars",1,0)*timeFrame/Period()));
   for (i=limit;i>=0;i--)
   {
      int y = iBarShift(NULL,timeFrame,Time[i]);
         vorUp[i] = iCustom(NULL,timeFrame,IndicatorFileName,"calculateValue",VortexPeriod,ShowDifference,0,y);
         vorDn[i] = iCustom(NULL,timeFrame,IndicatorFileName,"calculateValue",VortexPeriod,ShowDifference,1,y);

         //
         //
         //
         //
         //
      
         if (!Interpolate || y==iBarShift(NULL,timeFrame,Time[i-1])) continue;

         //
         //
         //
         //
         //

         datetime time = iTime(NULL,timeFrame,y);
            for(int n = 1; i+n < Bars && Time[i+n] >= time; n++) continue;	
            for(k = 1; k < n; k++)
            {
               if (vorUp[i] != EMPTY_VALUE && vorUp[i+n] != EMPTY_VALUE) vorUp[i+k] = vorUp[i] + (vorUp[i+n] - vorUp[i])*k/n;
               if (vorDn[i] != EMPTY_VALUE && vorDn[i+n] != EMPTY_VALUE) vorDn[i+k] = vorDn[i] + (vorDn[i+n] - vorDn[i])*k/n;
            }               
   }

   //
   //
   //
   //
   //

   return(0);      
}

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

string sTfTable[] = {"M1","M5","M15","M30","H1","H4","D1","W1","MN"};
int    iTfTable[] = {1,5,15,30,60,240,1440,10080,43200};

//
//
//
//
//

int stringToTimeFrame(string tfs)
{
   tfs = StringUpperCase(tfs);
   for (int i=ArraySize(iTfTable)-1; i>=0; i--)
         if (tfs==sTfTable[i] || tfs==""+iTfTable[i]) return(MathMax(iTfTable[i],Period()));
                                                      return(Period());
}
string timeFrameToString(int tf)
{
   for (int i=ArraySize(iTfTable)-1; i>=0; i--) 
         if (tf==iTfTable[i]) return(sTfTable[i]);
                              return("");
}

//
//
//
//
//

string StringUpperCase(string str)
{
   string   s = str;

   for (int length=StringLen(str)-1; length>=0; length--)
   {
      int char1 = StringGetChar(s, length);
         if((char1 > 96 && char1 < 123) || (char1 > 223 && char1 < 256))
                     s = StringSetChar(s, length, char1 - 32);
         else if(char1 > -33 && char1 < 0)
                     s = StringSetChar(s, length, char1 + 224);
   }
   return(s);
}