The OnTimer() function is called when the Timer event occurs, which is generated by the system timer only for Expert Advisors and indicators.
//+------------------------------------------------------------------+ //| Expert initialization function | //+------------------------------------------------------------------+ int OnInit() { //--- create timer EventSetTimer(120); //--- return(INIT_SUCCEEDED); } //+------------------------------------------------------------------+ //| Expert deinitialization function | //+------------------------------------------------------------------+ void OnDeinit(const int reason) { //--- destroy timer EventKillTimer(); } //+------------------------------------------------------------------+ //| Expert tick function | //+------------------------------------------------------------------+ void OnTick() { //--- } //+------------------------------------------------------------------+ //| Timer function | //+------------------------------------------------------------------+ void OnTimer() { //---Do Something every 120 seconds... } //+------------------------------------------------------------------+
For 5 min and other exact Time frames you can also compare Bars.
int bars; //+------------------------------------------------------------------+ //| Expert initialization function | //+------------------------------------------------------------------+ int OnInit() { bars=iBars(_Symbol,PERIOD_CURRENT); //--- return(INIT_SUCCEEDED); } //+------------------------------------------------------------------+ //| Expert deinitialization function | //+------------------------------------------------------------------+ void OnDeinit(const int reason) { } //+------------------------------------------------------------------+ //| Expert tick function | //+------------------------------------------------------------------+ void OnTick() { //--- if(bars<iBars(_Symbol,PERIOD_CURRENT);) { //New Bar ! Do something bars=iBars(_Symbol,PERIOD_CURRENT); } } //+------------------------------------------------------------------+
ok that's fair enough it could be a crude method but i will tell you this, about 80% of my profitable algorithms make use of it in one way or another.
Do you know of any other way to check to see if a new bar has arrived?
ok that's fair enough it could be a crude method but i will tell you this, about 80% of my profitable algorithms make use of it in one way or another.
Doesn't matter, Bars or iBars is not reliable to identify a new bar.
The only 100% reliable way is using time.
So what is a better method?
I tried to synchronize the timer with the bar count and that proved to be a larger disaster as it goes negative on many occasions.
So what is a better method?
![The "New Bar" Event Handler The "New Bar" Event Handler](https://c.mql5.com/36/6/the-new-bar-event-handler.png)
- 2010.10.11
- Konstantin Gruzdev
- www.mql5.com
Yes ok that is a function of mql5 i am still using mql4 i tried mql5 for a long period but for some unknown reason, i could never make it do the magic i can make mql4 do so im affraid in mql4 my methods are somewhat limited.
The same can be done with mql4.
By the way, you can see how to use the time to detect new bar, no need for a complex solution as in this article.
Ok so do you perhaps know if the ontimer is handled in mql5 strategy tester? i have looked at the docs but i can not find it and its not handled in mql4 which is a rather large obstacle for me so if it is possible in mql5 that alone would be a good reason to switch over and try once more.
![MQL5 - Language of trade strategies built-in the MetaTrader 5 client terminal](https://c.mql5.com/i/registerlandings/logo-2.png)
- Free trading apps
- Over 8,000 signals for copying
- Economic news for exploring financial markets
You agree to website policy and terms of use
Can someone help me ?
i'd like execute my orders every 1 or 2 or 5 Min in EA § How can I configure MQL4 for this ?