//+------------------------------------------------------------------+
//|                                       dynamic zone synthetic rsi |
//|                                                           mladen |
//+------------------------------------------------------------------+
#property copyright "mladen"
#property link      "mladenfx@gmail.com"

#property indicator_separate_window
#property indicator_buffers 4
#property indicator_color1  DeepSkyBlue
#property indicator_color2  LimeGreen
#property indicator_color3  Red
#property indicator_color4  Gold
#property indicator_width1  2
#property indicator_style4  STYLE_DOT
#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    RsiPrice               = PRICE_CLOSE;
extern int    EmaLength1             = 48;
extern int    RsiLength1             = 32;
extern int    EmaLength2             = 24;
extern int    RsiLength2             = 16;
extern int    EmaLength3             = 12;
extern int    RsiLength3             =  8;
extern int    DzLookBackBars         = 70;
extern double DzStartBuyProbability  = 0.10;
extern double DzStartSellProbability = 0.10;

//
//
//
//
//

double rsi[];
double bli[];
double sli[];
double zli[];
double em1[];
double em2[];
double em3[];

//+------------------------------------------------------------------+
//|                                                                  |
//+------------------------------------------------------------------+
//
//
//
//
//

int init()
{
   IndicatorBuffers(7);
   SetIndexBuffer(0,rsi);
   SetIndexBuffer(1,bli);
   SetIndexBuffer(2,sli);
   SetIndexBuffer(3,zli);
   SetIndexBuffer(4,em1);
   SetIndexBuffer(5,em2);
   SetIndexBuffer(6,em3);
   //
   //
   //
   //
   //
   
   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
      }      

   //
   //
   //
   //
   //

   IndicatorShortName ("Dynamic zone RSI ("+PriceType+","+EmaLength1+","+RsiLength1+","+EmaLength2+","+RsiLength2+","+EmaLength3+","+RsiLength3+","+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--)
   {
      em1[i] = iMA(NULL,0,EmaLength1,0,MODE_EMA,RsiPrice,i);
      em2[i] = iMA(NULL,0,EmaLength2,0,MODE_EMA,RsiPrice,i);
      em3[i] = iMA(NULL,0,EmaLength3,0,MODE_EMA,RsiPrice,i);
   }
   for(i=limit; i >= 0; i--)
   {
      double rsi1 = iRSIOnArray(em1,0,RsiLength1,i);
      double rsi2 = iRSIOnArray(em2,0,RsiLength2,i);
      double rsi3 = iRSIOnArray(em3,0,RsiLength3,i);
   
      //
      //
      //
      //
      //
         
      rsi[i] = (rsi3+2.0*rsi2+3.0*rsi1)/6.0;
      bli[i] = dzBuy (rsi, DzStartBuyProbability,  DzLookBackBars, Bars, i);
      sli[i] = dzSell(rsi, DzStartSellProbability, DzLookBackBars, Bars, i);
      zli[i] = dzSell(rsi, 0.5,                    DzLookBackBars, Bars, i);
   }
   return(0);
}



