intermittent divide by zero - page 2

 
  Print(" myprice.ask ", myprice.ask, " * Volume ", PositionGetDouble(POSITION_VOLUME), " * 100000 = ",
         myprice.ask * PositionGetDouble(POSITION_VOLUME) * 100000);
  position = myprice.ask * PositionGetDouble(POSITION_VOLUME) * 100000;


Sorry posted the wrong expert line in previous posts - should be

NR    0    StaticEA (EURUSD,M15)    17:03:57     myprice.ask 0.0 * Volume 5.05 * 100000 = 0.0

as you can see myprice.ask is returning 0.0 from these 2 code lines

The EA does the check below on every tick and it does not give any errors

  if(!SymbolInfoTick(_Symbol, myprice))
   {
    Print("No price error#",GetLastError());
    return;
   }
can you explain why myprice.ask does this. is it a bug or some sort of restriction I am not aware of?
 

Hi BigAl,

For me looks like some memory leak or similar problem, for example declare indicators inside ontick() instead of oninit().

As a suggestion, try comment all code inside ontick() and just test myprice.ask and print this value, that must be ok.

After that, put code by code again (removing comments step by step) and checking myprice.ask to detect when it goes zero. 

 
figurelli:

Hi BigAl,

For me looks like some memory leak or similar problem, for example declare indicators inside ontick() instead of oninit().

As a suggestion, try comment all code inside ontick() and just test myprice.ask and print this value, that must be ok.

After that, put code by code again (removing comments step by step) and checking myprice.ask to detect when it goes zero. 

Sorry for delay - been away - thanks for suggestion - will try and let you know
 
BigAl:
Sorry for delay - been away - thanks for suggestion - will try and let you know

very strange - may be a bug? see start of my code and the associated log. The test for a valid price does not seem to work as Print statements return a zero value but execution of the EA continues instead of performing the return. Any ideas

Fix is easy I just test for myprice.ask != 0  but still would like to know why it does this itermittently

LOG:

KO    0    StaticEA (EURUSD,H1)    17:03:10    1 0.0
RR    0    StaticEA (EURUSD,H1)    17:03:10    2 0.0
EE    0    StaticEA (EURUSD,H1)    17:03:10    3 0.0
OG    0    StaticEA (EURUSD,H1)    17:03:10    After select = true
DQ    0    StaticEA (EURUSD,H1)    17:03:10    4 0.0
DE    0    StaticEA (EURUSD,H1)    17:03:10    After PositionGetDouble(POSITION_VOLUME) = 4.99
GQ    0    StaticEA (EURUSD,H1)    17:03:10    5 0.0
NG    0    StaticEA (EURUSD,H1)    17:03:10     myprice.ask 0.0 * Volume 4.99 * 100000 = 0.0
RQ    0    StaticEA (EURUSD,H1)    17:03:10    6 0.0
PI    0    StaticEA (EURUSD,H1)    17:03:10    After position = 0.0

GM    0    StaticEA (EURUSD,H1)    17:03:10    7 0.0


bool restart = true;
int  i = 0, starthandle = 0, ticket = 0;
double newvolume = 0, position = 0, target = 0;
void OnTick()
 { 
  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;
  if(!SymbolInfoTick(_Symbol, myprice))
   {
    Print("No price error#",GetLastError());
    return;
   }
  Print(1, " ", myprice.ask);
  if(restart == true && PositionSelect(_Symbol) == false)
   {
    myrequest.price = NormalizeDouble(myprice.ask,_Digits);
    myrequest.volume = 5;
    myrequest.type = ORDER_TYPE_BUY;
    OrderSend(myrequest, myresult);
    if(myresult.retcode == 10009 || myresult.retcode == 10008)
     {
      target = myrequest.price * myrequest.volume * 100000;
      target = MathRound(target * 100) / 100;
      NormalizeDouble(target, 2);
      starthandle = FileOpen("target.dat", FILE_READ|FILE_BIN);
      if (starthandle < 0)
       {
        starthandle = FileOpen("target.dat", FILE_WRITE|FILE_BIN);
        if (starthandle < 0)
         {
          Print("File not opened for writing");
          return;
         }
        FileWriteDouble(starthandle, target);
        FileClose(starthandle);
       }
      Sleep(900000);
     }
    else
     {
      Print("Buy failed error#",GetLastError());
      return;
     }
   }
  if (restart == true && PositionSelect(_Symbol)==true)
   {
    starthandle = FileOpen("target.dat", FILE_READ|FILE_BIN);
    if (starthandle > -1)
     {
      target = FileReadDouble(starthandle);
      FileClose(starthandle);
      Print("Target = ",target);
      restart = 0;
     }
    else
     {
      Print("file not read");
     }
   }
  Print(2, " ", myprice.ask);
  PositionSelect(_Symbol);
  Print(3, " ", myprice.ask);
  Print("After select = ", PositionSelect(_Symbol));
  Print(4, " ", myprice.ask);
  Print("After PositionGetDouble(POSITION_VOLUME) = ", PositionGetDouble(POSITION_VOLUME));
  Print(5, " ", myprice.ask);
  Print(" myprice.ask ", myprice.ask, " * Volume ", PositionGetDouble(POSITION_VOLUME), " * 100000 = ",
         myprice.ask * PositionGetDouble(POSITION_VOLUME) * 100000);
  Print(6, " ", myprice.ask);
  position = myprice.ask * PositionGetDouble(POSITION_VOLUME) * 100000;
  Print("After position = ", position);
  Print(7, " ", myprice.ask);
  position = MathRound(position * 100)/100;
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
 
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

 

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

 

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

 

Hi phi.nuts. Please see my last post to figurelli - I would like to see your thoughts on what I have found on this problem - Thanks
Reason: