Download MetaTrader 5

time trading

To add comments, please log in or register
perianin974
18
perianin974  

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 ?  

Marco vd Heijden
Moderator
6151
Marco vd Heijden  

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);
   }   
  }
//+------------------------------------------------------------------+
Alain Verleyen
Moderator
35125
Alain Verleyen  
Marco vd Heijden:

The OnTimer() function is called when the Timer event occurs, which is generated by the system timer only for Expert Advisors and indicators.

For 5 min and other exact Time frames you can also compare Bars.

iBars() is not reliable in all cases to identify a new bar.
Marco vd Heijden
Moderator
6151
Marco vd Heijden  

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?

Alain Verleyen
Moderator
35125
Alain Verleyen  
Marco vd Heijden:
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.

Marco vd Heijden
Moderator
6151
Marco vd Heijden  

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.

Alain Verleyen
Moderator
35125
Alain Verleyen  
Marco vd Heijden:
So what is a better method?
https://www.mql5.com/en/articles/159
The "New Bar" Event Handler
The "New Bar" Event Handler
  • 2010.10.11
  • Konstantin Gruzdev
  • www.mql5.com
MQL5 programming language is capable of solving problems on a brand new level. Even those tasks, that already have such solutions, thanks to object oriented programming can rise to a higher level. In this article we take a specially simple example of checking new bar on a chart, that was transformed into rather powerful and versatile tool. What tool? Find out in this article.
Marco vd Heijden
Moderator
6151
Marco vd Heijden  
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.
Alain Verleyen
Moderator
35125
Alain Verleyen  
Marco vd Heijden:
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.

Marco vd Heijden
Moderator
6151
Marco vd Heijden  
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 strategy tester, 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.
Alain Verleyen
Moderator
35125
Alain Verleyen  
Marco vd Heijden:
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.
OnTimer is handled on MT5 ST, but not on MT4.
12
To add comments, please log in or register