stevehopwoodforex.com
https://www.stevehopwoodforex.com/phpBB3/
Print view

Help with zero divide error
https://www.stevehopwoodforex.com/phpBB3/viewtopic.php?t=3914
Page 1 of 1
Author:  nhtqngn14 [ Tue Nov 04, 2014 9:16 pm ]
Post subject:  Help with zero divide error

Hi, can some one help me with this peice of code which came from the expert advisor Cointegration Search Engine. it worked before but since the new Empty4 release. i get zero divide error from this line

Code: Select all

// convert to units
coef[i] = coef[i] * 1/ConvertCurrency(1,symb[i],"USD",iOpen(symb[i],0,0),s);

here is the function 
// convert the coefficients to usable hege ratios by multiplying
// xxx/usd pairs with their quote. The results can then be
// conveniently interpreted as multiples of needed Lots or Units.
// also take care of the special case when fitting a spread
// instead a trend
string s;
for (i=0; i<pairs; i++){
// if we fit a spread then all pairs except this one are on the other
// side (negative) and this one (the regressand) is 1 by definition
if (!trend){
if (i == this){
coef[i] = 1;
}else{
coef[i] = -coef[i];
}
}
// convert to units
coef[i] = coef[i] * 1/ConvertCurrency(1,symb[i],"USD",iOpen(symb[i],0,0),s);
// The following makes sure that if the first pair is an USD/XXX pair
// it is normalized to 1 again and the lot sizes of the other ones
// instead made smaller by the same factor.
if (!trend){
coef[i] = coef[i] /ConvertCurrency(1,symb[i],"USD",iOpen(symb[i],0,0),s);
}
}
Thank you very much
Author:  renexxxx [ Wed Nov 05, 2014 6:45 am ]
Post subject:  Help with zero divide error

Is this a joke? How to you expect anyone to try and make sense of this code. It is a fragment and does not contain the function definition of the ConvertCurrency() function. Obviously, since the only division ever done in this code, is to divide by the value returned by the ConvertCurrency() function, it follows that the ConvertCurrency() function returns 0.0. It hurts my eyes even looking at this code, and it probably won't compile on a 600+ compiler, as eg. an integer is compared to an object reference ('this').

I don't understand how people can write code that can't be read: this is what programmers call 'write-only code'. As a courtesy to you, I have properly indented the code: not that it will make any difference.

If you want my help, provide a complete sample of the code.

Code: Select all

   // convert to units
   coef[i] = coef[i] * 1/ConvertCurrency(1,symb[i],"USD",iOpen(symb[i],0,0),s);
 
   here is the function
   // convert the coefficients to usable hege ratios by multiplying
   // xxx/usd pairs with their quote. The results can then be
   // conveniently interpreted as multiples of needed Lots or Units.
   // also take care of the special case when fitting a spread
   // instead a trend
   string s;

   for (i=0; i<pairs; i++) {

      // if we fit a spread then all pairs except this one are on the other
      // side (negative) and this one (the regressand) is 1 by definition
      if ( !trend ) {
         if (i == this) {
            coef[i] = 1;
         }
         else {
            coef[i] = -coef[i];
         }
      }
   
      // convert to units
      coef[i] = coef[i] * 1/ConvertCurrency(1,symb[i],"USD",iOpen(symb[i],0,0),s);
      // The following makes sure that if the first pair is an USD/XXX pair
      // it is normalized to 1 again and the lot sizes of the other ones
      // instead made smaller by the same factor.
   
      if ( !trend ) {
         coef[i] = coef[i] /ConvertCurrency(1,symb[i],"USD",iOpen(symb[i],0,0),s);
      }
   }
Author:  nhtqngn14 [ Wed Nov 05, 2014 1:10 pm ]
Post subject:  Help with zero divide error

No this is not a joke. sorry about the function def. I totally forgot to add the source code of EA. This EA as i said was coded by 1 of the forum member. i was trying to run the EA for researching for my thesis paper. i really appreciate if u can sort the problem out.
Author:  renexxxx [ Wed Nov 05, 2014 7:48 pm ]
Post subject:  Help with zero divide error

The provided Cointegration Search Engine.mq4 does not compile. There are too many errors to fix -- notably one where a single-dimension double array (regressors) is passed to a R-function that expects a two-dimension double array.

If you are doing a thesis on a piece of MQL4 code, it might be somewhat helpful to learn to program and debug MQL4 programs. Good luck!
Author:  fxdaytrader [ Mon Dec 15, 2014 4:41 am ]
Post subject:  Help with zero divide error

nhtqngn14 ยป Wed Nov 05, 2014 1:10 pm wrote:No this is not a joke. sorry about the function def. I totally forgot to add the source code of EA. This EA as i said was coded by 1 of the forum member. i was trying to run the EA for researching for my thesis paper. i really appreciate if u can sort the problem out.
I have attached the compiled .ex4 (compiled by using the old build 509 editor, use forumsearch or see
http://www.forexfactory.com/showthread.php?t=470340 for info/download) -> The used R-library was downloaded from http://www.forexfactory.com/showthread.php?t=260422
Author:  snailbeard [ Wed Dec 17, 2014 6:48 am ]
Post subject:  Help with zero divide error

Divide by zero can be a headache to find in MetaQuotes MQL, but there are some things you can do minimize the pain.

If there are only a few places in which division by a variable takes place then you could use a regular expression to help find those lines:

example:
(/){1,1}[^/][^0-9]
looks for a '/' then anything but a '/' then anything but a number

once you have located these lines you can change the code to be safer:

(a) check the divisor first then print a message and/or line number __LINE__ (using the newer compiler)

Or to save on retyping similar code at each divisor...
(b) pass parameters to a custom function which handles division, checks the divisor and prints a message if the divisor is zero

double doDivision( double dTop, double dBottom, __LINE__ )
{
...
}

If don't want to change the code then you can always use divide and conquer by commenting out large sections of code until you narrow down the section containing the division by zero.

Defensive programming is a style of programming which assumes what can go wrong will go wrong and involves checking all function parameters. It adds an overhead to producing the code but reduces the time spent searching for errors later on.

The newer compiler is much better at spotting certain kinds of errors which the old compiler didn't care about so if this code has value it would worth bringing it up to date.
All times are UTC Page 1 of 1