//+------------------------------------------------------------------+
//|                                                dynamic zone MACD |
//|                                                           mladen |
//+------------------------------------------------------------------+
#property copyright "mladen"
#property link      "mladenfx@gmail.com"

#property indicator_separate_window
#property indicator_buffers 5
#property indicator_color1  DeepSkyBlue
#property indicator_color2  Gold
#property indicator_color3  LimeGreen
#property indicator_color4  Red
#property indicator_color5  DimGray
#property indicator_width1  2
#property indicator_style2  STYLE_DOT
#property indicator_style5  STYLE_DOT

//
//
//
//
//

#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    MacdFast               = 12;
extern int    MacdSlow               = 26;
extern int    MacdSignal             =  9;
extern int    MacdSignalMode         = MODE_EMA;
extern int    MacdPrice              = PRICE_CLOSE;
extern int    DzLookBackBars         = 70;
extern double DzStartBuyProbability  = 0.12;
extern double DzStartSellProbability = 0.12;

//
//
//
//
//

double macd[];
double macs[];
double bli[];
double sli[];
double cen[];

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

int init()
{
   SetIndexBuffer(0,macd); SetIndexLabel(0,"MACD");
   SetIndexBuffer(1,macs); SetIndexLabel(1,"MACD signal");
   SetIndexBuffer(2,bli);
   SetIndexBuffer(3,sli);
   SetIndexBuffer(4,cen);

   //
   //
   //
   //
   //
   
   string PriceType;
      switch(MacdPrice)
      {
         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
      }      

   //
   //
   //
   //
   //

   MacdFast = MathMax(MacdFast ,1);
   MacdSlow = MathMax(MacdSlow ,1);
   IndicatorShortName ("Dynamic zone MACD ("+MacdFast+","+MacdSlow+","+MacdSignal+","+PriceType+","+DzLookBackBars+","+DoubleToStr(DzStartBuyProbability,3)+","+DoubleToStr(DzStartSellProbability,3)+")");
   return(0);
}
int deinit()
{
   return(0);
}

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

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

   if(counted_bars<0) return(-1);
   if(counted_bars>0) counted_bars--;
         limit = MathMin(Bars-counted_bars,Bars-1);
         if (Digits == 3 || Digits == 5)
               pipMultiplier = 10;
         else  pipMultiplier = 1;                

   //
   //
   //
   //
   //
   //

      for(i=limit; i >= 0; i--) macd[i] = iMACD(NULL,0,MacdFast,MacdSlow,1,MacdPrice,MODE_MAIN,i)/Point/pipMultiplier;
      for(i=limit; i >= 0; i--)
      {
         macs[i] = iMAOnArray(macd,0,MacdSignal,0,MacdSignalMode,i);
         bli[i]  = dzBuy (macd,DzStartBuyProbability,  DzLookBackBars, Bars, i);
         sli[i]  = dzSell(macd,DzStartSellProbability, DzLookBackBars, Bars, i);
         cen[i]  = dzSell(macd,0.5                   , DzLookBackBars, Bars, i);
      }
   return(0);
}