//+------------------------------------------------------------------------+
//|                                                    dynamic zone rsi3m3 |
//| as described by Walter Bressert in his article                         |
//| Trading With Time Fractals to Reduce Risk and Improve Profit Potential |
//+------------------------------------------------------------------------+
#property copyright "mladen"
#property link      "mladenfx@gmail.com"

#property indicator_separate_window
#property indicator_buffers 3
#property indicator_color1  clrDeepSkyBlue
#property indicator_color2  clrLimeGreen
#property indicator_color3  clrRed
#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    RsiLength              = 3;
extern int    RsiPrice               = PRICE_CLOSE;
extern int    SmoothPeriod           = 3;
extern int    DzLookBackBars         = 70;
extern double DzStartBuyProbability  = 0.10;
extern double DzStartSellProbability = 0.10;
extern int    RsiLineWidth           = 10;
extern int    DzLineWidth            = 12;

//
//
//
//
//

double rsi[];
double rsiSmoothed[];
double bli[];
double sli[];

//+------------------------------------------------------------------+
//|                                                                  |
//+------------------------------------------------------------------+
//
//
//
//
//

int init()
{
   for (int i=0; i<indicator_buffers; i++) SetIndexStyle(i,DRAW_LINE);
   IndicatorBuffers(4);
   SetIndexBuffer(0,rsiSmoothed);  SetIndexStyle(0,EMPTY,EMPTY,RsiLineWidth);
   SetIndexBuffer(1,bli);          SetIndexStyle(1,EMPTY,EMPTY,DzLineWidth);
   SetIndexBuffer(2,sli);          SetIndexStyle(2,EMPTY,EMPTY,DzLineWidth);
   SetIndexBuffer(3,rsi);

   //
   //
   //
   //
   //
   
   string PriceType;
      switch(RsiPrice)
      {
         case PRICE_CLOSE:    PriceType = "Close";    break;  // 0
         case PRICE_OPEN:     PriceType = "Open";     break;  // 1
         case PRICE_HIGH:     PriceType = "High";     break;  // 2
         case PRICE_LOW:      PriceType = "Low";      break;  // 3
         case PRICE_MEDIAN:   PriceType = "Median";   break;  // 4
         case PRICE_TYPICAL:  PriceType = "Typical";  break;  // 5
         case PRICE_WEIGHTED: PriceType = "Weighted"; break;  // 6
      }      

   //
   //
   //
   //
   //

   RsiLength = MathMax(RsiLength ,1);
   IndicatorShortName ("Dynamic zone RSI3M3 ("+RsiLength+","+PriceType+","+DzLookBackBars+","+DoubleToStr(DzStartBuyProbability,3)+","+DoubleToStr(DzStartSellProbability,3)+")");
   return(0);
}
int deinit()
{
   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);

   //
   //
   //
   //
   //
   //

   for(i=limit; i >= 0; i--)
   {
      rsi[i] = iRSI(NULL,0,RsiLength,RsiPrice,i);    
      double rsiSmooth = rsi[i];
         for (int k=1; k<SmoothPeriod; k++) rsiSmooth += rsi[i+k];
                                            rsiSmooth /= SmoothPeriod;
      rsiSmoothed[i] = rsiSmooth;
      bli[i]         = dzBuy (rsiSmoothed,DzStartBuyProbability,  DzLookBackBars, Bars, i);
      sli[i]         = dzSell(rsiSmoothed,DzStartSellProbability, DzLookBackBars, Bars, i);
   }
   return(0);
}