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

#property indicator_chart_window
#property indicator_buffers 5
#property indicator_color1  clrLimeGreen
#property indicator_color2  clrOrange
#property indicator_color3  clrOrange
#property indicator_color4  clrDeepSkyBlue
#property indicator_color5  clrPaleVioletRed
#property indicator_style4  STYLE_DOT
#property indicator_style5  STYLE_DOT
#property strict

//
//
//
//
//

#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 double             Length            = 20;   // Bband Ma Length
extern ENUM_MA_METHOD     MaMethod          = 0;    // Bband Ma Method
extern ENUM_APPLIED_PRICE Price             = 0;    // Bband Price
extern double             Deviation         = 0.5;  // Bband standard deviation
extern int                DzLookBackBars    = 35;   // Dynamic zone lookback
extern double             DzBuyProbability  = 0.1;  // Dynamic zone buy probability
extern double             DzSellProbability = 0.1;  // Dynamic zone sell probability
extern int                LinesWidth        = 3;    // Bband line width 

//
//
//
//
//

double bbLine[];
double buffer2[];
double buffer3[];
double up[];
double dn[];
double smax[];
double smin[];
double trend[];

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

int init()
{
   IndicatorBuffers(8);
      SetIndexBuffer(0,bbLine);  SetIndexStyle(0,DRAW_LINE,EMPTY,LinesWidth);
      SetIndexBuffer(1,buffer2); SetIndexStyle(1,DRAW_LINE,EMPTY,LinesWidth);
      SetIndexBuffer(2,buffer3); SetIndexStyle(2,DRAW_LINE,EMPTY,LinesWidth); 
      SetIndexBuffer(3,up);
      SetIndexBuffer(4,dn);
      SetIndexBuffer(5,smax);
      SetIndexBuffer(6,smin);
      SetIndexBuffer(7,trend);
   IndicatorShortName("bollinger band stops line("+(string)Length+","+(string)Deviation+")");
return(0);
}
 
//+------------------------------------------------------------------+
//|                                                                  |
//+------------------------------------------------------------------+
//
//
//
//
//

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

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

   //
   //
   //
   //
   //
   
   if (trend[limit]==-1) CleanPoint(limit,buffer2,buffer3);
   for(i=limit; i>=0; i--)
   { 
      double StdDev   = iStdDev(NULL,0,(int)Length,0,MODE_SMA,Price,i);
      double ma       = iMA(NULL,0,(int)Length,0,MaMethod,Price,i);
             smax[i]  = ma + (StdDev*Deviation);
             smin[i]  = ma - (StdDev*Deviation);
              buffer2[i] = EMPTY_VALUE;
              buffer3[i] = EMPTY_VALUE;
              if (i<(Bars-1))
              {
                trend[i]    = trend[i+1]; 
	               if (Close[i] > smax[i+1]) trend[i]= 1; 
	               if (Close[i] < smin[i+1]) trend[i]=-1;
	               if (trend[i] > 0 && smin[i] < smin[i+1]) smin[i] = smin[i+1];
	               if (trend[i] < 0 && smax[i] > smax[i+1]) smax[i] = smax[i+1];
                  if (trend[i] == 1) bbLine[i] = smin[i];
                  if (trend[i] ==-1) bbLine[i] = smax[i]; 
                  if (trend[i]==-1) PlotPoint(i,buffer2,buffer3,bbLine);     
               }
                  up[i] = dzSellP(bbLine,DzSellProbability,DzLookBackBars,Bars,i,0.00001);
                  dn[i] = dzBuyP (bbLine,DzBuyProbability ,DzLookBackBars,Bars,i,0.00001);
      }
return(0);
}

//-------------------------------------------------------------------
//                                                                  
//-------------------------------------------------------------------
//
//
//
//
//

void CleanPoint(int i,double& first[],double& second[])
{
   if (i>=Bars-3) return;
   if ((second[i]  != EMPTY_VALUE) && (second[i+1] != EMPTY_VALUE))
        second[i+1] = EMPTY_VALUE;
   else
      if ((first[i] != EMPTY_VALUE) && (first[i+1] != EMPTY_VALUE) && (first[i+2] == EMPTY_VALUE))
          first[i+1] = EMPTY_VALUE;
}

void PlotPoint(int i,double& first[],double& second[],double& from[])
{
   if (i>=Bars-2) return;
   if (first[i+1] == EMPTY_VALUE)
      if (first[i+2] == EMPTY_VALUE) 
            { first[i]  = from[i];  first[i+1]  = from[i+1]; second[i] = EMPTY_VALUE; }
      else  { second[i] =  from[i]; second[i+1] = from[i+1]; first[i]  = EMPTY_VALUE; }
   else     { first[i]  = from[i];                           second[i] = EMPTY_VALUE; }
}

   