//+------------------------------------------------------------------+
//|                                                    adx trend.mq4 |
//+------------------------------------------------------------------+
#property copyright "mladen"
#property link      "www.forex-tsd.com"

#property indicator_separate_window
#property indicator_buffers 4
#property indicator_color1  DeepSkyBlue  
#property indicator_color2  Orange
#property indicator_color3  DeepSkyBlue  
#property indicator_color4  Orange
#property indicator_level1  0
#property indicator_levelcolor DimGray

//
//
//
//
//

extern string TimeFrame    = "Current time frame";
extern double PTPeriod     = 10;
extern int    PTPrice      = PRICE_MEDIAN;
extern bool   Interpolate  = true;

//
//
//
//
//

double ptlup[];
double ptldn[];
double pthup[];
double pthdn[];

//
//
//
//
//

string indicatorFileName;
bool   calculateValue;
bool   returnBars;
int    timeFrame;

//+------------------------------------------------------------------+
//|                                                                  |
//+------------------------------------------------------------------+
//
//
//
//
//

int init()
{
   SetIndexBuffer(0,pthup); SetIndexStyle(0,DRAW_HISTOGRAM);
   SetIndexBuffer(1,pthdn); SetIndexStyle(1,DRAW_HISTOGRAM);
   SetIndexBuffer(2,ptlup);
   SetIndexBuffer(3,ptldn);

   //
   //
   //
   //
   //

      indicatorFileName = WindowExpertName();
      calculateValue    = (TimeFrame=="CalculateValue"); if (calculateValue) return(0);
      returnBars        = (TimeFrame=="returnBars");     if (returnBars)     return(0);
      timeFrame         = stringToTimeFrame(TimeFrame);

   //
   //
   //
   //
   //
   
   IndicatorShortName(timeFrameToString(timeFrame)+" adx trend ("+DoubleToStr(PTPeriod,2)+")");
   return(0);
}
 
//+------------------------------------------------------------------+
//|                                                                  |
//+------------------------------------------------------------------+
//
//
//
//
//

double  work[][5];
#define prc 0
#define pdm 1
#define mdm 2
#define pdi 3
#define mdi 4

//
//
//
//
//

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 (returnBars) { pthup[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][prc] = iMA(NULL,0,1,0,MODE_SMA,PTPrice,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] = ((PTPeriod-1.0)*work[r-1][pdm]+tpdm)/PTPeriod;
         work[r][mdm] = ((PTPeriod-1.0)*work[r-1][mdm]+tmdm)/PTPeriod;

         //
         //
         //
         //
         //

         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] = ((PTPeriod-1.0)*work[r-1][pdi]+tpdi)/PTPeriod;
         work[r][mdi] = ((PTPeriod-1.0)*work[r-1][mdi]+tmdi)/PTPeriod;
   
         //
         //
         //
         //
         //
         
         ptlup[i] = work[r][pdi]-0.5;
         ptldn[i] = work[r][mdi]-0.5;
         pthup[i] = EMPTY_VALUE;
         pthdn[i] = EMPTY_VALUE;
            if (ptlup[i]>ptldn[i]) pthup[i] = ptlup[i];
            if (ptlup[i]<ptldn[i]) pthdn[i] = ptldn[i];
      }
      return(0);
   }
   
   //
   //
   //
   //
   //
   
   limit = MathMax(limit,MathMin(Bars,iCustom(NULL,timeFrame,indicatorFileName,"returnBars",0,0)*timeFrame/Period()));
   for (i=limit;i>=0;i--)
   {
      int y = iBarShift(NULL,timeFrame,Time[i]);
         pthup[i] = iCustom(NULL,timeFrame,indicatorFileName,"CalculateValue",PTPeriod,PTPrice,0,y);
         pthdn[i] = iCustom(NULL,timeFrame,indicatorFileName,"CalculateValue",PTPeriod,PTPrice,1,y);
         ptlup[i] = iCustom(NULL,timeFrame,indicatorFileName,"CalculateValue",PTPeriod,PTPrice,2,y);
         ptldn[i] = iCustom(NULL,timeFrame,indicatorFileName,"CalculateValue",PTPeriod,PTPrice,3,y);

         //
         //
         //
         //
         //
      
            if (timeFrame <= Period() || y==iBarShift(NULL,timeFrame,Time[i-1])) continue;
            if (!Interpolate) continue;

         //
         //
         //
         //
         //

         datetime time = iTime(NULL,timeFrame,y);
            for(int n = 1; i+n < Bars && Time[i+n] >= time; n++) continue;	
            for(int k = 1; k < n; k++)
            {
               ptlup[i+k] = ptlup[i] + (ptlup[i+n]-ptlup[i])*k/n;
               ptldn[i+k] = ptldn[i] + (ptldn[i+n]-ptldn[i])*k/n;
                  if (pthup[i] != EMPTY_VALUE) pthup[i+k] = ptlup[i+k];
                  if (pthdn[i] != EMPTY_VALUE) pthdn[i+k] = ptldn[i+k];
            }
   }
   
   //
   //
   //
   //
   //
      
   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 char = StringGetChar(s, length);
         if((char > 96 && char < 123) || (char > 223 && char < 256))
                     s = StringSetChar(s, length, char - 32);
         else if(char > -33 && char < 0)
                     s = StringSetChar(s, length, char + 224);
   }
   return(s);
}