//+------------------------------------------------------------------+
//|                                                 Dynamic zone CFP |
//|                                                                  |
//| original CFP by SemSemFX@rambler.ru                              |
//| http://onix-trade.net/forum/index.php?showtopic=107              |
//|                                                                  |
//| rewritten by mladen                                              |
//+------------------------------------------------------------------+
#property copyright "www.forex-tsd.com"
#property link      "www.forex-tsd.com"

#property indicator_separate_window
#property indicator_buffers 4
#property indicator_color1  DeepSkyBlue
#property indicator_color2  LimeGreen
#property indicator_color3  Red
#property indicator_color4  DimGray
#property indicator_width1  2
#property indicator_style4  STYLE_DOT

//
//
//
//
//

#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 string Prefix                 = "";
extern string Suffix                 = "";
extern int    MaMethod               = MODE_SMMA;
extern int    FastMaPeriod           = 3;
extern int    SlowMaPeriod           = 5;
extern int    Price                  = 6;
extern bool   USD                    = true;
extern bool   EUR                    = true;
extern bool   GBP                    = true;
extern bool   CHF                    = true;
extern bool   JPY                    = true;
extern bool   AUD                    = true;
extern bool   CAD                    = true;
extern bool   NZD                    = true;
extern int    DzLookBackBars         = 70;
extern double DzStartBuyProbability  = 0.16;
extern double DzStartSellProbability = 0.16;

extern bool   Interpolate = true;


//
//
//
//
//

double OUT[];
double cen[];
double bli[];
double sli[];

//
//
//
//
//

string symbols[];
string indicatorFileName;
bool   returnBars;
bool   calculateValue;
int    timeFrame;

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

int init()
{
   SetIndexBuffer(0,OUT); SetIndexLabel(0, Symbol()); 
   SetIndexBuffer(1,bli);
   SetIndexBuffer(2,sli);
   SetIndexBuffer(3,cen);
   
   //
   //
   //
   //
   //

      
      indicatorFileName = WindowExpertName();
      returnBars        = (TimeFrame=="returnBars"); if (returnBars) return(0);

            if (EUR) addSymbol("EURUSD");
            if (GBP) addSymbol("GBPUSD");
            if (AUD) addSymbol("AUDUSD");
            if (NZD) addSymbol("NZDUSD");
            if (CAD) addSymbol("USDCAD");
            if (CHF) addSymbol("USDCHF");
            if (JPY) addSymbol("USDJPY");
      
      calculateValue = (TimeFrame=="calculateValue"); if (calculateValue) return(0);
      timeFrame      = stringToTimeFrame(TimeFrame);

   //
   //
   //
   //
   //         
      
   IndicatorShortName(timeFrameToString(timeFrame)+" CFP "+Symbol());      
   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);
         if (returnBars) { OUT[0] = limit; return(0); }

   //
   //
   //
   //
   //

   if (calculateValue || timeFrame==Period())
   {
      for (int k=0; k<ArraySize(symbols); k++) limit = getLimit(limit,symbols[k]);
      
      //
      //
      //
      //
      //
      
      for(i=0; i<limit; i++)
      {
         double EURUSD_Fast=0; double EURUSD_Slow=0; if (!getMaValues(EUR,"EURUSD",EURUSD_Fast,EURUSD_Slow,i)) break;
         double GBPUSD_Fast=0; double GBPUSD_Slow=0; if (!getMaValues(GBP,"GBPUSD",GBPUSD_Fast,GBPUSD_Slow,i)) break;
         double AUDUSD_Fast=0; double AUDUSD_Slow=0; if (!getMaValues(AUD,"AUDUSD",AUDUSD_Fast,AUDUSD_Slow,i)) break;
         double NZDUSD_Fast=0; double NZDUSD_Slow=0; if (!getMaValues(NZD,"NZDUSD",NZDUSD_Fast,NZDUSD_Slow,i)) break;
         double USDCAD_Fast=0; double USDCAD_Slow=0; if (!getMaValues(CAD,"USDCAD",USDCAD_Fast,USDCAD_Slow,i)) break;
         double USDCHF_Fast=0; double USDCHF_Slow=0; if (!getMaValues(CHF,"USDCHF",USDCHF_Fast,USDCHF_Slow,i)) break;
         double USDJPY_Fast=0; double USDJPY_Slow=0; if (!getMaValues(JPY,"USDJPY",USDJPY_Fast,USDJPY_Slow,i)) break;

         //
         //
         //
         //
         //
       
         double arrUSD = 0; 
         if (USD) { if (EUR) arrUSD += EURUSD_Slow / EURUSD_Fast - 1;
                    if (GBP) arrUSD += GBPUSD_Slow / GBPUSD_Fast - 1;
                    if (AUD) arrUSD += AUDUSD_Slow / AUDUSD_Fast - 1;
                    if (NZD) arrUSD += NZDUSD_Slow / NZDUSD_Fast - 1;
                    if (CHF) arrUSD += USDCHF_Fast / USDCHF_Slow - 1;
                    if (CAD) arrUSD += USDCAD_Fast / USDCAD_Slow - 1;
                    if (JPY) arrUSD += USDJPY_Fast / USDJPY_Slow - 1; }
                 
         double arrEUR = 0;
         if (EUR) { if (USD)  arrEUR +=  EURUSD_Fast / EURUSD_Slow - 1;
                    if (GBP)  arrEUR += (EURUSD_Fast / GBPUSD_Fast) / (EURUSD_Slow / GBPUSD_Slow) - 1;
                    if (AUD)  arrEUR += (EURUSD_Fast / AUDUSD_Fast) / (EURUSD_Slow / AUDUSD_Slow) - 1;
                    if (NZD)  arrEUR += (EURUSD_Fast / NZDUSD_Fast) / (EURUSD_Slow / NZDUSD_Slow) - 1;
                    if (CHF)  arrEUR += (EURUSD_Fast * USDCHF_Fast) / (EURUSD_Slow * USDCHF_Slow) - 1;
                    if (CAD)  arrEUR += (EURUSD_Fast * USDCAD_Fast) / (EURUSD_Slow * USDCAD_Slow) - 1;
                    if (JPY)  arrEUR += (EURUSD_Fast * USDJPY_Fast) / (EURUSD_Slow * USDJPY_Slow) - 1; }
         
         double arrGBP = 0;
         if (GBP) { if (USD) arrGBP +=  GBPUSD_Fast / GBPUSD_Slow-1;
                    if (EUR) arrGBP += (EURUSD_Slow / GBPUSD_Slow) / (EURUSD_Fast / GBPUSD_Fast) - 1;
                    if (AUD) arrGBP += (GBPUSD_Fast / AUDUSD_Fast) / (GBPUSD_Slow / AUDUSD_Slow) - 1;
                    if (NZD) arrGBP += (GBPUSD_Fast / NZDUSD_Fast) / (GBPUSD_Slow / NZDUSD_Slow) - 1;
                    if (CHF) arrGBP += (GBPUSD_Fast * USDCHF_Fast) / (GBPUSD_Slow * USDCHF_Slow) - 1;
                    if (CAD) arrGBP += (GBPUSD_Fast * USDCAD_Fast) / (GBPUSD_Slow * USDCAD_Slow) - 1;
                    if (JPY) arrGBP += (GBPUSD_Fast * USDJPY_Fast) / (GBPUSD_Slow * USDJPY_Slow) - 1; }
       
         double arrAUD = 0;
         if (AUD) { if (USD) arrAUD +=  AUDUSD_Fast / AUDUSD_Slow - 1;
                    if (EUR) arrAUD += (EURUSD_Slow / AUDUSD_Slow) / (EURUSD_Fast / AUDUSD_Fast) - 1;
                    if (GBP) arrAUD += (GBPUSD_Slow / AUDUSD_Slow) / (GBPUSD_Fast / AUDUSD_Fast) - 1;
                    if (NZD) arrAUD += (AUDUSD_Fast / NZDUSD_Fast) / (AUDUSD_Slow / NZDUSD_Slow) - 1;
                    if (CHF) arrAUD += (AUDUSD_Fast * USDCHF_Fast) / (AUDUSD_Slow * USDCHF_Slow) - 1;
                    if (CAD) arrAUD += (AUDUSD_Fast * USDCAD_Fast) / (AUDUSD_Slow * USDCAD_Slow) - 1;
                    if (JPY) arrAUD += (AUDUSD_Fast * USDJPY_Fast) / (AUDUSD_Slow * USDJPY_Slow) - 1; }
       
         double arrNZD = 0;
         if (NZD) { if (USD) arrNZD +=  NZDUSD_Fast / NZDUSD_Slow - 1;
                    if (EUR) arrNZD += (EURUSD_Slow / NZDUSD_Slow) / (EURUSD_Fast / NZDUSD_Fast) - 1;
                    if (GBP) arrNZD += (GBPUSD_Slow / NZDUSD_Slow) / (GBPUSD_Fast / NZDUSD_Fast) - 1;
                    if (AUD) arrNZD += (AUDUSD_Slow / NZDUSD_Slow) / (AUDUSD_Fast / NZDUSD_Fast) - 1;
                    if (CHF) arrNZD += (NZDUSD_Fast * USDCHF_Fast) / (NZDUSD_Slow * USDCHF_Slow) - 1;
                    if (CAD) arrNZD += (NZDUSD_Fast * USDCAD_Fast) / (NZDUSD_Slow * USDCAD_Slow) - 1;
                    if (JPY) arrNZD += (NZDUSD_Fast * USDJPY_Fast) / (NZDUSD_Slow * USDJPY_Slow) - 1; }
       
         double arrCAD = 0;
         if (CAD) { if (USD) arrCAD +=  USDCAD_Slow / USDCAD_Fast - 1;
                    if (EUR) arrCAD += (EURUSD_Slow * USDCAD_Slow) / (EURUSD_Fast * USDCAD_Fast) - 1;
                    if (GBP) arrCAD += (GBPUSD_Slow * USDCAD_Slow) / (GBPUSD_Fast * USDCAD_Fast) - 1;
                    if (AUD) arrCAD += (AUDUSD_Slow * USDCAD_Slow) / (AUDUSD_Fast * USDCAD_Fast) - 1;
                    if (NZD) arrCAD += (NZDUSD_Slow * USDCAD_Slow) / (NZDUSD_Fast * USDCAD_Fast) - 1;
                    if (CHF) arrCAD += (USDCHF_Fast / USDCAD_Fast) / (USDCHF_Slow / USDCAD_Slow) - 1;
                    if (JPY) arrCAD += (USDJPY_Fast / USDCAD_Fast) / (USDJPY_Slow / USDCAD_Slow) - 1; }
       
         double arrCHF = 0;
         if (CHF) { if (USD) arrCHF +=  USDCHF_Slow / USDCHF_Fast - 1;
                    if (EUR) arrCHF += (EURUSD_Slow * USDCHF_Slow) / (EURUSD_Fast * USDCHF_Fast) - 1;
                    if (GBP) arrCHF += (GBPUSD_Slow * USDCHF_Slow) / (GBPUSD_Fast * USDCHF_Fast) - 1;
                    if (AUD) arrCHF += (AUDUSD_Slow * USDCHF_Slow) / (AUDUSD_Fast * USDCHF_Fast) - 1;
                    if (NZD) arrCHF += (NZDUSD_Slow * USDCHF_Slow) / (NZDUSD_Fast * USDCHF_Fast) - 1;
                    if (CAD) arrCHF += (USDCHF_Slow / USDCAD_Slow) / (USDCHF_Fast / USDCAD_Fast) - 1;
                    if (JPY) arrCHF += (USDJPY_Fast / USDCHF_Fast) / (USDJPY_Slow / USDCHF_Slow) - 1; }
       
         double arrJPY = 0;
         if (JPY) { if (USD) arrJPY +=  USDJPY_Slow / USDJPY_Fast - 1;
                    if (EUR) arrJPY += (EURUSD_Slow * USDJPY_Slow) / (EURUSD_Fast * USDJPY_Fast) - 1;
                    if (GBP) arrJPY += (GBPUSD_Slow * USDJPY_Slow) / (GBPUSD_Fast * USDJPY_Fast) - 1;
                    if (AUD) arrJPY += (AUDUSD_Slow * USDJPY_Slow) / (AUDUSD_Fast * USDJPY_Fast) - 1;
                    if (NZD) arrJPY += (NZDUSD_Slow * USDJPY_Slow) / (NZDUSD_Fast * USDJPY_Fast) - 1;
                    if (CAD) arrJPY += (USDJPY_Slow / USDCAD_Slow) / (USDJPY_Fast / USDCAD_Fast) - 1;
                    if (CHF) arrJPY += (USDJPY_Slow / USDCHF_Slow) / (USDJPY_Fast / USDCHF_Fast) - 1; }
       
         OUT[i] = 0;
            if(0 == StringFind(Symbol(), "USD", 0))  OUT[i] += arrUSD;
            if(3 == StringFind(Symbol(), "USD", 0))  OUT[i] -= arrUSD;
            if(0 == StringFind(Symbol(), "EUR", 0))  OUT[i] += arrEUR;
            if(0 == StringFind(Symbol(), "GBP", 0))  OUT[i] += arrGBP;
            if(3 == StringFind(Symbol(), "GBP", 0))  OUT[i] -= arrGBP;
            if(0 == StringFind(Symbol(), "CHF", 0))  OUT[i] += arrCHF;
            if(3 == StringFind(Symbol(), "CHF", 0))  OUT[i] -= arrCHF;
            if(3 == StringFind(Symbol(), "JPY", 0))  OUT[i] -= arrJPY;
            if(0 == StringFind(Symbol(), "CAD", 0))  OUT[i] += arrCAD;
            if(3 == StringFind(Symbol(), "CAD", 0))  OUT[i] -= arrCAD;
            if(0 == StringFind(Symbol(), "AUD", 0))  OUT[i] += arrAUD;
            if(3 == StringFind(Symbol(), "AUD", 0))  OUT[i] -= arrAUD;
            if(0 == StringFind(Symbol(), "NZD", 0))  OUT[i] += arrNZD;
            if(3 == StringFind(Symbol(), "NZD", 0))  OUT[i] -= arrNZD;
      }

      //
      //
      //
      //
      //
      
         if (Digits>3)
               double precision = 0.001;
         else         precision = 0.1;            
         for(; i>=0; i--)
         {
            bli[i] = dzBuyP (OUT, DzStartBuyProbability,  DzLookBackBars, Bars, i, precision);
            sli[i] = dzSellP(OUT, DzStartSellProbability, DzLookBackBars, Bars, i, precision);
            cen[i] = dzSellP(OUT, 0.5,                    DzLookBackBars, Bars, i, precision);
         }
      return(0);
   }      

   //
   //
   //
   //
   //

   for (k=0; k<ArraySize(symbols); k++) limit = getTfLimit(limit,symbols[k]);
   for (i=limit; i>=0; i--)
   {
      int y = iBarShift(NULL,timeFrame,Time[i]);
         OUT[i] = iCustom(NULL,timeFrame,indicatorFileName,"calculateValue",Prefix,Suffix,MaMethod,FastMaPeriod,SlowMaPeriod,Price,USD,EUR,GBP,CHF,JPY,AUD,CAD,NZD,DzLookBackBars,DzStartBuyProbability,DzStartSellProbability,0,y);
         bli[i] = iCustom(NULL,timeFrame,indicatorFileName,"calculateValue",Prefix,Suffix,MaMethod,FastMaPeriod,SlowMaPeriod,Price,USD,EUR,GBP,CHF,JPY,AUD,CAD,NZD,DzLookBackBars,DzStartBuyProbability,DzStartSellProbability,1,y);
         sli[i] = iCustom(NULL,timeFrame,indicatorFileName,"calculateValue",Prefix,Suffix,MaMethod,FastMaPeriod,SlowMaPeriod,Price,USD,EUR,GBP,CHF,JPY,AUD,CAD,NZD,DzLookBackBars,DzStartBuyProbability,DzStartSellProbability,2,y);
         cen[i] = iCustom(NULL,timeFrame,indicatorFileName,"calculateValue",Prefix,Suffix,MaMethod,FastMaPeriod,SlowMaPeriod,Price,USD,EUR,GBP,CHF,JPY,AUD,CAD,NZD,DzLookBackBars,DzStartBuyProbability,DzStartSellProbability,3,y);

         //
         //
         //
         //
         //
      
            if (y==iBarShift(NULL,timeFrame,Time[i-1]) || !Interpolate) continue;

         //
         //
         //
         //
         //

         datetime time = iTime(NULL,timeFrame,y);
            for(int n = 1; i+n < Bars && Time[i+n] >= time; n++) continue;	
            for(int j = 1; j < n; j++)
            {
               OUT[i+j] = OUT[i] + (OUT[i+n]-OUT[i])*j/n;
               bli[i+j] = bli[i] + (bli[i+n]-bli[i])*j/n;
               sli[i+j] = sli[i] + (sli[i+n]-sli[i])*j/n;
               cen[i+j] = cen[i] + (cen[i+n]-cen[i])*j/n;
            }               
   }

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

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

void addSymbol(string symbol)
{
   ArrayResize(symbols,ArraySize(symbols)+1); symbols[ArraySize(symbols)-1] = Prefix+symbol+Suffix;
}

//
//
//
//
//

int getLimit(int limit, string symbol)
{
   if (symbol!=Symbol())
          limit = MathMin(Bars-1,MathMax(iCustom(symbol,0,indicatorFileName,"returnBars",0,0),limit));
   return(limit);
}
int getTfLimit(int limit, string symbol)
{
   return(MathMin(Bars-1,MathMax((iCustom(symbol,timeFrame,indicatorFileName,"returnBars",0,0)+1)*timeFrame/Period(),limit)));
}



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

bool getMaValues(bool doIt, string symbol, double& fast, double& slow,int i)
{
   if (doIt)
   {
      fast = ma(Prefix+symbol+Suffix,FastMaPeriod,MaMethod,Price,i);
      slow = ma(Prefix+symbol+Suffix,SlowMaPeriod,MaMethod,Price,i);
            return(fast!=0 && slow!=0);
   }
   return(true);
}

//
//
//
//
//
  
double ma(string sym, int per, int Mode, int Price, int i)
  {
   double res = 0; int k = 1;
   switch(Period())
   {
       case 1:     res += iMA(sym, 0, per*k, 0, Mode, Price,i); k += 5;
       case 5:     res += iMA(sym, 0, per*k, 0, Mode, Price,i); k += 3;
       case 15:    res += iMA(sym, 0, per*k, 0, Mode, Price,i); k += 2;
       case 30:    res += iMA(sym, 0, per*k, 0, Mode, Price,i); k += 2;
       case 60:    res += iMA(sym, 0, per*k, 0, Mode, Price,i); k += 4;
       case 240:   res += iMA(sym, 0, per*k, 0, Mode, Price,i); k += 6;
       case 1440:  res += iMA(sym, 0, per*k, 0, Mode, Price,i); k += 4;
       case 10080: res += iMA(sym, 0, per*k, 0, Mode, Price,i); k += 4;
       case 43200: res += iMA(sym, 0, per*k, 0, Mode, Price,i);
   }
   return(res);
}


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

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);
}