//+------------------------------------------------------------------+
//|                                    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    SmoothEMA               = 15;
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);
      SmoothEMA        = MathMax(0,SmoothEMA);
   IndicatorShortName("Dynamic zone DSS Bressert ("+StochasticLength+","+SmoothEMA+")");
   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);


   //
   //
   //
   //
   //
   
      double alpha1 = 2.0 / (1.0+SmoothEMA);
    	for(i = limit; i>=0; i--)
      {
         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);           
}