Hi All,
Is there a way to run OnTick() during the bar close on the current bar? It I do the execution at the start of a new bar and then check the previous bar, my concern is that the execution will be a candle too late if i do this approach. Any thoughts on this? Thanks.
You cannot execute or run OnTick() function.
It is an Event Driven function, which is executed automatically when the application Metatrader receives a Tick from the Symbol.
When a Tick is received, it causes an Event, and that event calls the OnTick() function to be executed.
OnTick has a super high execution frequency, it can be executed hundred times on a second or a minute. For each price change on the market, it receives a Tick, and get executed.
Anything you put inside it, may be called hundred of times in a few seconds. And that is its purpose.
About the "execute at Bar Close", technically this is the same as "execute at Bar Open", because at the same instant when one bar Closes, another Opens. So this is precisely the same moment.
To execute something when a new bar Opens, there are lot of examples here on the forum.
Also, for Strategy Tester, you have that option too, to test your code.
here is an working exemple, to execute functions, when a new bar opens, via OnTick().
datetime PrevBarTime; datetime time_0; int OnInit() { // initialize the variables here on OnInit. // time_0 = iTime(_Symbol, PERIOD_CURRENT, 0); PrevBarTime = iTime(_Symbol, PERIOD_CURRENT, 0); } void OnTick() { // update the time holder time_0 = iTime(_Symbol, PERIOD_CURRENT, 0); // As soon as a candle Closes, which is the // same moment a new one Opens, this will // be true, and execution will go into the // function below // if(time_0 != PrevBarTime) { PrevBarTime = time_0; //call your custom functions inside here // Myfunction(); AnotherFunction(); etc(); } }
You cannot execute or run OnTick() function.
It is an Event Driven function, which is executed automatically when the application Metatrader receives a Tick from the Symbol.
When a Tick is received, it causes an Event, and that event calls the OnTick() function to be executed.
OnTick has a super high execution frequency, it can be executed hundred times on a second or a minute. For each price change on the market, it receives a Tick, and get executed.
Anything you put inside it, may be called hundred of times in a few seconds. And that is its purpose.
About the "execute at Bar Close", technically this is the same as "execute at Bar Open", because at the same instant when one bar Closes, another Opens. So this is precisely the same moment.
To execute something when a new bar Opens, there are lot of examples here on the forum.
Also, for Strategy Tester, you have that option too, to test your code.
here is an working exemple, to execute functions, when a new bar opens, via OnTick().
Ooops, what I meant was to execute a command inside OnTick().
About the bar closes when another one opens do make sense and I will take that approach. Thanks for the advise!
Try to think logically about it! Is it possible to know when the last tick arrives that closes a bar?
The answer is no! You will never know for certain if yet a new tick will arrive before the bar closes. You will only know this when a bar has closed in two distinct situations:
- When the trading server's time enters the new bar's opening time ...
- ... or when a new tick arrives that is the first tick for new bar.
So, your solution is to:
- from within the OnTimer() monitor the TimeCurrent() and detect when it has entered a new bar's time ...
- ... and/or, from within OnTick() monitor for the arrival of the first tick of a new bar.
Option two is the most common method, because the first tick is usually on top of the opening bar's time and the delay between closing and opening a new bar is so minimal that it is inconsequential. Besides you actually need the market to move in order to do anything useful, such as manage trades, etc.
EDIT: Please note however, that OHLC data and indicators only update and mark a new bar when the first tick arrives, so if using the Timing method instead of the new Tick method, you will have to check for this in order to decide if you must analyse the current bar (not closed yet) or the previous bar (because a new bar has just opened).
EDIT2: My advice, stick to the New Tick method which is much more practical! You need the market to move anyway and the delay is inconsequential.
- Free trading apps
- Over 8,000 signals for copying
- Economic news for exploring financial markets
You agree to website policy and terms of use
Hi All,
Is there a way to run OnTick() during the bar close on the current bar? It I do the execution at the start of a new bar and then check the previous bar, my concern is that the execution will be a candle too late if i do this approach. Any thoughts on this? Thanks.