//+------------------------------------------------------------------+
//|                                       Dynamic zone dema macd.mq4 |
//+------------------------------------------------------------------+
#property copyright "www.forex-tsd.com"
#property link      "www.forex-tsd.com"

#property indicator_separate_window
#property indicator_buffers 5
#property indicator_color1  DeepSkyBlue
#property indicator_color2  DimGray
#property indicator_color3  PaleVioletRed
#property indicator_color4  DeepSkyBlue
#property indicator_color5  PaleVioletRed
#property indicator_width4  2
#property indicator_style2  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 int    MacdFast               = 12;
extern int    MacdSlow               = 26;
extern int    SignalPeriod           =  1;
extern int    SignalMode             = MODE_SMA;
extern int    MacdPrice              = PRICE_CLOSE;				
extern int    DzLookBackBars         = 70;
extern double DzStartBuyProbability  = 0.1;
extern double DzStartSellProbability = 0.1;

//
//
//
//
//

double Buffer1[];
double Buffer2[];
double bli[];
double sli[];
double zli[];


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

int init()
{
   SetIndexBuffer(0,bli);
   SetIndexBuffer(1,zli);
   SetIndexBuffer(2,sli);
	SetIndexBuffer(3,Buffer1);
	SetIndexBuffer(4,Buffer2);
	IndicatorShortName("MACD of DEMA("+MacdFast+","+MacdSlow+","+SignalPeriod+")");
}

//
//
//
//
//

double workEmas[][4];

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

   if(counted_bars<0) return(-1);
   if(counted_bars>0) counted_bars--;
         limit = MathMin(Bars-counted_bars,Bars-1);
         if (ArrayRange(workEmas,0) != Bars) ArrayResize(workEmas,Bars);

   //
   //
   //
   //
   //
   
   double alphaf = 2.0 / (1.0 + MacdFast);
   double alphas = 2.0 / (1.0 + MacdSlow);
   
	for(i = limit, r=Bars-i-1; i>=0; i--,r++)
	{
	  double price = iMA(NULL,0,1,0,MODE_SMA,MacdPrice,i);
   	  workEmas[r][0] = workEmas[r-1][0]+alphaf*(price         -workEmas[r-1][0]);
   	  workEmas[r][1] = workEmas[r-1][1]+alphaf*(workEmas[r][0]-workEmas[r-1][1]);
   	  workEmas[r][2] = workEmas[r-1][2]+alphas*(price         -workEmas[r-1][2]);
   	  workEmas[r][3] = workEmas[r-1][3]+alphas*(workEmas[r][2]-workEmas[r-1][3]);
   	  
		  Buffer1[i] = 2.0*workEmas[r][0]-workEmas[r][1]-(2.0*workEmas[r][2]-workEmas[r][3]); 
		  
		  //
		  //
		  //
		  //
		  //
		  
        bli[i] = dzBuyP (Buffer1,DzStartBuyProbability,  DzLookBackBars, Bars, i, 0.0001);
        sli[i] = dzSellP(Buffer1,DzStartSellProbability, DzLookBackBars, Bars, i, 0.0001);
        zli[i] = dzSellP(Buffer1,0.5                   , DzLookBackBars, Bars, i, 0.0001);
	}
	if (SignalPeriod>1) for(i = limit; i >=0; i--) Buffer2[i] = iMAOnArray(Buffer1,Bars,SignalPeriod,0,SignalMode,i);
	return(0);
}

