Download MetaTrader 5
To add comments, please log in or register
Have you found an interesting application? Publish it in Code Base - millions of traders will appreciate that!
Meier
15
Meier 2010.11.18 20:04 

hello mql community,

i'm lucky to be a part of this community. mql is the first programming language that makes sense. :-)

but i've a problem: it seems so that the sleep function doesn't work in backtest. is that right? is there a solution for that?


best regards, derox

gordon
2322
gordon 2010.11.18 21:03  

Sleep() is 'skipped' in the Tester. From the article 'Testing Features and Limits in MetaTrader 4':

Some functions are processed/passed without output

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

Technically it's impossible to simulate Sleep(), since the Tester's time is quantized as opposed to being continuous (as in Live/Demo). U can simulate Sleep() to some degree by skipping ticks, but that only enables sleeping the amount of time between ticks. That time is determined by the specific bar's volume value, since the Tester produces as many ticks in a bar as the volume in that bar (in 'Every Tick' mode).

So for example if you are Testing on M1 and a certain bar has a volume of 20, u will only be able to skip 60/20=3 sec at a time, so u will only be able to simulate sleeping for 3 secs. Depending on what u are trying to achieve this might be good enough for u.

As to how to technically do it:

int start()
{
   static int wait = 0;
   if ( ... )                             // condition for sleeping
      wait = TimeCurrent() + 5;           // sleeping for at least 5 seconds
   if ( TimeCurrent() < wait )            
      return(0);                          // skiping to next tick

   // main program code here...
   
   return(0);
}

Note that this will make it "sleep" for at least 5 seconds (but maybe much more than that)... Also, the following will NOT work in the Tester (although it will work in Live/Demo):

int start()
{
   int wait = 0;
   if ( ... )                              // condition for sleeping
      wait = TimeCurrent() + 5;            // sleeping for at least 5 seconds
   while ( TimeCurrent() < wait ) {}       // this will NOT work in the Tester, it would cause it to loop endlessly...

   // main program code here...
   
   return(0);
}
gordon
2322
gordon 2010.11.18 21:09  
There is an article about the pros and cons of sleeping here -> https://www.mql5.com/en/articles/1558.
Meier
15
Meier 2010.11.18 22:29  
gordon:
There is an article about the pros and cons of sleeping here -> https://www.mql5.com/en/articles/1558.

thanks gordon for your answer. the problem is that the signal for opening a trade is still valid if the expert hits the take profit. result is he open another position, but the signal is weak at this time. i can filter this trades manually, but it takes time. i thought there was an easy way. ;-p thank you very much for your help.
HarriMQL5
285
HarriMQL5 2011.05.15 14:27  
gordon:

Sleep() is 'skipped' in the Tester. From the article 'Testing Features and Limits in MetaTrader 4':

Technically it's impossible to simulate Sleep(), since the Tester's time is quantized as opposed to being continuous (as in Live/Demo). U can simulate Sleep() to some degree by skipping ticks, but that only enables sleeping the amount of time between ticks. That time is determined by the specific bar's volume value, since the Tester produces as many ticks in a bar as the volume in that bar (in 'Every Tick' mode).

So for example if you are Testing on M1 and a certain bar has a volume of 20, u will only be able to skip 60/20=3 sec at a time, so u will only be able to simulate sleeping for 3 secs. Depending on what u are trying to achieve this might be good enough for u.

As to how to technically do it:

Note that this will make it "sleep" for at least 5 seconds (but maybe much more than that)... Also, the following will NOT work in the Tester (although it will work in Live/Demo):

Thanks! You answered my question about why my EA got stuck in an endless loop, when it tries using TimeCurrent to try to break-out.

But then how do I know my code will work (in backtest) before putting it into demo? I'm trying to create a loop to retry trading commands (OrderSend, OrderClose) until a timeout is hit. When the timeout is hit, the code should give up and start() fn exits until the next tick.

lai kok sen
5
lai kok sen 2012.03.12 08:42  
actually, the suggested method of using timecurrent() and adding a wait time does works, if you code it out logically in your EA, i use that all the time to beat the sleep() issue i need in backtest...and live as well.
whroeder1
13616
whroeder1 2012.03.12 14:51  
senshine:
actually, the suggested method of using timecurrent() and adding a wait time does works, if you code it out logically in your EA, i use that all the time to beat the sleep() issue i need in backtest...and live as well.
Reread the code. He didn't use a test of TimeCurrent() and return. he used a loop, which will NEVER work in the tester.
Ai Lik Tey
152
Ai Lik Tey 2013.11.03 05:35  

This is an old thread. I'm just adding a code hoping that this will help someone.

The Sleep() function do not work during Backtesting as it is automatically skipped. I use this custom sbSleepForMS() routine in place of the standard Sleep() function. This sbSleepForMS() will sleep for x milliseconds regardless of whichever mode it is in (Live or Backtest). It works for me,

void sbSleepForMS( int pviSleepForMS ) {
        //This is a Sleep() function designed to work on either LIVE mode or BACKTEST mode.
        // The built-in Sleep() function is skipped by MQL4 during Backtesting, thus another
        // way to simulate the Sleep() function is required. We will use a while loop for it.

        if( !IsTesting() )
                Sleep(pviSleepForMS);
        else {
                //This section uses a while loop to simulate Sleep() during Backtest.
                int viSleepUntilTick    = GetTickCount() + pviSleepForMS;
                while( GetTickCount() < viSleepUntilTick ) {
                        //Do absolutely nothing. Just loop until the desired tick is reached.
                }
        }
}
Simon Gniadkowski
Moderator
17963
Simon Gniadkowski 2013.11.03 09:19  
fhlee74:

This is an old thread. I'm just adding a code hoping that this will help someone.

The Sleep() function do not work during Backtesting as it is automatically skipped. I use this custom sbSleepForMS() routine in place of the standard Sleep() function. This sbSleepForMS() will sleep for x milliseconds regardless of whichever mode it is in (Live or Backtest). It works for me,

If you understood how the Strategy Tester worked you would understand why sleep() in the Strategy Tester makes absolutely no logical sense . . .
whroeder1
13616
whroeder1 2013.11.03 13:29  
And why your sbSleepForMS() will only slow down your optimization but do nothing else.
Ai Lik Tey
152
Ai Lik Tey 2014.07.16 19:48  

I do not know why others would need it. But for me:

- I uses it during VISUAL MODE. When some condition happen during backtest, I would like the process to be "slowed down" or "paused" so I can check some of the params and conditions. This is probably during development/debug stage.

- Another time when I use it is also with VISUAL MODE, when I want to make a video of how the EA works, I will put the sbSleepForMS() when my EA opens/closes trades. I will then set the backtest to run fastest.
This way, when my video plays, or when I want to show people the EA "in action", it will skip all the boring parts (will zoom past with the fastest speed) and when my EA opens/closes orders, it will slow down during those times. This will create a more "visually appealing" playback.

/
To add comments, please log in or register