Download MetaTrader 5

Sleep() function wrongly documented in the mql4 reference

To add comments, please log in or register
bernd
986
bernd  

The Sleep() function documentation in the MQL4 reference currently reads:


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.

Parameters:
milliseconds - Sleeping interval in milliseconds.


This is incomplete or one might be even tempted to call it wrong. Either the documentation is the intended behavior and the actual implementation and behavior of Sleep() is plain wrong or the implementation is as it is intended to be, then the documentation should be changed:


Sleep() will not sleep the specified interval in milliseconds by making use of precise timer functions, instead it seems it will simply count a predetermined number of clock cycles per requested millisecond. Try switching the clock for example from 1200MHz to 600MHz while the program is running and every Sleep(1000) will suddenly sleep 2000 milliseconds. Run a few more applications on the same machine and it will slow down even more. The documentation should mention this.

JC
1514
JC  
7bit:

This is incomplete or one might be even tempted to call it wrong. Either the documentation is the intended behavior and the actual implementation and behavior of Sleep() is plain wrong or the implementation is as it is intended to be, then the documentation should be changed: [...]

I would imagine that MT4's Sleep() function is a simple wrapper around the Win32 Sleep() function. That's got well-known limitations as e.g. summarised in the 4th post on the following page:

http://social.msdn.microsoft.com/Forums/en-US/csharpgeneral/thread/ffad3056-6913-4a09-b2f9-5ceb1a67a06a

Personally, I don't think it's unreasonable for MetaQuotes to omit these detailed caveats from their documentation. They could perhaps add something like the following: "Only use Sleep() for short-term delays to stop your EA consuming 100% of processor time. Do not use it in order to delay execution for a precise, lengthy period of time."

Philippe
201
Philippe  
jjc:

I would imagine that MT4's Sleep() function is a simple wrapper around the Win32 Sleep() function. That's got well-known limitations as e.g. summarised in the 4th post on the following page:

https://www.mql5.com/go?link=http://social.msdn.microsoft.com/Forums/en-US/csharpgeneral/thread/ffad3056-6913-4a09-b2f9-5ceb1a67a06a

Personally, I don't think it's unreasonable for MetaQuotes to omit these detailed caveats from their documentation. They could perhaps add something like the following: "Only use Sleep() for short-term delays to stop your EA consuming 100% of processor time. Do not use it in order to delay execution for a precise, lengthy period of time."


MQL4 is a russian software...not a swiss one :)
Paul
632
Paul  
You can't go changing the time base of your computer and expect the timers to run at the same speed. It's like changing the length of the pendulum of a grandfather clock from 1 metre to 2 metres and expect it to still tick in seconds or like fitting big wheels to your car and expecting the speedo to read the right speeds. The most annoying thing is Sleep() doesn't work in an indicator.
Philippe
201
Philippe  
Ruptor:
You can't go changing the time base of your computer and expect the timers to run at the same speed. It's like changing the length of the pendulum of a grandfather clock from 1 metre to 2 metres and expect it to still tick in seconds or like fitting big wheels to your car and expecting the speedo to read the right speeds. The most annoying thing is Sleep() doesn't work in an indicator.


The most annoying thing is Sleep() doesn't work in an indicator.

I have write this code which is compatible with backtester. May be is't ok with indicator. I let you try.

#import  "shell32.dll"   
#import "kernel32.dll"

int SleepEx( int dwMilliseconds, bool bAlertable);    


#include <WinUser32.mqh>
#include <stderror.mqh>
                                                    


// Sleep compatible avec le tester
//------------------------------------------------------------------------------------------------------------------------------------- Waiting
void Waiting(double MesSecondes=0)
{
   if (IsTesting() || IsOptimization())
   {
   int Tempo = (1000 * MesSecondes);
   
   SleepEx(Tempo,false);         
   }
   else
   {
   Sleep(MesSecondes*1000);            //sleep libère le processeur pour autre tâche mais n'est pas reconnu par le tester
   }
}
//------------------------------------------------------------------------------------------------------------------------------------- 
Paul
632
Paul  
Matutin:


The most annoying thing is Sleep() doesn't work in an indicator.

I have write this code which is compatible with backtester. May be is't ok with indicator. I let you try.

Thanks for the code but why would you want the sleep command to work in back testing mode? Time does not run as real time in the back tester so why would you want to wait a real number of seconds? I don't understand. If a psuedo delay is required in the code during back testing then surely it must take a note of the tick time and decide if the next tick is at a greater time than the required delay. It certainly can't be acccurate because there is no clock during back testing only tick times.
Philippe
201
Philippe  
Ruptor:
Thanks for the code but why would you want the sleep command to work in back testing mode? Time does not run as real time in the back tester so why would you want to wait a real number of seconds? I don't understand. If a psuedo delay is required in the code during back testing then surely it must take a note of the tick time and decide if the next tick is at a greater time than the required delay. It certainly can't be acccurate because there is no clock during back testing only tick times.

Only for cpu management while waiting for file be copied or created by other program call from my ea
whroeder1
15917
whroeder1  
Matutin:

Only for cpu management while waiting for file be copied or created by other program call from my ea
You don't need the wait. Display the current values, once the file is updated, display the updated values on the next tick. If you're depending on an asynchronous task to update, then the indicator should change asynchronously.
Philippe Pauleau
154
Philippe Pauleau  
Paul:
Thanks for the code but why would you want the sleep command to work in back testing mode? 

In visual mode backtesting it is usefull.

To add comments, please log in or register