//+------------------------------------------------------------------+
//|                                    Dynamic zone DSS Bressert.mq4 |
//|                                                           mladen |
//+------------------------------------------------------------------+
#property copyright "mladen"
#property link      "mladenfx@gmail.com"

#property indicator_separate_window
#property indicator_buffers 6
#property indicator_color1  DeepSkyBlue
#property indicator_color2  LimeGreen
#property indicator_color3  LimeGreen
#property indicator_color4  Red
#property indicator_color5  Red
#property indicator_color6  DimGray
#property indicator_style3  STYLE_DOT
#property indicator_style4  STYLE_DOT
#property indicator_style6  STYLE_DOT
#property indicator_width1  2
#property indicator_width2  2
#property indicator_minimum 0
#property indicator_maximum 100

//
//
//
//
//

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

//
//
//
//
//

extern int    StochasticLength        = 55;
extern int    SmoothJMA               = 15;
extern double Phase                   = 0;
extern int    DzLookBackBars          = 70;
extern double DzStartBuyProbability1  = 0.10;
extern double DzStartBuyProbability2  = 0.25;
extern double DzStartSellProbability1 = 0.10;
extern double DzStartSellProbability2 = 0.25;

//
//
//
//
//

double dssBuffer[];
double sigBuffer[];
double bl1Buffer[];
double bl2Buffer[];
double sl1Buffer[];
double sl2Buffer[];
double zliBuffer[];
double st1Buffer[];
double ss1Buffer[];


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

int init()
{
   IndicatorBuffers(8);
   SetIndexBuffer(0,dssBuffer);
   SetIndexBuffer(1,bl1Buffer);
   SetIndexBuffer(2,bl2Buffer);
   SetIndexBuffer(3,sl2Buffer);
   SetIndexBuffer(4,sl1Buffer);
   SetIndexBuffer(5,zliBuffer);
   SetIndexBuffer(6,st1Buffer);
   SetIndexBuffer(7,ss1Buffer);
      StochasticLength = MathMax(1,StochasticLength);
      
   IndicatorShortName("Dynamic zone DJSS Bressert ("+StochasticLength+","+SmoothJMA+")");
   return(0);
}
int deinit(){ return(0); }




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

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

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


   //
   //
   //
   //
   //
   
     
    	for(i = limit; i>=0; i--)
      {
         SmoothJMA     = iSmooth(iMA(NULL,0,1,0,MODE_SMA,PRICE_CLOSE,i),SmoothJMA,Phase,i);
         double alpha1 = 2.0 / (1.0+SmoothJMA);
         double close  = iMA(NULL,0,1,0,MODE_SMA,PRICE_CLOSE,i);

         //
         //
         //
         //
         //
      
            double min = iMA(NULL,0,1,0,MODE_SMA,PRICE_LOW ,i);
            double max = iMA(NULL,0,1,0,MODE_SMA,PRICE_HIGH,i);
            for (int k=1; k<StochasticLength; k++)
            {
               min = MathMin(min,iMA(NULL,0,1,0,MODE_SMA,PRICE_LOW ,i+k));
               max = MathMax(max,iMA(NULL,0,1,0,MODE_SMA,PRICE_HIGH,i+k));
            }
      
            st1Buffer[i] = 0; if (min!=max) st1Buffer[i] = 100*(close-min)/(max-min);
               if (i==(Bars-1))
               {
                  ss1Buffer[i] = st1Buffer[i];
                  dssBuffer[i] = st1Buffer[i];
                  sigBuffer[i] = st1Buffer[i];
                  continue;
               }            
            ss1Buffer[i] = ss1Buffer[i+1]+alpha1*(st1Buffer[i]-ss1Buffer[i+1]);

         //
         //
         //
         //
         //
      
            min = ss1Buffer[i];
            max = ss1Buffer[i];
            for (k=1; k<StochasticLength; k++)
            {
               min = MathMin(min,ss1Buffer[i+k]);
               max = MathMax(max,ss1Buffer[i+k]);
            }
            double stoch = 0; if (min!=max) stoch = 100*(ss1Buffer[i]-min)/(max-min);
            
            //
            //
            //
            //
            //
            
            dssBuffer[i] = dssBuffer[i+1]+alpha1*(stoch       -dssBuffer[i+1]);
               if (DzStartBuyProbability1 >0) bl1Buffer[i] = dzBuy (dssBuffer, DzStartBuyProbability1,  DzLookBackBars, Bars, i);
               if (DzStartBuyProbability2 >0) bl2Buffer[i] = dzBuy (dssBuffer, DzStartBuyProbability2,  DzLookBackBars, Bars, i);
               if (DzStartSellProbability1>0) sl1Buffer[i] = dzSell(dssBuffer, DzStartSellProbability1, DzLookBackBars, Bars, i);
               if (DzStartSellProbability2>0) sl2Buffer[i] = dzSell(dssBuffer, DzStartSellProbability2, DzLookBackBars, Bars, i);
                                              zliBuffer[i] = dzSell(dssBuffer, 0.5                    , DzLookBackBars, Bars, i);
      }
   return(0);           
}


double wrk[][10];

#define bsmax  5
#define bsmin  6
#define volty  7
#define vsum   8
#define avolty 9

//
//
//
//
//

double iSmooth(double price, double length, double phase, int i)
{
   if (ArrayRange(wrk,0) != Bars) ArrayResize(wrk,Bars);
   
   int r = Bars-i-1; 
      if (r==0) { for(int k=0; k<7; k++) wrk[0][k]=price; for(; k<10; k++) wrk[0][k]=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];
      double del2   = price - wrk[r-1][bsmin];
      double div    = 1.0/(10.0+10.0*(MathMin(MathMax(length-10,0),100))/100);
      int    forBar = MathMin(r,10);
	
         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] + (wrk[r][volty]-wrk[r-forBar][volty])*div;
   
         //
         //
         //
         //
         //
   
         wrk[r][avolty] = wrk[r-1][avolty]+(2.0/(MathMax(4.0*length,30)+1.0))*(wrk[r][vsum]-wrk[r-1][avolty]);
            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;
	
   //
   //
   //
   //
   //
      
      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] = price + alpha*(wrk[r-1][0]-price);
         wrk[r][1] = (price - wrk[r][0])*(1-beta) + beta*wrk[r-1][1];
         wrk[r][2] = (wrk[r][0] + R*wrk[r][1]);
         wrk[r][3] = (wrk[r][2] - wrk[r-1][4])*MathPow((1-alpha),2) + MathPow(alpha,2)*wrk[r-1][3];
         wrk[r][4] = (wrk[r-1][4] + wrk[r][3]); 

   //
   //
   //
   //
   //

   return(wrk[r][4]);
}

