Download MetaTrader 5

OnTick() or OnTimer()?

To add comments, please log in or register
Marcos Maia
159
Marcos Maia  

Hello folks, 

 I'm building an EA that must place a trade at the beginning of a new bar. Regarding to less time delay from the opening time of the new bar, which one is more recommended to use: OnTick() or OnTimer()?

Keith Watford
Moderator
10064
Keith Watford  
A new bar requires a new tick, so just use OnTick() and check for a new bar
Marcos Maia
159
Marcos Maia  
GumRai:
A new bar requires a new tick, so just use OnTick() and check for a new bar
Thanks.
JD4
1101
JD4  
GumRai:
A new bar requires a new tick, so just use OnTick() and check for a new bar
Aren't there multiple ticks on each bar?  Like when the price changes at all, doesn't that generate a new tick?  Not very familiar with MQL, sorry if this is obvious to everybody else.
Keith Watford
Moderator
10064
Keith Watford  
JD4:
Aren't there multiple ticks on each bar?  Like when the price changes at all, doesn't that generate a new tick?  Not very familiar with MQL, sorry if this is obvious to everybody else.

Yes, usually. However, each bar can only have 1 first tick

The open time of a bar is stored in a static/globalscope variable.

At each tick the open time of the current bar is compared to the value. If it is not the same, then it must be the first tick of a new bar (received by the Expert)

The variable is reassigned the value of the current bar's open time in order to check for the next new bar.

JD4
1101
JD4  
GumRai:

Yes, usually. However, each bar can only have 1 first tick

The open time of a bar is stored in a static/globalscope variable.

At each tick the open time of the current bar is compared to the value. If it is not the same, then it must be the first tick of a new bar (received by the Expert)

The variable is reassigned the value of the current bar's open time in order to check for the next new bar.

Makes sense.  TY for the answer.
whroeder1
15107
whroeder1  
JD4: Aren't there multiple ticks on each bar? 
  1. not necessarily, "Free-of-Holes" Charts - MQL4 Articles
  2. Ticks per bar vary wildly (100x) from broker to broker.
JD4
1101
JD4  
GumRai:

Yes, usually. However, each bar can only have 1 first tick

The open time of a bar is stored in a static/globalscope variable.

At each tick the open time of the current bar is compared to the value. If it is not the same, then it must be the first tick of a new bar (received by the Expert)

The variable is reassigned the value of the current bar's open time in order to check for the next new bar.

Just thought of something again on this.  When using OnTick(), which will work because of the bar needing that first tick anyway, how is it to differentiate between that (infamous) first tick, and any following ticks related to that same bar?
Keith Watford
Moderator
10064
Keith Watford  
JD4:
Just thought of something again on this.  When using OnTick(), which will work because of the bar needing that first tick anyway, how is it to differentiate between that (infamous) first tick, and any following ticks related to that same bar?

Strange that you quote my post which has already answered your question

GumRai:

Yes, usually. However, each bar can only have 1 first tick

The open time of a bar is stored in a static/globalscope variable.

At each tick the open time of the current bar is compared to the value. If it is not the same, then it must be the first tick of a new bar (received by the Expert)

The variable is reassigned the value of the current bar's open time in order to check for the next new bar.

 If it is not the same, then it must be the first tick of a new bar (received by the Expert)

It follows that if it IS the same, it must be a subsequent tick 

JD4
1101
JD4  
GumRai:

Strange that you quote my post which has already answered your question

 If it is not the same, then it must be the first tick of a new bar (received by the Expert)

It follows that if it IS the same, it must be a subsequent tick 

Sorry, must have been half asleep when I was reading it, because I did not see that until now, going back and re-reading it after you re-pointed it out to me. 
To add comments, please log in or register