//+------------------------------------------------------------------+
//|                                                 dynamic zone atr |
//|                                                           mladen |
//+------------------------------------------------------------------+
#property copyright "mladen"
#property link      "mladenfx@gmail.com"

#property indicator_separate_window
#property indicator_buffers 3
#property indicator_color1  DeepSkyBlue
#property indicator_color2  LimeGreen
#property indicator_color3  Red
#property indicator_width1  2

//
//
//
//
//

#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    AtrLength              = 14;
extern int    AtrSmooth              =  3;
extern int    AtrSmoothMode          = MODE_SMA;
extern int    DzLookBackBars         = 35;
extern double DzStartBuyProbability  = 0.12;
extern double DzStartSellProbability = 0.12;

//
//
//
//
//

double ats[];
double atr[];
double bli[];
double sli[];

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

int init()
{
   IndicatorBuffers(4);
   SetIndexBuffer(0,ats);
   SetIndexBuffer(1,bli);
   SetIndexBuffer(2,sli);
   SetIndexBuffer(3,atr);

   //
   //
   //
   //
   //
   
   AtrLength = MathMax(AtrLength ,1);
   IndicatorShortName ("Dynamic zone ATR ("+AtrLength+","+AtrSmooth+","+DzLookBackBars+","+DoubleToStr(DzStartBuyProbability,3)+","+DoubleToStr(DzStartSellProbability,3)+")");
   return(0);
}
int deinit()
{
   return(0);
}

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

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

   if(counted_bars<0) return(-1);
   if(counted_bars>0) counted_bars--;
         limit = MathMin(Bars-counted_bars,Bars-1);
         if (Digits == 3 || Digits == 5)
               pipMultiplier = 10;
         else  pipMultiplier = 1;                
         

   //
   //
   //
   //
   //
   //

   for(i=limit; i >= 0; i--) atr[i] = iATR(NULL,0,AtrLength,i)/Point/pipMultiplier;
   for(i=limit; i >= 0; i--)
   {
      if (AtrSmooth>1)
            ats[i] = iMAOnArray(atr,0,AtrSmooth,0,AtrSmoothMode,i);
      else  ats[i] = atr[i];
      bli[i] = dzBuy (ats, DzStartBuyProbability,  DzLookBackBars, Bars, i);
      sli[i] = dzSell(ats, DzStartSellProbability, DzLookBackBars, Bars, i);
   }
   return(0);
}