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

#property indicator_separate_window
#property indicator_buffers 4
#property indicator_color1  PaleVioletRed
#property indicator_color2  DeepSkyBlue
#property indicator_color3  DimGray
#property indicator_color4  DeepSkyBlue
#property indicator_width4  2
#property indicator_level1  0
#property indicator_levelcolor DarkSlateGray
#property indicator_style3  STYLE_DOT

//
//
//
//
//

#import "dynamicZone.dll"
   double dzBuyP(double& sourceArray[],double probabiltyValue, int lookBack, int bars, int i,double precision);
   double dzSellP(double& sourceArray[],double probabiltyValue, int lookBack, int bars, int i,double precision);
#import

//
//
//
//
//

extern string TimeFrame              = "current time frame";
extern int    Length                 = 14;
extern int    Price                  =  0;
extern int    DzLookBackBars         = 70;
extern double DzStartBuyProbability  = 0.15;
extern double DzStartSellProbability = 0.15;
extern bool   Interpolate            = true;

//
//
//
//
//

double vol[];
double sli[];
double bli[];
double zli[];
string indicatorFileName;
bool   returnBars;
bool   calculateValue;
int    timeFrame;

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

int init()
{
   SetIndexBuffer(0,bli);
   SetIndexBuffer(1,sli);
   SetIndexBuffer(2,zli);
   SetIndexBuffer(3,vol);

      //
      //
      //
      //
      //
      
         indicatorFileName = WindowExpertName();
         calculateValue    = (TimeFrame=="calculateValue"); if (calculateValue) return(0);
         returnBars        = (TimeFrame=="returnBars");     if (returnBars)     return(0);
         timeFrame         = stringToTimeFrame(TimeFrame);
   IndicatorShortName(timeFrameToString(timeFrame)+" Jurik volty ("+Length+")");
   return(0);
}

//
//
//
//
//

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

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

   //
   //
   //
   //
   //
   
   if (calculateValue || timeFrame == Period())
   {
      for(i=limit; i>=0; i--)
      {
         vol[i] = iVolty(iMA(NULL,0,1,0,MODE_SMA,Price,i),Length,i);
         bli[i] = dzBuyP (vol, DzStartBuyProbability,  DzLookBackBars, Bars, i,0.0001);
         sli[i] = dzSellP(vol, DzStartSellProbability, DzLookBackBars, Bars, i,0.0001);
         zli[i] = dzSellP(vol, 0.5,                    DzLookBackBars, Bars, i,0.0001);
      }      
      return(0);
   }
   
   //
   //
   //
   //
   //

   limit = MathMax(limit,MathMin(Bars-1,iCustom(NULL,timeFrame,indicatorFileName,"returnBars",0,0)));
   for(i=limit; i>=0; i--)
   {
      int y = iBarShift(NULL,timeFrame,Time[i]);
         bli[i] = iCustom(NULL,timeFrame,indicatorFileName,"calculateValue",Length,Price,DzLookBackBars,DzStartBuyProbability,DzStartSellProbability,0,y);
         sli[i] = iCustom(NULL,timeFrame,indicatorFileName,"calculateValue",Length,Price,DzLookBackBars,DzStartBuyProbability,DzStartSellProbability,1,y);
         zli[i] = iCustom(NULL,timeFrame,indicatorFileName,"calculateValue",Length,Price,DzLookBackBars,DzStartBuyProbability,DzStartSellProbability,2,y);
         vol[i] = iCustom(NULL,timeFrame,indicatorFileName,"calculateValue",Length,Price,DzLookBackBars,DzStartBuyProbability,DzStartSellProbability,3,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(int k = 1; k < n; k++)
            {
               bli[i+k] = bli[i]  + (bli[i+n] -bli[i])*k/n;
               sli[i+k] = sli[i]  + (sli[i+n] -sli[i])*k/n;
               zli[i+k] = zli[i]  + (zli[i+n] -zli[i])*k/n;
               vol[i+k] = vol[i]  + (vol[i+n] -vol[i])*k/n;
            }               
   }
   return(0);
}

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

double wrk[][5];
#define bsmax  0
#define bsmin  1
#define volty  2
#define vsum   3
#define avolty 4
#define avgLen 65

//
//
//
//
//

double iVolty(double price, double length, int i)
{
   if (ArrayRange(wrk,0) != Bars) ArrayResize(wrk,Bars);
   
   int r = Bars-i-1; if (r==0) { for(int k=0; k<5; k++) wrk[0][k]=0; return(0); }

   //
   //
   //
   //
   //
   
      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];
      double del2 = price - wrk[r-1][bsmin];
	
         wrk[r][volty] = 0;
               if(MathAbs(del1) > MathAbs(del2)) wrk[r][volty] = MathAbs(del1); 
               if(MathAbs(del1) < MathAbs(del2)) wrk[r][volty] = MathAbs(del2); 
         wrk[r][vsum] =	wrk[r-1][vsum] + 0.1*(wrk[r][volty]-wrk[r-10][volty]);
   
         double avg = wrk[r][vsum];  for (k=1; k<avgLen && (r-k)>=0 ; k++) avg += wrk[r-k][vsum];
                                                                           avg /= k;
         wrk[r][avolty] = avg;                                           
            if (wrk[r][avolty] > 0)
               double dVolty = wrk[r][volty]/wrk[r][avolty]; 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] = price; else wrk[r][bsmax] = price - Kv*del1;
         if (del2 < 0) wrk[r][bsmin] = price; else wrk[r][bsmin] = price - Kv*del2;

   //
   //
   //
   //
   //
 
   return(wrk[r][vsum]);
}

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

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);
}