//------------------------------------------------------------------
#property copyright "www.forex-tsd.com"
#property link      "www.forex-tsd.com"
//------------------------------------------------------------------
#property indicator_separate_window
#property indicator_buffers 7
#property indicator_color1  clrLimeGreen
#property indicator_color2  clrRed
#property indicator_color3  clrPaleVioletRed
#property indicator_color4  clrGold
#property indicator_color5  clrLimeGreen
#property indicator_color6  clrRed
#property indicator_color7  clrGold
#property indicator_width1  2
#property indicator_width3  2
#property indicator_width4  2
#property indicator_style7  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 double             FastPeriod             = 12;
extern double             SlowPeriod             = 26;
extern double             SignalPeriod           =  9;
extern ENUM_APPLIED_PRICE Price                  = PRICE_CLOSE;
extern bool               ShowOsma               = true;
extern int                DzLookBackBars         = 70;
extern double             DzStartBuyProbability  = 0.10;
extern double             DzStartSellProbability = 0.10;

double osmau[];
double osmad[];
double macd[];
double signal[];
double bli[];
double sli[];
double zli[];

//------------------------------------------------------------------
//                                                                  
//------------------------------------------------------------------
//
//
//
//
//

int init()
{
   int type = DRAW_NONE; if (ShowOsma) type = DRAW_HISTOGRAM;
   SetIndexBuffer(0,osmau); SetIndexStyle(0,type);
   SetIndexBuffer(1,osmad); SetIndexStyle(1,type);
   SetIndexBuffer(2,macd);
   SetIndexBuffer(3,signal);
   SetIndexBuffer(4,bli);
   SetIndexBuffer(5,sli);
   SetIndexBuffer(6,zli);
      IndicatorShortName("Dynamic zone VEMA MACD ("+DoubleToStr(FastPeriod,2)+","+DoubleToStr(SlowPeriod,2)+","+DoubleToStr(SignalPeriod,2)+")");
   return(0); 
}

//------------------------------------------------------------------
//                                                                  
//------------------------------------------------------------------
//
//
//
//
//

double vemas[][6];
#define _numf 0
#define _denf 1
#define _emaf 2
#define _nums 3
#define _dens 4
#define _emas 5

int start()
{
   int i,r,counted_bars=IndicatorCounted();
      if(counted_bars<0) return(-1);
      if(counted_bars>0) counted_bars--;
         int limit = MathMin(Bars-counted_bars,Bars-1);
         if (ArrayRange(vemas,0)!=Bars) ArrayResize(vemas,Bars);

   //
   //
   //
   //
   //
   //
   
   double alphaf = 2.0/(1.0+FastPeriod);
   double alphas = 2.0/(1.0+SlowPeriod);
   double alphag = 2.0/(1.0+SignalPeriod);
   for(i=limit, r=Bars-i-1; i>=0; i--,r++)
   {
      double price  = iMA(NULL,0,1,0,MODE_SMA,Price,i);
         if (r<2)
         {
            vemas[r][_numf] = (Volume[i]*price);
            vemas[r][_denf] = (Volume[i]);
            vemas[r][_nums] = (Volume[i]*price);
            vemas[r][_dens] = (Volume[i]);
            continue;
         }            
         vemas[r][_numf] = vemas[r-1][_numf]+alphaf*(Volume[i]*price-vemas[r-1][_numf]);
         vemas[r][_denf] = vemas[r-1][_denf]+alphaf*(Volume[i]      -vemas[r-1][_denf]);
         vemas[r][_emaf] = vemas[r][_numf]/vemas[r][_denf];
         vemas[r][_nums] = vemas[r-1][_nums]+alphas*(Volume[i]*price-vemas[r-1][_nums]);
         vemas[r][_dens] = vemas[r-1][_dens]+alphas*(Volume[i]      -vemas[r-1][_dens]);
         vemas[r][_emas] = vemas[r][_nums]/vemas[r][_dens];
         macd[i]   = vemas[r][_emaf]-vemas[r][_emas];
         signal[i] = signal[i+1]+alphag*(macd[i]-signal[i+1]);
         bli[i] = dzBuyP (macd, DzStartBuyProbability,  DzLookBackBars, Bars, i,0.0001);
         sli[i] = dzSellP(macd, DzStartSellProbability, DzLookBackBars, Bars, i,0.0001);
         zli[i] = dzSellP(macd, 0.5,                    DzLookBackBars, Bars, i,0.0001);
         osmau[i]  = EMPTY_VALUE;
         osmad[i]  = EMPTY_VALUE;
            double osma = macd[i]-signal[i];
               if (osma>0) osmau[i] = osma;
               if (osma<0) osmad[i] = osma;            
   }
return(0); 
}

