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

#property indicator_separate_window
#property indicator_buffers 8
#property indicator_color1  LimeGreen
#property indicator_color2  LimeGreen
#property indicator_color3  Orange
#property indicator_color4  Orange
#property indicator_color5  DimGray
#property indicator_color6  LimeGreen
#property indicator_color7  Orange
#property indicator_color8  Orange
#property indicator_style2  STYLE_DOT
#property indicator_style3  STYLE_DOT
#property indicator_style5  STYLE_DOT
#property indicator_width6  2
#property indicator_width7  2
#property indicator_width8  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;
extern bool   arrowsVisible           = false;
extern string arrowsIdentifier        = "DssArrows";
extern color  arrowsUpColor           = clrLimeGreen;
extern color  arrowsDnColor           = clrOrange;

//
//
//
//
//

double dssBuffer[];
double dssBufferda[];
double dssBufferdb[];
double sigBuffer[];
double bl1Buffer[];
double bl2Buffer[];
double sl1Buffer[];
double sl2Buffer[];
double zliBuffer[];
double crossup[];
double crossdn[];


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

int init()
{
   IndicatorBuffers(10);
   SetIndexBuffer(0,bl1Buffer);
   SetIndexBuffer(1,bl2Buffer);
   SetIndexBuffer(2,sl2Buffer);
   SetIndexBuffer(3,sl1Buffer);
   SetIndexBuffer(4,zliBuffer);
   SetIndexBuffer(5,dssBuffer);
   SetIndexBuffer(6,dssBufferda);
   SetIndexBuffer(7,dssBufferdb);
   SetIndexBuffer(8,crossup);
   SetIndexBuffer(9,crossdn);
      StochasticLength = MathMax(1,StochasticLength);
      SmoothEMA        = MathMax(0,SmoothEMA);
   IndicatorShortName("Dynamic zone DSS Bressert ("+StochasticLength+","+SmoothEMA+")");
   return(0);
}

//
//
//
//
//

int deinit()
{
   string lookFor       = arrowsIdentifier+":";
   int    lookForLength = StringLen(lookFor);
   for (int i=ObjectsTotal()-1; i>=0; i--)
   {
      string objectName = ObjectName(i);
         if (StringSubstr(objectName,0,lookForLength) == lookFor) ObjectDelete(objectName);
   }
   return(0);
}

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

double workDss[][3];
#define _st1   0
#define _ss1   1
#define _slope 2

int start()
{
   int i,r,counted_bars = IndicatorCounted();
      if(counted_bars < 0) return(-1);
      if(counted_bars > 0) counted_bars--;
           int limit = MathMin(Bars-counted_bars,Bars-1);
           if (ArrayRange(workDss,0)!=Bars) ArrayResize(workDss,Bars);
           


   //
   //
   //
   //
   //
   
      double sort[];
      double alpha1 = 2.0 / (1.0+SmoothEMA);
      if (workDss[Bars-limit-1][_slope]==-1) CleanPoint(limit,dssBufferda,dssBufferdb);
    	for(i = limit, r=Bars-i-1; i>=0; i--,r++)
      {
         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));
            }
      
            workDss[r][_st1] = 0; if (min!=max) workDss[r][_st1] = 100*(close-min)/(max-min);
               if (i==(Bars-1))
               {
                  workDss[r][_ss1] = workDss[r][_st1];
                  dssBuffer[i]     = workDss[r][_st1];
                  sigBuffer[i]     = workDss[r][_st1];
                  continue;
               }            
            workDss[r][_ss1] = workDss[r-1][_ss1]+alpha1*(workDss[r][_st1]-workDss[r-1][_ss1]);

         //
         //
         //
         //
         //
      
            min = workDss[r][_ss1];
            max = workDss[r][_ss1];
            for (k=1; k<StochasticLength; k++)
            {
               min = MathMin(min,workDss[r-k][_ss1]);
               max = MathMax(max,workDss[r-k][_ss1]);
            }
            double stoch = 0; if (min!=max) stoch = 100*(workDss[r][_ss1]-min)/(max-min);
            
            //
            //
            //
            //
            //
            
            dssBufferda[i] = EMPTY_VALUE;
            dssBufferdb[i] = EMPTY_VALUE;
            dssBuffer[i]   = dssBuffer[i+1]+alpha1*(stoch-dssBuffer[i+1]);
            int size = 0;
               if (DzStartBuyProbability1 >0) { size++; bl1Buffer[i] = dzBuy (dssBuffer, DzStartBuyProbability1,  DzLookBackBars, Bars, i); if (ArraySize(sort)<size) ArrayResize(sort,size); sort[size-1] = bl1Buffer[i]; }
               if (DzStartBuyProbability2 >0) { size++; bl2Buffer[i] = dzBuy (dssBuffer, DzStartBuyProbability2,  DzLookBackBars, Bars, i); if (ArraySize(sort)<size) ArrayResize(sort,size); sort[size-1] = bl2Buffer[i]; }
               if (DzStartSellProbability1>0) { size++; sl1Buffer[i] = dzSell(dssBuffer, DzStartSellProbability1, DzLookBackBars, Bars, i); if (ArraySize(sort)<size) ArrayResize(sort,size); sort[size-1] = sl1Buffer[i]; }
               if (DzStartSellProbability2>0) { size++; sl2Buffer[i] = dzSell(dssBuffer, DzStartSellProbability2, DzLookBackBars, Bars, i); if (ArraySize(sort)<size) ArrayResize(sort,size); sort[size-1] = sl2Buffer[i]; }
                                                size++; zliBuffer[i] = dzSell(dssBuffer, 0.5                    , DzLookBackBars, Bars, i); if (ArraySize(sort)<size) ArrayResize(sort,size); sort[size-1] = zliBuffer[i];
                                                ArraySort(sort); 
            workDss[r][_slope] = workDss[r-1][_slope];
            crossup[i]         = crossup[i+1];
            crossdn[i]         = crossdn[i+1];
               if (dssBuffer[i]>dssBuffer[i+1]) workDss[r][_slope] =  1;
               if (dssBuffer[i]<dssBuffer[i+1]) workDss[r][_slope] = -1;
               if (workDss[r][_slope] == -1) PlotPoint(i,dssBufferda,dssBufferdb,dssBuffer);
               if (dssBuffer[i] > sort[size-1]) crossup[i] =  1;
               if (dssBuffer[i] < sort[size-1]) crossup[i] = -1;
               if (dssBuffer[i] > sort[     0]) crossdn[i] =  1;
               if (dssBuffer[i] < sort[     0]) crossdn[i] = -1;
               manageArrow(i);
      }
   return(0);           
}


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

void manageArrow(int i)
{
   if (arrowsVisible )
   {
         ObjectDelete(arrowsIdentifier+":"+Time[i]);
         if (crossup[i]!=crossup[i+1])
         {
            if (crossup[i] == 1) drawArrow(i,arrowsUpColor,241,false);
            if (crossup[i] ==-1) drawArrow(i,arrowsDnColor,242,true);
         }
         if (crossdn[i]!=crossdn[i+1])
         {
            if (crossdn[i] == 1) drawArrow(i,arrowsUpColor,241,false);
            if (crossdn[i] ==-1) drawArrow(i,arrowsDnColor,242,true);
         }
   }
}               

//
//
//
//
//

void drawArrow(int i,color theColor,int theCode,bool up)
{
   string name = arrowsIdentifier+":"+Time[i];
   double gap  = iATR(NULL,0,20,i);
   
      //
      //
      //
      //
      //
      
      ObjectCreate(name,OBJ_ARROW,0,Time[i],0);
         ObjectSet(name,OBJPROP_ARROWCODE,theCode);
         ObjectSet(name,OBJPROP_COLOR,theColor);
         if (up)
               ObjectSet(name,OBJPROP_PRICE1,High[i]+gap);
         else  ObjectSet(name,OBJPROP_PRICE1,Low[i] -gap);
}

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

void CleanPoint(int i,double& first[],double& second[])
{
   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 (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;
      }
}