Alternative solution to Sleep() in script

 

Hi there,

I understand that the command Sleep () does not work in MT4 script. Can I use the following to make the delay?

int _time_waiting=0;
   _time_waiting = TimeLocal() + 30; // the pause ends in 30 seconds after the current local time
while ( TimeLocal() < _time_waiting ){}

 
I thought sleep() does work in scripts ? it doesn't work in Indicators or in the Strategy Tester.
 
Limitations are described here:
https://docs.mql4.com/common/Sleep
https://docs.mql4.com/runtime/start
My Sleep() test results:
In Indicator's init(), deinit() and start(): not works, throws LastError 4055 (Custom indicator error.)
In EA's deinit(): not works, no error code
In Script's start(), EA's init() and start(): works with no error code (if not in StrategyTester).
Where not works, it is because these codes run in the terminal's main thread (I tried SleepEx from kernel32.dll and it suspended the entire terminal, in StrategyTester suspended it's thread).
 
Don't use TimeLocal, use TimeCurrent so it works in the StrategyTester
int start(){
   static int _time_waiting=0; if (TimeCurrent() < _time_waiting) return(0); // Wait for next tick
   :
   _time_waiting = TimeCurrent() + 30; // the pause ends in 30 seconds after this tick
}
 
RaptorUK:
I thought sleep() does work in scripts ? it doesn't work in Indicators or in the Strategy Tester.

Raptor, you are correct sleep() does not work in scripts; hence, I am looking for an alternative solution.
 
Isn't TimeCurrent() only updated with an incoming tick? Doesn't that mean it would never be updated in the while condition, and so it would create an infinite loop?
 
No infinite loop. No loop at all. Just return and wait for the next tick.
 
waterhorse:

Raptor, you are correct sleep() does not work in scripts; hence, I am looking for an alternative solution.
Read what I wrote again . . . Sleep() DOES work in Scripts . . . I have coded a script today using Sleep() . . . it certainly does work.
 

Oops, I see what you meant now. I thought you where trying to implement it with waterhorse's while condition - whereas the code you posted was intended to be entirely separate.

That is actually an oddly pleasing piece of code :-)

 
RaptorUK:
Read what I wrote again . . . Sleep() DOES work in Scripts . . . I have coded a script today using Sleep() . . . it certainly does work.

Great, then my problem is solved. Thanks everyone very much for contributing your comments.
Reason: