Download MetaTrader 5

intermittent divide by zero

To add comments, please log in or register
Find out how to install MetaTrader on Mac OS
BigAl
489
BigAl 2012.12.05 10:26 


  MqlTick myprice;

  MqlTradeRequest myrequest;

  MqlTradeResult myresult;

  ZeroMemory(myrequest);

  myrequest.action = TRADE_ACTION_DEAL;

  myrequest.sl = 0;

  myrequest.tp = 0;

  myrequest.symbol = _Symbol;

  myrequest.magic = 0;

  myrequest.type_filling = ORDER_FILLING_FOK;

  myrequest.deviation = 10;

 

/* body of my code here

segment causing problem is here.  The lot size is around 5 lots (4.98 - 5.02) does not seem to matter

*/

 

  PositionSelect(_Symbol);

  position = myprice.ask * PositionGetDouble(POSITION_VOLUME) * 100000;

  position = MathRound(position * 100)/100;

  NormalizeDouble(position, 2);

  if (target > position)

   {

    if (target - position < 600) return;

    Print("target ", target, " - position  ", position, " = ", target - position, " > 600");

Documentation on MQL5: Standard Constants, Enumerations and Structures / Trade Constants / Trade Operation Types
Documentation on MQL5: Standard Constants, Enumerations and Structures / Trade Constants / Trade Operation Types
  • www.mql5.com
Standard Constants, Enumerations and Structures / Trade Constants / Trade Operation Types - Documentation on MQL5
BigAl
489
BigAl 2012.12.05 10:38  

oops!! sorry pushed the add button too early.

At the point where the print statement is I get in the log

 IS    0    StaticEA (EURUSD,M15)    16:59:01    target 652630.0 - position = 0.0 = 652630.0 > 600

So the value of position is showing zero and the if statement will be true causing my divide by zero error in the nest line of code

CN    2    StaticEA (EURUSD,M15)    16:59:01    zero divide in 'StaticEA.mq5' (107,113)



phi nuts
2184
phi nuts 2012.12.05 21:49  

Hi BigAl,

1. Next time when posting code, please use SRC button till succeed.

 

2. You ask for advice by only showing partial of the code, you may get the answer but may not what you want

 if (target > position && position > 0)
    {
     ...

    }

 

Rogerio Figurelli
Moderator
39655
Rogerio Figurelli 2012.12.06 01:41  

Note that this debug info indicates error in line 107 column 113 of StaticEA.mq5, so please copy and paste this one (identify it to be analysed here) or just double check it if there is some variable that assumes division by zero value.

zero divide in 'StaticEA.mq5' (107,113)
Documentation on MQL5: MQL5 programs / Runtime Errors
Documentation on MQL5: MQL5 programs / Runtime Errors
  • www.mql5.com
MQL5 programs / Runtime Errors - Documentation on MQL5
BigAl
489
BigAl 2012.12.06 02:21  
phi.nuts:

Hi BigAl,

1. Next time when posting code, please use SRC button till succeed.

 

2. You ask for advice by only showing partial of the code, you may get the answer but may not what you want

 

Ok thanks for the advice - just thought it may be easier to just provide the relevant code snippet.

Your suggestion will overcome the problem, but why does it sometimes give position a value of zero - is my code incorrect (or not correct enough?) please

BigAl
489
BigAl 2012.12.06 02:29  
figurelli:

Note that this debug info indicates error in line 107 column 113 of StaticEA.mq5, so please copy and paste this one (identify it to be analysed here) or just double check it if there is some variable that assumes division by zero value.

Thanks for the info - source code line is 107

  position = MathRound(position * 100)/100;

where does column 113 show? is this from the source (not 113 columns) or from the logs somewhere?

Also do you know what the first 2 columns in the log mean?

Thanks

Rogerio Figurelli
Moderator
39655
Rogerio Figurelli 2012.12.06 02:55  
BigAl:

Thanks for the info - source code line is 107

  position = MathRound(position * 100)/100;

where does column 113 show? is this from the source (not 113 columns) or from the logs somewhere?

Also do you know what the first 2 columns in the log mean?

Thanks

Hi BigAl, looks like some C++ collateral effect since you are using a constant as denominator.

Note that in your Print debug line the position value looks Null, and is not show (for example, we are expecting target TTT - position PPP = ...., but PPP is void in this Print) 

IS    0    StaticEA (EURUSD,M15)    16:59:01    target 652630.0 - position = 0.0 = 652630.0 > 600

 So if position is Null maybe this divide by zero is a MathRound error return and you have to solve and identify why position is Null when running this line.

 Please post more code, like position declaration and other position variable operation before this Print to help we analyse it.


BigAl
489
BigAl 2012.12.06 20:50  
figurelli:

Hi BigAl, looks like some C++ collateral effect since you are using a constant as denominator.

Note that in your Print debug line the position value looks Null, and is not show (for example, we are expecting target TTT - position PPP = ...., but PPP is void in this Print) 

 So if position is Null maybe this divide by zero is a MathRound error return and you have to solve and identify why position is Null when running this line.

 Please post more code, like position declaration and other position variable operation before this Print to help we analyse it.


currently running as requested but still waiting for error. will post again when it fails again
BigAl
489
BigAl 2012.12.07 11:54  
figurelli:

Hi BigAl, looks like some C++ collateral effect since you are using a constant as denominator.

Note that in your Print debug line the position value looks Null, and is not show (for example, we are expecting target TTT - position PPP = ...., but PPP is void in this Print) 

 So if position is Null maybe this divide by zero is a MathRound error return and you have to solve and identify why position is Null when running this line.

 Please post more code, like position declaration and other position variable operation before this Print to help we analyse it.


Hi again, have another divide by zero error. please see the associated code, expert log and journal below. Is this something to do with floating point maybe?

expert log last few ticks:

EK    0    StaticEA (EURUSD,M15)    16:58:57    After select = true
NF    0    StaticEA (EURUSD,M15)    16:58:57    After position = 652426.21
PN    0    StaticEA (EURUSD,M15)    16:58:57    After rounding 652426.21
MI    0    StaticEA (EURUSD,M15)    16:58:57    After normalise = 652426.21
DL    0    StaticEA (EURUSD,M15)    16:59:00    After select = true
MG    0    StaticEA (EURUSD,M15)    16:59:00    After position = 0.0
CO    0    StaticEA (EURUSD,M15)    16:59:00    After rounding 0.0
FK    0    StaticEA (EURUSD,M15)    16:59:00    After normalise = 0.0
HO    0    StaticEA (EURUSD,M15)    16:59:00    After select = true
QJ    0    StaticEA (EURUSD,M15)    16:59:00    After position = 0.0
OS    0    StaticEA (EURUSD,M15)    16:59:00    After rounding 0.0
RG    0    StaticEA (EURUSD,M15)    16:59:00    After normalise = 0.0
PL    0    StaticEA (EURUSD,M15)    16:59:00    target 652630.0 - position = 0.0 = 652630.0 > 600

OK    2    StaticEA (EURUSD,M15)    16:59:00    zero divide in 'StaticEA.mq5' (115,113)

journal log last few entries previous day

PL    0    Network    03:03:35    '20037049': scanning network for access points
QK    0    Network    03:03:38    '20037049': scanning network finished

RR    0    Trades    12:25:12    '20037049': exchange buy 0.01 EURUSD at market
HE    0    Trades    12:25:12    '20037049': order #23830852 buy 0.01 / 0.01 EURUSD at 1.29618 done
KD    0    Trades    12:25:12    '20037049': deal #17349090 buy 0.01 EURUSD at 1.29618 done (based on order #23830852)
KO    0    Trades    14:50:26    '20037049': exchange sell 0.01 EURUSD at market
IK    0    Trades    14:50:26    '20037049': order #23832912 sell 0.01 / 0.01 EURUSD at 1.29622 done
KI    0    Trades    14:50:26    '20037049': deal #17350297 sell 0.01 EURUSD at 1.29622 done (based on order #23832912)
GD    0    Network    15:03:35    '20037049': scanning network for access points
CR    0    Network    15:03:37    '20037049': scanning network finished
LE    0    Experts    16:59:00    expert StaticEA (EURUSD,M15) removed
HL    0    Network    21:03:35    '20037049': scanning network for access points
LJ    0    Network    21:03:37    '20037049': scanning network finished

today

PL    0    Network    03:03:35    '20037049': scanning network for access points
QK    0    Network    03:03:38    '20037049': scanning network finished


  PositionSelect(_Symbol);
  Print("After select = ", PositionSelect(_Symbol));
  position = myprice.ask * PositionGetDouble(POSITION_VOLUME) * 100000;
  Print("After position = ", position);
  position = MathRound(position * 100)/100;
  Print("After rounding ", position);
  NormalizeDouble(position, 2);
  Print("After normalise = ", position);
  if (target > position)
   {
    if (target - position < 600) return;
    Print("target ", target, " - position = ", position, " = ", target - position, " > 600");
    Print("target - position = ", target - position, " / ask ", myprice.ask, " = ", (target - position)/myprice.ask);
    Print("(target - position) / myprice.ask / 1000 = ", (target - position) / myprice.ask / 1000);
    Print("(target = position) / myprice.ask / 1000 / 100 = ", (target - position) / myprice.ask / 1000 / 100);
    Print("Rounded = ", MathRound((target - position)/myprice.ask/1000) / 100);
    newvolume = MathRound((target - position)/myprice.ask/1000) / 100;

from the above you can see that the position select stays true, no trades are done yet within 3 seconds the position calculation after the select changes from 652426.21 to zero.

It is as if the server is busy and returns the PositionGetDouble(POSITION_VOLUME) as zero. So i will add that as a print check while waiting for your response.

Documentation on MQL5: Standard Constants, Enumerations and Structures / Trade Constants / Position Properties
Documentation on MQL5: Standard Constants, Enumerations and Structures / Trade Constants / Position Properties
  • www.mql5.com
Standard Constants, Enumerations and Structures / Trade Constants / Position Properties - Documentation on MQL5
BigAl
489
BigAl 2012.12.12 09:26  
figurelli:

Hi BigAl, looks like some C++ collateral effect since you are using a constant as denominator.

Note that in your Print debug line the position value looks Null, and is not show (for example, we are expecting target TTT - position PPP = ...., but PPP is void in this Print) 

 So if position is Null maybe this divide by zero is a MathRound error return and you have to solve and identify why position is Null when running this line.

 Please post more code, like position declaration and other position variable operation before this Print to help we analyse it.


OK found the problem but need help/explanation of why its happening
  Print(" mypsice.ask ", myprice.ask, " * Volume ", PositionGetDouble(POSITION_VOLUME), " * 100000 = ",
         myprice.ask * PositionGetDouble(POSITION_VOLUME) * 100000);
  
  position = myprice.ask * PositionGetDouble(POSITION_VOLUME) * 100000;

when these code lines are executed myprice.ask is returning 0.0 sometimes

GG    0    StaticEA (EURUSD,M15)    17:03:57    position = 0.0 - target 652630.0 = -652630.0 > 600

can you explain why myprice.ask does this. is it a bug or some sort of restriction I am not aware of?

Get in touch with developers using Service Desk!
Get in touch with developers using Service Desk!
  • www.mql5.com
We therefore attach great importance to all user reports about issues in our programs and try to answer each one of them.
BigAl
489
BigAl 2012.12.12 09:33  
phi.nuts:

Hi BigAl,

1. Next time when posting code, please use SRC button till succeed.

 

2. You ask for advice by only showing partial of the code, you may get the answer but may not what you want

 

OK found the problem but need help/explanation of why its happening
  Print(" myprice.ask ", myprice.ask, " * Volume ", PositionGetDouble(POSITION_VOLUME), " * 100000 = ",
         myprice.ask * PositionGetDouble(POSITION_VOLUME) * 100000);
  
  position = myprice.ask * PositionGetDouble(POSITION_VOLUME) * 100000;

when these code lines are executed myprice.ask is returning 0.0 sometimes

GG    0    StaticEA (EURUSD,M15)    17:03:57    position = 0.0 - target 652630.0 = -652630.0 > 600

can you explain why myprice.ask does this. is it a bug or some sort of restriction I am not aware of?


12
To add comments, please log in or register