Possible Error :: Cannot set timer error when section in timer function is not commented out .

 

Hi , i have this code i want to test a brokers stop levels .

When i comment out the sections defined on the timer function the timer can be set . 

When i leave it as is i get an error "Cannot set millisecond timer (100)"

It's probably something i missed , but i can't find it. 

Also , an mt5 is running an optimization while this happens , i currently can't stop the optimization to see if the timer can be set however.

#property strict
int OnInit()
  {
  if(!EventSetMillisecondTimer(100)){return(INIT_FAILED);}
  return(INIT_SUCCEEDED);
  }
void OnDeinit(const int reason)
  {
  }
void OnTimer(){
EventKillTimer();
Print("Timer");
//comment out from here >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
double minlot=(double)SymbolInfoDouble(_Symbol,SYMBOL_VOLUME_MIN);
if(minlot>0){
  //open up in sequence and the close immediately 
    int distance=100,step=10;
    for(int i=0;i<10;i++){
    double up=Ask+((double)distance)*_Point;
    //buy stop 
      ResetLastError();
      int ticket=OrderSend(_Symbol,OP_BUYSTOP,minlot,up,1000,0,0,NULL,0,0,clrAliceBlue);
      if(ticket!=-1){
        Print("Buy stop placed at distance ("+IntegerToString(distance)+")");
        Sleep(200);
        ResetLastError();
        
        while(!OrderDelete(ticket,clrBlack)){
             if(GetLastError()==4108){break;}
             ResetLastError();
             Sleep(50);
             }
        Sleep(100);
        }else{
      Print("Cannot place Buy stop at distance ("+IntegerToString(distance)+") #"+IntegerToString(GetLastError()));
      break;
      }
    distance-=step;
    if(distance<10){break;}
    }
  }else{Print("Invalid Lot");}
//comment out till here <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
ExpertRemove();
}
 
Lorentzos Roussos:

Hi , i have this code i want to test a brokers stop levels .

When i comment out the sections defined on the timer function the timer can be set . 

When i leave it as is i get an error "Cannot set millisecond timer (100)"

It's probably something i missed , but i can't find it. 

Also , an mt5 is running an optimization while this happens , i currently can't stop the optimization to see if the timer can be set however.

Someone run this on their machine , that was not running an optimization on mt5 and they dont get the error.

It has to do with the cores being used or something , i guess

 
Lorentzos Roussos #: Someone run this on their machine , that was not running an optimization on mt5 and they dont get the error. It has to do with the cores being used or something , i guess

When setting the timer, you have to retry several times, as it sometimes fails on the first few calls.

William has posted about this several times. Do a search of his posts about it.

 

Forum on trading, automated trading systems and testing trading strategies

Error: cannot set timer (1)

William Roeder, 2020.02.24 17:50

  1. You have to on MT4, because sometime it returns an error (4030.)

    EST can fail. The default suggestion is to loop until you enable it.
              Build 1080 Cannot Set Timer error with EventSetTimer function - Indices - MQL4 programming forum #8

    I don't try to enable them in OnInit but instead I keep trying to enable them in OnTick/OnCalculate until they succeed. (No race condition, no CPU loop, no timer ticks before chart is updated.)
              Cannot set timer (60) - EA not working - MT4 - MQL4 programming forum

    int OnInit(){
       while(!EventSetTimer(1) ){
          alert_id(StringFormat("EST:%i", _LastError) );
          Sleep(5000);   if(_StopFlag) return INIT_FAILED;
       }

    1  09:31:41.071   order_graphic_tool_v6 USDCHF,Daily: cannot set timer (1)
    0  09:31:41.071   order_graphic_tool_v6 USDCHF,Daily: Alert: order_graphic_tool_v6 USDCHF D1 EST:4030
    0  09:31:41.071   order_graphic_tool_v6 NZDCAD,Daily: Alert: order_graphic_tool_v6 NZDCAD D1 EST:4030
    1  09:31:41.071   order_graphic_tool_v6 GBPCHF,Daily: cannot set timer (1)
    1  09:31:41.071   order_graphic_tool_v6 AUDSGD,Daily: cannot set timer (1)
    0  09:31:41.071   order_graphic_tool_v6 GBPCHF,Daily: Alert: order_graphic_tool_v6 GBPCHF D1 EST:4030
    0  09:31:41.071   order_graphic_tool_v6 AUDSGD,Daily: Alert: order_graphic_tool_v6 AUDSGD D1 EST:4030
    1  09:31:41.071   order_graphic_tool_v6 USDCAD,Daily: cannot set timer (1)
    1  09:31:41.071   order_graphic_tool_v6 GBPJPY,Daily: cannot set timer (1)
    0  09:31:41.071   order_graphic_tool_v6 GBPJPY,Daily: Alert: order_graphic_tool_v6 GBPJPY D1 EST:4030
    0  09:31:41.071   order_graphic_tool_v6 USDCAD,Daily: Alert: order_graphic_tool_v6 USDCAD D1 EST:4030

    It's some type of race condition, highly variable.
    In EA I'm using 5 second sleep, which usually doesn't result in a second attempt. For indicators try and set it in OnCalculate. On failure return zero and retry next tick. This drastically reduces the problem.

 
Fernando Carreiro #:

Thank you 

EST:4024 , both on init and on tick

All cores are busy , probably related 

I'm not good at CS at all , but the machine has not restarted for 30hrs more or less and it was optimizing both on excel's genetic algo and mt5 for about a day straight. 

 
Lorentzos Roussos #: Thank you EST:4024 , both on init and on tick

I wonder if the MQL4 error codes have changed?

According to the documentation ... Runtime Errors - Codes of Errors and Warnings - Constants, Enumerations and Structures - MQL4 Reference

4024

ERR_INTERNAL_ERROR

Internal error

4030

ERR_CHART_NOREPLY

No reply from chart

Runtime Errors - Codes of Errors and Warnings - Constants, Enumerations and Structures - MQL4 Reference
Runtime Errors - Codes of Errors and Warnings - Constants, Enumerations and Structures - MQL4 Reference
  • docs.mql4.com
Runtime Errors - Codes of Errors and Warnings - Constants, Enumerations and Structures - MQL4 Reference
 
Fernando Carreiro #:

I wonder if the MQL4 error codes have changed?

According to the documentation ... Runtime Errors - Codes of Errors and Warnings - Constants, Enumerations and Structures - MQL4 Reference

4024

ERR_INTERNAL_ERROR

Internal error

4030

ERR_CHART_NOREPLY

No reply from chart

What's internal error ?and  why it can set the timer when i comment out a section on the timer function ?

I guess having 2 machines would be better
 
By the way, this is an EA and not an indicator or script, correct?
 
Fernando Carreiro #:
When you comment out the OnTimer function, it probably ignores the EST.

I mean a section inside the function not the on timer function , where the comments are 

Fernando Carreiro #:
By the way, this is an EA and not an indicator or script, correct?

EA , and i'm currently on a 2nd generation i3 , nothing powerful .

Can i shut off use of 1 core in the mt5 tester (while its running preferablly)?

 
Lorentzos Roussos #: I mean a section inside the function not the on timer function , where the comments are 

Yes, I deleted my post after I saw that you were not commenting out the entire function but just part of it.

Lorentzos Roussos #: Can i shut off use of 1 core in the mt5 tester (while its running preferablly)?

Never tried it, so don't know.

 

Try not to kill the timer here:

void OnTimer(){
EventKillTimer();

To make the code inside the OnTimer() be executed only once, you can use the flag.

Perhaps the timer just took offense at you for killing him. He will still write "timer" in his journal, but he does not want to trade for you after you treated him ugly.


This is just another joke of mine, but I think you should check it out.