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

Back to topics list To post a new topic, please log in or register
avatar
13
dring 2010.03.06 16:03
 

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?


 
avatar
2985
Ais 2010.03.06 16:21 #
 

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.

 
avatar
12985
WHRoeder 2010.03.06 21:44 #
 
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()
 
avatar
22
chipdude 2010.05.18 09:10 #
 
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.
 
avatar
113
circlesquares 2010.05.18 10:16 #
 

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."

 
avatar
81
ttechnik 2014.02.02 21:04 #
 
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://forum.mql4.com/33392

avatar
12985
WHRoeder 2014.02.02 23:30 #
 

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.

avatar
5
umbertosm 2015.01.25 13:50 #
 
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)

 

 

avatar
Moderator
3206
angevoyageur 2015.01.25 14:05 #
 
umbertosm:
You can simulate Sleep() in Indicators with this function,


to be called with

 

 

There is no valid reason to "Sleep" in an indicator.
avatar
12985
WHRoeder 2015.01.25 21:48 #
 
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.
Back to topics list  

To add comments, please log in or register