Custom Normalize AD Indicator, why it gives zero divide error, yet the print check shows the value is not Zero ? - page 2

 
int startPosition = InpAveragingPeriod; //startPosition = 0;
//int limit = rates_total - prev_calculated;
//+--------------------------------------------------------------------------------------------------------+
//| Indicator Main Loop ...
//+--------------------------------------------------------------------------------------------------------+
//double vSumPrice  = 0.0;
//long   vSumVolume = 0.0;
for(int i = startPosition; i < (rates_total - 1) && !IsStopped(); i++)          // seems not optimized way as I am calculating all bars everytime !!!
{
   double vSumPrice  = 0.0;
   long   vSumVolume = 0.0;
        
   for(int j = i; j > i - InpAveragingPeriod; j--) //for(int j = 1; j <= InpAveragingPeriod; j++)
   {
      if((high[j] - low[j]) != 0)
      {
         vSumPrice  += ((close[j] - open[j]) / (high[j] - low[j]) * tick_volume[j];
         vSumVolume += tick_volume[j];
      }
   }
   if(vSumVolume == 0)
   {
      BufferNormAD[i] = EMPTY_VALUE;
   }
   else
      BufferNormAD[i] = (vSumPrice / vSumVolume);

   //vSumPrice  = 0;
   //vSumVolume = 0;
}
 
Nagisa Unada #:

VOLLA ...

finally it shows the results. Thanks a lot Nagisa for helping me out.

Just noticed one issue, Zero Bar shows no value !!!

here is the revised complete indicator code for your ready reference.

//+------------------------------------------------------------------+
//|                                                Normalized AD.mq5 |
//+------------------------------------------------------------------+
#property description "Normalized Accumulation/Distribution [AD%]"

#property indicator_separate_window
#property indicator_buffers 1
#property indicator_plots   1
#property indicator_type1   DRAW_HISTOGRAM
#property indicator_color1  LightSeaGreen
#property indicator_label1  "AD%"
//--- input params
input int                 InpAvgPeriod  = 13;
input ENUM_APPLIED_VOLUME InpVolumeType = VOLUME_TICK; // Volume type
//--- indicator buffer
double BufferNormAD[];
//+------------------------------------------------------------------+
//| Custom indicator initialization function                         |
//+------------------------------------------------------------------+
void OnInit()
  {
//--- indicator digits
   IndicatorSetInteger(INDICATOR_DIGITS,5);
//--- indicator short name
   IndicatorSetString(INDICATOR_SHORTNAME,"AKT AD% ... Where we CLOSE in range ");
//--- index buffer
   SetIndexBuffer(0,BufferNormAD,INDICATOR_DATA);
//--- set index draw begin
   PlotIndexSetInteger(0,PLOT_DRAW_BEGIN,1);
  }
//+------------------------------------------------------------------+
//| Accumulation/Distribution                                        |
//+------------------------------------------------------------------+
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[]) {
  //--- check for bars count
    if(rates_total <= InpAvgPeriod)
      return(0);        // No enough bars, exit with zero result
  //---
    int start = InpAvgPeriod;
    for(int i = start; i < (rates_total-1) && !IsStopped(); i++) {

      double sumPrice  = 0.0;
      double sumVolume = 0.0;
      
      for(int j = i; j >(i-InpAvgPeriod) && !IsStopped(); j--) {
        if((high[j] - low[j]) != 0) {
          sumPrice  += (close[j]-open[j]) / (high[j]-low[j]) * tick_volume[j];
          sumVolume += (double)tick_volume[j];
        }
      } // end of internal for..loop

      if(sumVolume == 0)    BufferNormAD[i] = EMPTY_VALUE;
      else                  BufferNormAD[i] = (sumPrice/sumVolume)/_Point; // added just to get results in higher decimals
    } // end of external for..loop
  //---
    return(rates_total);
} // END of method OnCalculate()
//+------------------------------------------------------------------+

Screen Shot: how AD% looks in histogram