//+------------------------------------------------------------------+
//|   Unkown                                                         |
//|                                                                  |
//|                                                                  |
//|                                                                  |
//+------------------------------------------------------------------+

//Modified, 04/05/2022, by jeanlouie, www.forexfactory.com/jeanlouie
// - array out of range bug for avgvolume and volume[] in oncalc

//+------------------------------------------------------------------+
//|                                                                  |
//+------------------------------------------------------------------+

#property     strict
#property     indicator_separate_window
#property     indicator_buffers                                    3
#property     indicator_level1                                     0
#property     indicator_color1                                     clrDodgerBlue
#property     indicator_color2                                     clrRed
#property     indicator_color3                                     clrAqua
#property     indicator_levelcolor                                 clrDimGray

input         int                 CountBars                     =  1000;                //  Count Bars:
input         int                 EMZPeriod                     =  40;                  //  EMZ Period:
input         double              MinValue                      = -1000;                //  Min Value:
input         double              MaxValue                      =  1000;                //  Max Value:
input         int                 FastMAPeriod                  =  24;                  //  Fast Period:
input         ENUM_MA_METHOD      FastMAMethod                  =  MODE_SMA;            //  Method MA:
input         int                 SlowMAPeriod                  =  55;                  //  Slow Period:
input         ENUM_MA_METHOD      SlowMAMethod                  =  MODE_SMA;            //  Method MA:

double        FastBuff[];
double        SlowBuff[];
double        EMZBuffer[];

//———————————————————————|

int OnInit()
{
   IndicatorBuffers(3);
   IndicatorShortName("");

   SetIndexBuffer(0,EMZBuffer);
   SetIndexBuffer(1,FastBuff);
   SetIndexBuffer(2,SlowBuff);

   SetIndexStyle (0,DRAW_LINE);
   SetIndexStyle (1,DRAW_LINE);
   SetIndexStyle (2,DRAW_LINE);

   SetIndexLabel(0,"EMZ");
   SetIndexLabel(1,"FastMA");
   SetIndexLabel(2,"SlowMA");

   return(INIT_SUCCEEDED);
}

//—————————————————————————————————————————————|

int OnCalculate(const int       rates_total,
                const int       prev_calculated,
                const datetime &time[],
                const double   &open[],
                const double   &high[],
                const double   &low[],
                const double   &close[],
                const long     &tick_volume[],
                const long     &volume[],
                const int      &spread[])
{
//—————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————|

   int     limit,i,k,j;
   double _H,_L,_V,_HP,_LP,EMZ,sumaEMZ;

   if(prev_calculated == 0){for(i = rates_total-fmax(fmax(FastMAPeriod,SlowMAPeriod),EMZPeriod); i < rates_total; i++) EMZBuffer[i]= 0;}

   limit=CountBars>=rates_total?rates_total-fmax(fmax(FastMAPeriod,SlowMAPeriod),EMZPeriod):CountBars;

   for(i = 0; i < MathMin(limit,rates_total-fmax(fmax(FastMAPeriod,SlowMAPeriod),EMZPeriod)); i++)
   {
      sumaEMZ = 0;
      int count = 0;
      double avgvolume = 0;

      for(k = 0; k < MathMin(rates_total-fmax(fmax(FastMAPeriod,SlowMAPeriod),EMZPeriod),i+EMZPeriod); k++)
      {
         avgvolume +=(double)Volume[MathMin(rates_total-1,i+k)];
         count++;
      }  avgvolume /= count;

      for(k = 0; k < EMZPeriod; k++)
      {
         _H = high[i+k];
         _L = low[i+k];
         _HP = high[i+1+k];
         _LP = low[i+1+k];
         _V =(double)Volume[i+k];

         if(_V < 1 && open[i+k]!= close[i+k])
         {
            for(j=1; j<limit; j++){if(Volume[i + k + j]>= 1){_V =(double)Volume[i+k+j]/(j+1); break;}}
         }

         _V = MathMax(_V,avgvolume/4);
         _V =_V/avgvolume;

         EMZ = 0.0;

         if(_H >_L)
         if(_V /(_H -_L)> 0) EMZ =(((_H+_L)/2)-((_HP+_LP)/2))/(_V/(_H-_L))*10000000000;

         if(MinValue <-1) EMZ = fmax(MinValue,EMZ);
         if(MaxValue > 1) EMZ = fmin(MaxValue,EMZ);

         sumaEMZ = sumaEMZ+EMZ;
      }
      EMZBuffer[i]=(sumaEMZ/EMZPeriod);
   }

   for(i=limit; i>=0; i--) FastBuff[i]= iMAOnArray(EMZBuffer,0,FastMAPeriod,0,FastMAMethod,i);
   for(i=limit; i>=0; i--) SlowBuff[i]= iMAOnArray(EMZBuffer,0,SlowMAPeriod,0,SlowMAMethod,i);

   return(rates_total);
}

//—————————————————————————————————| end of the algorithm |——————————————————————————————————|