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

#property indicator_separate_window
#property indicator_buffers 6
#property indicator_color1  Goldenrod
#property indicator_color2  LimeGreen
#property indicator_color3  LimeGreen
#property indicator_color4  Red
#property indicator_color5  Red
#property indicator_color6  DimGray
#property indicator_style3  STYLE_DOT
#property indicator_style4  STYLE_DOT
#property indicator_style6  STYLE_DOT
#property indicator_width1  2

//
//
//
//
//

#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 string TimeFrame               = "Current time frame";
extern int    Length                  = 14;
extern int    Price                   = PRICE_TYPICAL;
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   Interpolate             = true;

//
//
//
//
//

double rsx[];
double bl1Buffer[];
double bl2Buffer[];
double sl1Buffer[];
double sl2Buffer[];
double zliBuffer[];

//
//
//
//
//

string indicatorFileName;
bool   returnBars;
bool   calculateValue;
int    timeFrame;

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

int init()
{
   SetIndexBuffer(0,rsx); SetIndexLabel(0,"RSX");
   SetIndexBuffer(1,bl1Buffer);
   SetIndexBuffer(2,bl2Buffer);
   SetIndexBuffer(3,sl2Buffer);
   SetIndexBuffer(4,sl1Buffer);
   SetIndexBuffer(5,zliBuffer);
   
   //
   //
   //
   //
   //
   
   indicatorFileName = WindowExpertName();
   calculateValue    = TimeFrame=="calculateValue"; if (calculateValue) { return(0); }
   returnBars        = TimeFrame=="returnBars";     if (returnBars)     { return(0); }
   timeFrame         = stringToTimeFrame(TimeFrame);
   
   //
   //
   //
   //
   //
          
   IndicatorShortName(timeFrameToString(timeFrame)+ " Double dynamic Rsx ("+Length+")");
   
   return(0);
}

//
//
//
//
//
   
int deinit() { return(0); }

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


double wrkBuffer[][13];

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

   if(counted_bars<0) return(-1);
   if(counted_bars>0) counted_bars--;
         limit = MathMin(Bars-counted_bars,Bars-1);
         if (returnBars) { rsx[0] = MathMin(limit+1,Bars-1); return(0); }
         if (ArrayRange(wrkBuffer,0) != Bars) ArrayResize(wrkBuffer,Bars);

   //
   //
   //
   //
   //
   
   if (calculateValue || timeFrame == Period())
   {
   
   //
   //
   //
   //
   //
   
   double Kg = (3.0)/(2.0+Length); 
   double Hg = 1.0-Kg;
   for(i=limit, r=Bars-i-1; i>=0; i--, r++)
   {
      wrkBuffer[r][12] = iMA(NULL,0,1,0,MODE_SMA,Price,i);

         if (i==(Bars-1)) { for (int c=0; c<12; c++) wrkBuffer[r][c] = 0; continue; }  

      //
      //
      //
      //
      //
      
      double mom = wrkBuffer[r][12]-wrkBuffer[r-1][12];
      double moa = MathAbs(mom);
      for (int k=0; k<3; k++)
      {
         int kk = k*2;
            wrkBuffer[r][kk+0] = Kg*mom                + Hg*wrkBuffer[r-1][kk+0];
            wrkBuffer[r][kk+1] = Kg*wrkBuffer[r][kk+0] + Hg*wrkBuffer[r-1][kk+1]; mom = 1.5*wrkBuffer[r][kk+0] - 0.5 * wrkBuffer[r][kk+1];
            wrkBuffer[r][kk+6] = Kg*moa                + Hg*wrkBuffer[r-1][kk+6];
            wrkBuffer[r][kk+7] = Kg*wrkBuffer[r][kk+6] + Hg*wrkBuffer[r-1][kk+7]; moa = 1.5*wrkBuffer[r][kk+6] - 0.5 * wrkBuffer[r][kk+7];
      }
      if (moa != 0)
           rsx[i] = MathMax(MathMin((mom/moa+1.0)*50.0,100.00),0.00); 
      else rsx[i] = 50.0;
      
      if (DzStartBuyProbability1 >0) bl1Buffer[i] = dzBuyP (rsx, DzStartBuyProbability1,  DzLookBackBars, Bars, i, 0.0001);
      if (DzStartBuyProbability2 >0) bl2Buffer[i] = dzBuyP (rsx, DzStartBuyProbability2,  DzLookBackBars, Bars, i, 0.0001);
      if (DzStartSellProbability1>0) sl1Buffer[i] = dzSellP(rsx, DzStartSellProbability1, DzLookBackBars, Bars, i, 0.0001);
      if (DzStartSellProbability2>0) sl2Buffer[i] = dzSellP(rsx, DzStartSellProbability2, DzLookBackBars, Bars, i, 0.0001);
                                     zliBuffer[i] = dzSellP(rsx, 0.5                    , DzLookBackBars, Bars, i, 0.0001);
         
   }
   
   return(0);
   }
   
   //
   //
   //
   //
   //

   limit = MathMax(limit,MathMin(Bars,iCustom(NULL,timeFrame,indicatorFileName,"returnBars",0,0)*timeFrame/Period()));
         
   for (i=limit;i>=0; i--)
   {
      int y = iBarShift(NULL,timeFrame,Time[i]);
         rsx[i]       = iCustom(NULL,timeFrame,indicatorFileName,"calculateValue",Length,Price,DzLookBackBars,DzStartBuyProbability1,DzStartBuyProbability2,DzStartSellProbability1,DzStartSellProbability2,0,y);
         bl1Buffer[i] = iCustom(NULL,timeFrame,indicatorFileName,"calculateValue",Length,Price,DzLookBackBars,DzStartBuyProbability1,DzStartBuyProbability2,DzStartSellProbability1,DzStartSellProbability2,1,y);
         bl2Buffer[i] = iCustom(NULL,timeFrame,indicatorFileName,"calculateValue",Length,Price,DzLookBackBars,DzStartBuyProbability1,DzStartBuyProbability2,DzStartSellProbability1,DzStartSellProbability2,2,y);
         sl2Buffer[i] = iCustom(NULL,timeFrame,indicatorFileName,"calculateValue",Length,Price,DzLookBackBars,DzStartBuyProbability1,DzStartBuyProbability2,DzStartSellProbability1,DzStartSellProbability2,3,y);
         sl1Buffer[i] = iCustom(NULL,timeFrame,indicatorFileName,"calculateValue",Length,Price,DzLookBackBars,DzStartBuyProbability1,DzStartBuyProbability2,DzStartSellProbability1,DzStartSellProbability2,4,y);
         zliBuffer[i] = iCustom(NULL,timeFrame,indicatorFileName,"calculateValue",Length,Price,DzLookBackBars,DzStartBuyProbability1,DzStartBuyProbability2,DzStartSellProbability1,DzStartSellProbability2,5,y);
         
         //
         //
         //
         //
         // 
         
         if (!Interpolate || y==iBarShift(NULL,timeFrame,Time[i-1])) continue;

         //
         //
         //
         //
         //

         datetime time = iTime(NULL,timeFrame,y);
            for(n = 1; i+n < Bars && Time[i+n] >= time; n++) continue;	
            for(j = 1; j < n; j++)
            {
               rsx[i+j]       = rsx[i]       + (rsx[i+n]      -rsx[i])      *j/n;
               bl1Buffer[i+j] = bl1Buffer[i] + (bl1Buffer[i+n]-bl1Buffer[i])*j/n;
               bl2Buffer[i+j] = bl2Buffer[i] + (bl2Buffer[i+n]-bl2Buffer[i])*j/n;
               sl2Buffer[i+j] = sl2Buffer[i] + (sl2Buffer[i+n]-sl2Buffer[i])*j/n;
               sl1Buffer[i+j] = sl1Buffer[i] + (sl1Buffer[i+n]-sl1Buffer[i])*j/n;
               zliBuffer[i+j] = zliBuffer[i] + (zliBuffer[i+n]-zliBuffer[i])*j/n;
            }
   }

   //
   //
   //
   //
   //
   
   return(0);
}

string sTfTable[] = {"M1","M5","M15","M30","H1","H4","D1","W1","MN"};
int    iTfTable[] = {1,5,15,30,60,240,1440,10080,43200};

//
//
//
//
//

int stringToTimeFrame(string tfs)
{
   tfs = stringUpperCase(tfs);
   for (int i=ArraySize(iTfTable)-1; i>=0; i--)
         if (tfs==sTfTable[i] || tfs==""+iTfTable[i]) return(MathMax(iTfTable[i],Period()));
                                                      return(Period());
}
string timeFrameToString(int tf)
{
   for (int i=ArraySize(iTfTable)-1; i>=0; i--) 
         if (tf==iTfTable[i]) return(sTfTable[i]);
                              return("");
}

//
//
//
//
//

string stringUpperCase(string str)
{
   string   s = str;

   for (int length=StringLen(str)-1; length>=0; length--)
   {
      int char1 = StringGetChar(s, length);
         if((char1 > 96 && char1 < 123) || (char1 > 223 && char1 < 256))
                     s = StringSetChar(s, length, char1 - 32);
         else if(char1 > -33 && char1 < 0)
                     s = StringSetChar(s, length, char1 + 224);
   }
   return(s);
}

