Zero Divide Error

 

It must be late and I can't find it. It's in an indicator, that when loaded on a chart, it works fine. But when I call it from an EA I get Zero Divide. note: I have called this from 9 other EA's. Each call gives me the same result in the Journal for Strategy Tester.

Here's the code call in the EA:

double varPercB=iCustom(NULL,0,"bb - Percent b",bbPeriod,bbDeviation,bbPrice,0,1);
Here is the indicator code:
#property indicator_separate_window
#property indicator_buffers 1

//---- input parameters
extern int       bbPeriod=20;
extern int       bbDeviation=2;
extern int       bbPrice=0;

double PercentB[];
//+------------------------------------------------------------------+
//| Custom indicator initialization function                         |
//+------------------------------------------------------------------+
int init()
  {
   IndicatorShortName("%b - Period="+bbPeriod+"    ");   
   SetIndexStyle(0,DRAW_LINE,STYLE_SOLID,1,Blue);
   IndicatorDigits(Digits+1);
   SetIndexBuffer(0,PercentB);
   SetIndexLabel(0,"%b");
   return(0);
  }
//+------------------------------------------------------------------+
//| Custom indicator deinitialization function                       |
//+------------------------------------------------------------------+
int deinit()
  {
//----
   
//----
   return(0);
  }
//+------------------------------------------------------------------+
//| Custom indicator iteration function                              |
//+------------------------------------------------------------------+
int start()
  {
   //Bar stuff
   int    counted_bars=IndicatorCounted(); int limit; 
   if(counted_bars<0) return(-1); 
   limit=Bars-counted_bars-1;
   for(int shift=0; shift<limit; shift++)
   {
      double bbUpper=iBands(NULL,0,bbPeriod,bbDeviation,0,bbPrice,MODE_UPPER,shift);
      double bbLower=iBands(NULL,0,bbPeriod,bbDeviation,0,bbPrice,MODE_LOWER,shift);
      PercentB[shift]=((Close[shift]-bbLower)/(bbUpper-bbLower))*100.0; 
   }
   return(0);
  }
//+------------------------------------------------------------------+

I put a comment in my EA's (I've tested on multiple EA's) to see what the value was I was getting back. I get back the value the indicator shows in the chart window.

Any ideas?

Note: I get this error in the Strategy Tester.

 

Why not use Print() to ascertain the values of bbUpper and bbLower directly before you attempt to set PercentB...

And take it from there.

CB

 
nondisclosure:

It must be late and I can't find it. It's in an indicator, that when loaded on a chart, it works fine. But when I call it from an EA I get Zero Divide. note: I have called this from 9 other EA's. Each call gives me the same result in the Journal for Strategy Tester.

It looks from your code as though it has to be because the upper and lower bands are the same (i.e. bbUpper-bbLower equals zero), including the special case where both values returned by iBands() are zero.

I've not checked this but, if you have something like 87354 bars of history, then calling iBands() with a shift value such as 87352 may well give the same value for the upper and lower bands (because the standard deviation cannot be calculated over fewer than 2 bars). If I were you I'd simply check for bbUpper == bbLower, and use a different calculation for PercentB[] if true.
Reason: