Help with this function

 
//+------------------------------------------------------------------+
//|                                               malfunc_tester.mq4 |
//|                            Copyright © 2013,                     |
//|                                                                  |
//+------------------------------------------------------------------+
#property copyright "Copyright © 2013,******
#property link      "**********"

#property indicator_chart_window

double EURUSDprev;

//---------------------------------
// init function
//------------------------------------
int init()
{
    ObjectCreate("EURUSDc", OBJ_LABEL, 0, 0, 0);  // Creating obj.
   ObjectSet("EURUSDc", OBJPROP_CORNER, 0);    // Reference corner
   ObjectSet("EURUSDc", OBJPROP_XDISTANCE, 70);// X coordinate   
   ObjectSet("EURUSDc", OBJPROP_YDISTANCE, 15);// Y coordinate

return(0);
}

//---------------------------------
// start function
//------------------------------------
int start()
{
 
double EURUSDv=PercCalc("EURUSD",EURUSDprev);
string mytext=StringConcatenate(EURUSDv," %");
ObjectSetText("EURUSDc",mytext,8,"Arial",Red);

EURUSDprev=MarketInfo("EURUSD",MODE_BID);

return(0);
}


//---------------------------------
// de initialize
//------------------------------------
int deinit()
{
//----
ObjectsDeleteAll(EMPTY,OBJ_LABEL);
//----
   return(0);
}

// Custom function PercCalc
double PercCalc(string aSymbol,double pValue)
 {
  double calc1=MarketInfo(aSymbol,MODE_BID)-pValue;
  double calc2=calc1/pValue;
  double calc3=NormalizeDouble(calc2*100,Digits);
  return(calc3);
 }

I need help here. The label/object is not showing. After tests i discovered that its the division in the PercCalc custom function because if i comment out calc2 and calc3 then set calc1 as output like below it displays something please help;

...

double PercCalc(string aSymbol,double pValue)
 {
  double calc1=MarketInfo(aSymbol,MODE_BID)-pValue;
  // double calc2=calc1/pValue;
  // double calc3=NormalizeDouble(calc2*100,Digits);
  return(calc1);
 }

/* This modification works but i want it to work in its full mode and not just show the word label */
Why is the division in PercCalc function causing a mulfunction?
 
THIS EATING MY HEAD BIG TIME !!!
 
tonny:
THIS EATING MY HEAD BIG TIME !!!

Look at the log or experts tab . . . .

2013.11.04 09:55:05 malfunc_tester EURUSD,H1: zero divide

2013.11.04 09:55:05 malfunc_tester EURUSD,H1: zero divide

2013.11.04 09:55:04 malfunc_tester EURUSD,H1: zero divide

2013.11.04 09:55:04 malfunc_tester EURUSD,H1: zero divide

2013.11.04 09:55:03 malfunc_tester EURUSD,H1: zero divide

2013.11.04 09:55:03 malfunc_tester EURUSD,H1: zero divide

2013.11.04 09:55:01 malfunc_tester EURUSD,H1: zero divide

2013.11.04 09:55:01 malfunc_tester EURUSD,H1: zero divide

 
RaptorUK:

Look at the log or experts tab . . . .

Make this simple change . . .

// Custom function PercCalc
double PercCalc(string aSymbol,double pValue)
 {
   if(pValue == 0.0)  // avoid divide by zero error
      return(0.0); 
      
  double calc1=MarketInfo(aSymbol,MODE_BID)-pValue;
  double calc2=calc1/pValue;
  double calc3=NormalizeDouble(calc2*100,Digits);
  return(calc3);
 }
 
RaptorUK:

Look at the log or experts tab . . . .


Yes you are right raptor i tried replacing pValue with 2 in the divide and it displayed some red numbers. But why is this happening causing pValue to be 0?
 
tonny:

Yes you are right raptor i tried replacing pValue with 2 in the divide and it displayed some red numbers. But why is this happening causing pValue to be 0?
What is it's value when it is declared ? when the indicator runs for the very first tick it sees ? then the divide by zero = crash and pValue is always 0.0 . . . and there is always a divide by zero, etc, etc.
 
RaptorUK:

Make this simple change . . .


Yer that now works. Thanks.
 

Add this to

EURUSDprev=MarketInfo("EURUSD",MODE_BID);

init()

 
GumRai:

Add this to

init()


It now works. EURUSDprev's work is to store the last Bid for use in the next tick so it has to be after the calculations so that only the previous Bid is used. But i think your method could also work only that if you are dealing with over 50 pairs it becomes lots of extra work unlike raptor style. I knew at start EURUSDprev wont have a value but i didnt know it would now stick there. I thought at the second tick it will now get a value and continue normally.
 
GumRai:

Add this to

init()

You should still check for the possibility of the divide by zero . . .
 

I know this topic is closed ... but just wanted to post this tip for the future readers.

If you are getting "zero divide" error in expert log then do the following:

1. Open mq4 file in editor

2. Search for "/"

3. Confirm the denominator is not zero (for manually entered using extern etc) .... or if the denominator is a calculated value then make sure that this is normalized

4. after normalizing the denominator, 99% chance is there that zero divide error would have gone.

4. You can normalize any value with following function

NormalizeDouble(variable, digit);
Reason: