//+------------------------------------------------------------------+
//| HeikenAshiOscillator.mq4
//| Copyright http://www.pointzero-indicator.com
//+------------------------------------------------------------------+

#property copyright "Copyright © Pointzero-indicator.com"
#property link      "http://www.pointzero-indicator.com"

#property indicator_separate_window
#property indicator_buffers 5
#property indicator_color1  clrDodgerBlue
#property indicator_color2  clrRed
#property indicator_color3  clrTeal
#property indicator_color4  clrRed
#property indicator_color5  clrDodgerBlue
#property indicator_width1  2
#property indicator_width2  2
#property indicator_width3  1
#property indicator_style4  STYLE_DOT
#property indicator_style5  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            AvPeriod               = 2;              // Ma Period to use
extern ENUM_MA_METHOD MaMetod                = MODE_EMA;       // Ma Method to use
extern int            DzLookBackBars         = 25;             // Dynamic zone look back
extern double         DzStartBuyProbability  = 0.10;           // Dynamic zone buy probability
extern double         DzStartSellProbability = 0.10;           // Dynamic zone sell probability

//-------------------------------
// Buffers
//-------------------------------

double bli[];
double sli[];
double Buffer1[];
double Buffer2[];
double Buffer3[];
double Buffer4[];
double Buffer5[];
double trend[];

//+------------------------------------------------------------------+
//| Custom indicator initialization function                         |
//|------------------------------------------------------------------|
int init()
{
   IndicatorBuffers(8);
   SetIndexBuffer(0,Buffer1); SetIndexStyle(0,DRAW_HISTOGRAM);
   SetIndexBuffer(1,Buffer2); SetIndexStyle(1,DRAW_HISTOGRAM);
   SetIndexBuffer(2,Buffer3); 
   SetIndexBuffer(3,bli);
   SetIndexBuffer(4,sli);  
   SetIndexBuffer(5,Buffer4);  
   SetIndexBuffer(6,Buffer5); 
   SetIndexBuffer(7,trend);    
  
   IndicatorShortName("Heiken Ashi Oscillator");
return(0);
}

//+------------------------------------------------------------------+
//| Custom indicator deinitialization function                       |
//+------------------------------------------------------------------+
int deinit()
{
   return(0);
}

//+------------------------------------------------------------------+
//| Custom indicator iteration function                              |
//+------------------------------------------------------------------+
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);
   
   //
   //
   //
   //
   //
   
   for (i = limit; i >= 0; i--)
   {  
      double haClose = (Open[i]+High[i]+Low[i]+Close[i])/4;
      double haOpen  = (Open[i+1]+Close[i+1])/2;
      Buffer4[i]     = (haOpen + haClose)*0.5;
      Buffer5[i]     = Buffer4[i]-Buffer4[i+1];

      Buffer1[i] = EMPTY_VALUE;
      Buffer2[i] = EMPTY_VALUE;
      if (i<(Bars-1))
      {
      
        trend[i]   = trend[i+1];
        if(haOpen<haClose) trend[i] = 1;
        if(haOpen>haClose) trend[i] =-1;
        if(trend[i] == 1) Buffer1[i] = Buffer5[i];
        if(trend[i] ==-1) Buffer2[i] = Buffer5[i]; 
      }
   }
   for (i = limit; i >= 0; i--) 
   {
       Buffer3[i] = iMAOnArray(Buffer5,0,AvPeriod,0,MaMetod,i);
       bli[i]     = dzBuyP (Buffer5, DzStartBuyProbability,  DzLookBackBars, Bars, i, 0.00001);
       sli[i]     = dzSellP(Buffer5, DzStartSellProbability, DzLookBackBars, Bars, i, 0.00001);
   }
return(0);
}