//+------------------------------------------------------------------+
//|                                                  OnChart WPR.mq4 |
//|                                                           mladen |
//|                                                                  |
//+------------------------------------------------------------------+
#property copyright "mladen"
#property link      ""

#property indicator_chart_window
#property indicator_buffers 4
#property indicator_color1 Crimson
#property indicator_color2 Goldenrod
#property indicator_color3 Goldenrod
#property indicator_color4 DeepSkyBlue
#property indicator_width4 2
#property indicator_style1 STYLE_DASH

//
//
//
//
//

extern string timeFrame             = "Current time frame";
extern int    ShortLimit            = 10;
extern int    LongLimit             = 20;
extern int    CfbNormLength         = 50;
extern int    CfbDepth              = 6;
extern int    CfbPrice              = PRICE_WEIGHTED;
extern int    CfbSmooth             = 8;
extern double CfbResultSmooth       = 2;
extern double CfbResultSmoothPhase  = 0.0;
extern bool   CfbResultSmoothDouble = true;
extern double SmoothLength          = 5;
extern double SmoothPhase           = 0;  
extern double Length                = 120;
extern double Phase                 = 0;
extern int    overBought            = -20;
extern int    overSold              = -80;
extern bool   SmoothDouble          = true;
extern bool   interpolate           = true;

//
//
//
//
//

double ExtMapBuffer1[];
double ExtMapBuffer2[];
double ExtMapBuffer3[];
double ExtMapBuffer4[];
int    TimeFrame;
int    atrTimeFrame;

//+------------------------------------------------------------------+
//|                                                                  |
//+------------------------------------------------------------------+
//
//
//
//
//

int init()
{
   SetIndexBuffer(0,ExtMapBuffer1);
   SetIndexBuffer(1,ExtMapBuffer2);
   SetIndexBuffer(2,ExtMapBuffer3);
   SetIndexBuffer(3,ExtMapBuffer4);
   

   //
   //
   //
   //
   //
   
   TimeFrame = stringToTimeFrame(timeFrame);
   atrTimeFrame=PERIOD_D1;
   if (TimeFrame >= atrTimeFrame)
      switch (TimeFrame)
         {
            case PERIOD_D1: atrTimeFrame = PERIOD_W1; break;
            default:        atrTimeFrame = PERIOD_MN1;
         }
   return(0);
}
int deinit() { return(0); }

//+------------------------------------------------------------------+
//|                                                                  |
//+------------------------------------------------------------------+
//
//
//
//
//
//

int start()
{
   double calcSmoothLength = MathSqrt(SmoothLength); 
   int counted_bars=IndicatorCounted();
   int limit,i;
   
   if(counted_bars<0) return(-1);
   if(counted_bars>0) counted_bars--;
      limit=MathMax(MathMin(Bars-counted_bars,Bars-1),TimeFrame/Period());   
      

   //
   //
   //
   //
   //
   
      for (i=limit; i>=0; i--)
      {
      int y           = iBarShift(NULL,TimeFrame   ,Time[i]);
      int r           = iBarShift(NULL,atrTimeFrame,Time[i]);
      double avgRange = iATR(NULL,atrTimeFrame,Length,r);
      double maValue  = iDSmooth(iMA(NULL,TimeFrame,1,0,MODE_SMA,PRICE_MEDIAN,y),Length,Phase,y);
      double wprValue = iCustom(NULL,TimeFrame,"Cfb adaptive wprsmooth correct",ShortLimit,LongLimit,CfbNormLength,CfbDepth,CfbPrice,CfbSmooth,CfbResultSmooth,CfbResultSmoothPhase,CfbResultSmoothDouble,SmoothLength,SmoothPhase,SmoothDouble,0,y);
            
               
            //
            //
            //
            //
            //
               
            ExtMapBuffer1[i] = maValue;
            ExtMapBuffer2[i] = maValue+(avgRange*(overBought+50)/100);
            ExtMapBuffer3[i] = maValue-(avgRange*(-50-overSold)/100);
            ExtMapBuffer4[i] = maValue+(wprValue+50)/100*avgRange;
               
            //
            //
            //
            //
            //
               
            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;	
              double factor = 1.0 / n;
              for(int k = 1; k < n; k++)
              {
                 ExtMapBuffer1[i+k] = k*factor*ExtMapBuffer1[i+n] + (1.0-k*factor)*ExtMapBuffer1[i];
                 ExtMapBuffer2[i+k] = k*factor*ExtMapBuffer2[i+n] + (1.0-k*factor)*ExtMapBuffer2[i];
                 ExtMapBuffer3[i+k] = k*factor*ExtMapBuffer3[i+n] + (1.0-k*factor)*ExtMapBuffer3[i];
                 ExtMapBuffer4[i+k] = k*factor*ExtMapBuffer4[i+n] + (1.0-k*factor)*ExtMapBuffer4[i];
                 
              }             
         }
   
   //
   //
   //
   //
   //
         
   return(0);
}

//+------------------------------------------------------------------+
//+------------------------------------------------------------------+
//
//
//
//
//

int stringToTimeFrame(string tfs)
{
   int tf=0;
       tfs = StringUpperCase(tfs);
         if (tfs=="M1" || tfs=="1")     tf=PERIOD_M1;
         if (tfs=="M5" || tfs=="5")     tf=PERIOD_M5;
         if (tfs=="M15"|| tfs=="15")    tf=PERIOD_M15;
         if (tfs=="M30"|| tfs=="30")    tf=PERIOD_M30;
         if (tfs=="H1" || tfs=="60")    tf=PERIOD_H1;
         if (tfs=="H4" || tfs=="240")   tf=PERIOD_H4;
         if (tfs=="D1" || tfs=="1440")  tf=PERIOD_D1;
         if (tfs=="W1" || tfs=="10080") tf=PERIOD_W1;
         if (tfs=="MN" || tfs=="43200") tf=PERIOD_MN1;
         if (tf<Period()) tf=Period();
  return(tf);
}

//
//
//
//
//

string StringUpperCase(string str)
{
   string   s = str;
   int      lenght = StringLen(str) - 1;
   int      tchar;
   
   while(lenght >= 0)
      {
         tchar = StringGetChar(s, lenght);
         
         //
         //
         //
         //
         //
         
         if((tchar > 96 && tchar < 123) || (tchar > 223 && tchar < 256))
                  s = StringSetChar(s, lenght, tchar - 32);
          else 
              if(tchar > -33 && tchar < 0)
                  s = StringSetChar(s, lenght, tchar + 224);
                  
         //
         //
         //
         //
         //
                                 
         lenght--;
   }
   
   //
   //
   //
   //
   //
   
   return(s);
}


double wrk[][20];

#define bsmax  5
#define bsmin  6
#define volty  7
#define vsum   8
#define avolty 9

//
//
//
//
//

double iDSmooth(double price, double length, double phase, int i, int s=0)
{
   if (SmoothDouble)
         return (iSmooth(iSmooth(price,MathSqrt(length),phase,i,s),MathSqrt(length),phase,i,s+10));
   else  return (iSmooth(price,length,phase,i,s));
}

//
//
//
//
//

double iSmooth(double price, double length, double phase, int i, int s=0)
{
   if (length <=1) return(price);
   if (ArrayRange(wrk,0) != Bars) ArrayResize(wrk,Bars);
   
   int r = Bars-i-1; 
      if (r==0) { for(int k=0; k<7; k++) wrk[r][k+s]=price; for(; k<10; k++) wrk[r][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];
      double div    = 1.0/(10.0+10.0*(MathMin(MathMax(length-10,0),100))/100);
      int    forBar = MathMin(r,10);
	
         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] + (wrk[r][volty+s]-wrk[r-forBar][volty+s])*div;
         
         //
         //
         //
         //
         //
   
         wrk[r][avolty+s] = wrk[r-1][avolty+s]+(2.0/(MathMax(4.0*length,30)+1.0))*(wrk[r][vsum+s]-wrk[r-1][avolty+s]);
            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]);
}  


