Sleep and RefreshRates

 

I've noticed some codes Sleep at the start of a code then Refreshes to update values before continuing with the rest.

Is it not better to Sleep at the end of the code? Then RefreshRates is not required?

edit: should probably specify purpose: to make sure the code only runs once per second for example Sleep(1000)

 
eempc:

I've noticed some codes Sleep at the start of a code then Refreshes to update values before continuing with the rest.

Is it not better to Sleep at the end of the code? Then RefreshRates is not required?

edit: should probably specify purpose: to make sure the code only runs once per second for example Sleep(1000)

f you want your code to run every second you can now use the OnTimer() event handler. But you have to use RefreshRates().
 
angevoyageur:
f you want your code to run every second you can now use the OnTimer() event handler. But you have to use RefreshRates().


Is there any available information that OnTimer event has out of date rates and therefore RefreshRates() is mandatory? Just curious, because I would not expect it at all.
 
Ovo:

Is there any available information that OnTimer event has out of date rates and therefore RefreshRates() is mandatory? Just curious, because I would not expect it at all.
You are right, I didn't check. It's only a supposition. We will see when Market will be opened.
 

This is absolutely true and confirmed for OFFLINE charts at least, as of MT4 Build 840. I've been trying to get an EA to run on an offline chart for a few hours now. Every time OnTimer() kicks off (50ms intervals), Time[0] and iTime() and Close[0] are always always from the last bar as of the time the EA was added to the chart, no matter how many bars have been created since the last OnTimer() event had ran. I decided to add RefreshRates() to force it, and it immediately started working as expected. 

I had been using an isNewBar() function that works fine in Strategy Testers, but never on an offline chart directly.  RefreshRates() was the culprit. Heres my newest revision for isNewBar() if anyone is interested.

 

Now, I just call isNewBar(Time[0]) ... But you can call it with iTime() as well, in case you're looking at another chart for some reason.

/*********************************************************
* Bar Functions
**********************************************************/
bool isNewBar(datetime now) {
   static datetime _currentTime;
   if (_currentTime != now) {
      _currentTime = now;
      return (true);
   }   
   return (false);
}
angevoyageur
:

You are right, I didn't check. It's only a supposition. We will see when Market will be opened.
Reason: