Download MetaTrader 5

Sleep() doesn't sleep in Custum Indicators start()

To add comments, please log in or register
Jon Doe
54
Jon Doe  

hi there,

i need a Sleep() in a custom indicators start() function but it doesn't work.

so let's say this is the indicator:

#property indicator_chart_window

int start() {
    datetime start_time = TimeLocal();
    Sleep(2000);
    Print("elapsed: ", TimeLocal() - start_time);

    Print("");
}

if i attached that to a chart it always prints "elapsed: 0" but it should be "elapsed: 2"

what's wrong?

Airat Safin
1300
Airat Safin  

Documentation / Sleep ():

The Sleep() function suspends execution of the current expert within the specified interval.
The Sleep() function cannot be called from custom indicators since they calculate in the interface thread and may not decelerate it.
The checking of the expert stop flag status every 0.1 second has been built into the function.

whroeder1
15326
whroeder1  
Why would you want to sleep in an indicator? Calculate the indicator value for the display and return. You wait until a new tick and MT4 calls your start()
chipdude
69
chipdude  
WHRoeder:
Why would you want to sleep in an indicator? Calculate the indicator value for the display and return. You wait until a new tick and MT4 calls your start()
See function ArrayCopyRates(). I use ArrayCopyRates() to link MT4 data to a custom DLL at init(). This function can fail, returning an error code 4066, if data is still updating when the call is made. In the example provided the handling is to call Sleep(5000) then repeat the call to ArrayCopyRates(). I am researching why my code never returns from the call to Sleep(). If Sleep() cannot be used from within a custom indicator then a more robust implementation of ArrayCopyRates() is required.
Al W
173
Al W  

You can do it in start(). Since you only wanted to send the mt4 data the one time, maybe set a global bool variable to true if it completed successfully. You might have to also set that bool to false in the deinit() function depending on your needs.

Although worded a bit funny, I believe they specifically stated in the documentation not to try to deal with market prices in the init() function.

"It is not recommended to call start() function from init() function or to perform trade operations, as chart data, market prices, etc. can be incomplete by the moment of the module initialization."

ttechnik
256
ttechnik  
dring:

hi there,

i need a Sleep() in a custom indicators start() function but it doesn't work.

so let's say this is the indicator:

#property indicator_chart_window

int start() {
datetime start_time = TimeLocal();
Sleep(2000);
Print("elapsed: ", TimeLocal() - start_time);

Print("");
}

if i attached that to a chart it always prints "elapsed: 0" but it should be "elapsed: 2"

what's wrong?


https://www.mql5.com/en/forum/127105

whroeder1
15326
whroeder1  

Don't dredge up old posts. Thread start date 2010.03.06 16:03. I don't think the OP is still waiting four years.

And you still can't sleep in indicators. Using your link would bring the terminal to a crawl.

Umberto
40
Umberto  
You can simulate Sleep() in Indicators with this function,

//----------------------------------------------------------------------------+
int SleepXIndicators(int milli_seconds)
  {
   uint cont=0;
   uint startTime;
   int sleepTime=0;
   startTime = GetTickCount();
   while (cont<500000000)
     {
      cont++;
      sleepTime = (int)(GetTickCount()-startTime);
      if ( sleepTime >= milli_seconds ) break;
     }   
   return(sleepTime);
  }
//----------------------------------------------------------------------------+

to be called with

SleepXIndicators(milliseconds)
Alain Verleyen
Moderator
32022
Alain Verleyen  
umbertosm:
You can simulate Sleep() in Indicators with this function,


to be called with

 

 

There is no valid reason to "Sleep" in an indicator.
whroeder1
15326
whroeder1  
chipdude: See function ArrayCopyRates(). I use ArrayCopyRates() to link MT4 data to a custom DLL at init(). This function can fail, returning an error code 4066, if data is still updating when the call is made. In the example provided
  1. The example provided is an EA.
  2. You are in an indicator. If you slept (can't) or use a busy loop (umbertosm) nothing else will happen, EVER - the terminal is waiting for the indicator to return.
  3. Not necessary. Just call ACR (using a MqlRates array) in init, report any non-4066 errors and RETURN. On the tick, test if array has size and if array[0].time is non-zero. Otherwise ignore the tick.
To add comments, please log in or register