Download MetaTrader 5
To add comments, please log in or register
Do you keep a blog? Place a link to MQL5.community there and make money!
mop0
197
mop0 2014.02.08 12:55 

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)

Alain Verleyen
Moderator
28535
Alain Verleyen 2014.02.08 14:17  
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().
Ovo Cz
2958
Ovo Cz 2014.02.08 14:29  
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.
Alain Verleyen
Moderator
28535
Alain Verleyen 2014.02.08 19:13  
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.
Tim Welch
4727
Tim Welch 2015.09.07 14:12  

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.
/
To add comments, please log in or register