Problem with Division in MQL4

 

hi,

this is a part of my indi but it does not work. when I delete the division, it works! I will appreciate if you could help me to solve the problem.

double A1,A2,A3;
         A1=iClose("EURUSD", PERIOD_CURRENT, i); 
         A2=iClose("GBPUSD", PERIOD_CURRENT, i); 
                
         A3 =(1/A1+1/A2);

 
I would guess it's doing following:

1 divided by A1 -> result.

Result +1 ->

Result divided by A2.

You probably want

(1/A1) + (1/A2)
 
madjid62:

hi,

this is a part of my indi but it does not work. when I delete the division, it works! I will appreciate if you could help me to solve the problem.

double A1,A2,A3;
         A1=iClose("EURUSD", PERIOD_CURRENT, i); 
         A2=iClose("GBPUSD", PERIOD_CURRENT, i); 
                
         A3 =(1/A1+1/A2);

 double A1,A2,A3;
         A1=iClose("EURUSD", PERIOD_CURRENT, i); 
         A2=iClose("GBPUSD", PERIOD_CURRENT, i); 
         if(A1>0 && A2>0) A3 =(1/A1+1/A2);
 

Hi,

I have tested the code and the division works fine when A1 and A2 have valid values.
So you should check if A1 and A2 are greater 0 (as Mehmet Bastem suggested) and check
the results of the iClose function.

Best regards

 
Werner Klehr:

Hi,

I have tested the code and the division works fine when A1 and A2 have valid values.
So you should check if A1 and A2 are greater 0 (as Mehmet Bastem suggested) and check
the results of the iClose function.

Best regards

yeah it works as its own, but when I add it to the main indi, it does not. maybe it is heavy for metaeditor to calculate it. I have used NormalizeDoubleto reduce size of double but not worked.
 

Hi,

I advise you to check the values of A1 and A2 with a print statement and change the code
as Mehment suggested:

if(A1>0 && A2>0) A3 =(1/A1+1/A2); 

or

if(A1>0 && A2>0) A3 =((1/A1) + (1/A2));

The division shouldn't be a problem for the Metatrader - I do more complex calculations with MQL4 without any problems.

Best regards

 
Werner Klehr:

Hi,

I advise you to check the values of A1 and A2 with a print statement and change the code
as Mehment suggested:

or

The division shouldn't be a problem for the Metatrader - I do more complex calculations with MQL4 without any problems.

Best regards

As I mentioned in first post, I use currencies as input, so how could it be less than 0 ??!! Moreover,  Mehment already has the same problem and did not suggest a solution.
 

Hi,

i mean, that the way you get the values for A1 and A2 maybe not correct and should be checked.

Best regards

 
You can do following to avoid division by zero

(1 / (A1 + (DBL_MIN * (A1 == 0.0)))) +
(1 / (A2 + (DBL_MIN * (A2 == 0.0))))

This will always work. No matter the input.
 
         A1=iClose("EURUSD", PERIOD_CURRENT, i); 
         A2=iClose("GBPUSD", PERIOD_CURRENT, i);  
  1. On MT4: Unless the current chart is that specific symbol(s)/TF(s) referenced, you must handle 4066/4073 errors before accessing candle/indicator values. To avoid getting zero.
              Download history in MQL4 EA - Forex Calendar - MQL4 programming forum - Page 3 #26.4 2019.05.20

  2. Same index for both symbols. You must check each symbol for its bar count. To avoid getting zero.

  3. Do not assume an index on the current chart is the same as on other charts. Always use iBarShift.

 
William Roeder:
  1. On MT4: Unless the current chart is that specific symbol(s)/TF(s) referenced, you must handle 4066/4073 errors before accessing candle/indicator values. To avoid getting zero.
              Download history in MQL4 EA - Forex Calendar - MQL4 programming forum - Page 3 #26.4 2019.05.20

  2. Same index for both symbols. You must check each symbol for its bar count. To avoid getting zero.

  3. Do not assume an index on the current chart is the same as on other charts. Always use iBarShift.

Thank you William, unfortunately I am not that good in programing.
Reason: