sleep() not working in this code!!

 

i used the sleep() in the following code to delay the execution of code that follows  the sleep(). but it didnt work..is this code wrong?....or is there any other option to use instead of sleep.

i wanted the timedelay to be true at "1.33", whereas it gets true by "1.28", which means i think sleep is not working....is it so?

{  
    bool timedelay=false;
    
    int datetime800 = TimeCurrent();
    double hour1 = TimeHour(datetime800);
    double min0=TimeMinute(datetime800);
    double min1=min0/100;
    double hour0=hour1+min1;
    
    
    
    if  (hour0==1.28)
    {   
        Sleep(300000);
        timedelay=true;

    }
 
}
 

Is this code part of an indicator, rather than an EA or script?

Sleep() does not work in indicators. https://docs.mql4.com/common/Sleep

 

What a horrible way of trying to check if the time is 1:28 am

Your  (hour0==1.28)   may well not work because you are comparing doubles . . . . read this thread: Can price != price ?


Why not just doe something like this ?

    if  (TimeHour(TimeCurrent()) == 1 && TimeMinute(TimeCurrent()) == 28)
       {   
       Sleep(300000);
       timedelay=true;
       }


 

 
i tried that too, but it doesnt work still... if i remove the sleep it works the same, if i include sleep still the situation is same. and btw this is ea not an indicator....hence i think sleep should have worked.
 
kmnatarajan:
i tried that too, but it doesnt work still... if i remove the sleep it works the same, if i include sleep still the situation is same. and btw this is ea not an indicator....hence i think sleep should have worked.
How did you try it when the Markets are closed ?
 
i tested using strategy tester......
 
kmnatarajan:
i tested using strategy tester......

LOL . . .

 

read this:    Testing Features and Limits in MetaTrader 4  

especially the bit where it says . . .  

"Some functions are processed/passed without output

These are Sleep(), Alert(), SendMail(), PlaySound(), MessageBox(), WindowFind(), WindowHandle(), WindowIsVisible()"


 
oops, my bad....thanks for the info. will try in realtime and post wat happened.
 
RaptorUK:

What a horrible way of trying to check if the time is 1:28 am

Your  (hour0==1.28)   may well not work because you are comparing doubles . . . . read this thread: Can price != price ?

Or something like
#define HR2400      86400           // 24 * 3600
datetime TimeOfDay(datetime when){  return( when % HR2400          );       }
datetime DateOfDay(datetime when){  return( when - TimeOfDay(when) );       }
//////////
#define HR0128 5280 // 1*3600 + 28*60
datetime now = TimeCurrent();
int      tod = TimeOfDay(now)
if (tod == HR0128) ...
 

i tested this in real....this is wat i wanted to happen, if the time is 1.28, then delay 5 mins, then place a buy order at open time of next bar.


But now the order gets placed at 1.33 for this code...am i wrong somewhere?

datetime BarTime = 0;
bool timedelay=false;

  if (TimeHour(TimeCurrent()) == 1 && TimeMinute(TimeCurrent()) == 28)
    { 
       Sleep(300000);
       timedelay=true;
     }
     
 
     if (BarTime < Time[0] && timedelay=true )
    {
        
        BarTime = Time[0]; 
        BuyOrder1;
        
    } 
 
kmnatarajan:

i tested this in real....this is wat i wanted to happen, if the time is 1.28, then delay 5 mins, then place a buy order at open time of next bar.


But now the order gets placed at 1.33 for this code...am i wrong somewhere?


So you want the Order placed at the open of the 01:35 bar ? 


After your sleep BarTime equals 0,  this is less than Time[0]  so BuyOrder1 is called . . .   try this . . .

datetime BarTime = 0;
bool timedelay = false;

if (TimeHour(TimeCurrent()) == 1 && TimeMinute(TimeCurrent()) == 28)
   { 
   Sleep(300000);
   timedelay = true;
   BarTime = Time[0];       //  <--  add this line
   }
     
if (BarTime < Time[0] && timedelay )   // <---- fix this line
   {
   BarTime = Time[0]; 
   BuyOrder1;
   } 

 

 By the way . . .  timedelay=true  should give an error ? 

Reason: