Currently, my EA opens several orders on the same candle. I think the material in this article would help me.
How do I implement it?
You can take the trade entry logic out of the OnTick() event and put it inside the OnNewBar() function. This way, it will only be executed when there is a new bar, no more entering several times on the same candle, because once it has been executed, it will only execute again on the next candle.
I was hoping for a MQL5 libraries function but it seems there is none? I have a modular trading lib and I ran into the issue where the signal module would set its IsNewBar flag and overrides the prevCandleTime and when TrailingSL module had to evaluate the same function it returned false because the prevCandleTime is same as current.
Yes I can store the result in a central flag and use it for all modules and I would not run onto this is HOWEVER the modules can run on different timeframes, hence this solution is sooo perfect thanks.
In my opinion, sometimes we unnecesarily complicate stuff. For practical reasons nothing, even performance wise, will beat the system time (windows time). At the start of the next minute, or to be sure, after a few seconds, we can assume there will be a new bar for every Symbol. Say minute.. five minutes.. fifteen minutes, or whatever your timeframe. To be sure, you can check if bar time and system time are the same.
But there's no need of creating multiple instances of this class, just to check if there is a new bar, when we all actually know when a new bar will be painted.
Max Go new bar has arrived. IMHO it is easier or am I missing something?
Imagine a situation when the opening price of a new bar is equal to the opening price of the previous bar.... There is a high probability to catch such a situation on small TFs.
I have no simpler method than this one. Tracks the opening time of the current bar and compares them at each tick.
// I don't have a simpler method than this.// Tracks the opening time of the current bar and compares them at each tick.double lastTime = 0;
voidOnTick() {
if(isNewBar()){
Print("New Bar");
}
}
bool isNewBar() {
double thisTime= NormalizeDouble(iTime(_Symbol, PERIOD_CURRENT, 0), _Digits);
if(lastTime != thisTime) {
lastTime = thisTime;
returntrue;
}
returnfalse;
}
Allow the use of cookies to log in to the MQL5.com website.
Please enable the necessary setting in your browser, otherwise you will not be able to log in.
You are signed in. Two-factor authentication has been disabled. Set it up again and scan a new QR code, or continue with two-factor authentication disabled.
Currently, my EA opens several orders on the same candle. I think the material in this article would help me.
How do I implement it?
You can take the trade entry logic out of the OnTick() event and put it inside the OnNewBar() function. This way, it will only be executed when there is a new bar, no more entering several times on the same candle, because once it has been executed, it will only execute again on the next candle.
Good article.
Thanks!
Very very Nice thanks,
I was hoping for a MQL5 libraries function but it seems there is none? I have a modular trading lib and I ran into the issue where the signal module would set its IsNewBar flag and overrides the prevCandleTime and when TrailingSL module had to evaluate the same function it returned false because the prevCandleTime is same as current.
Yes I can store the result in a central flag and use it for all modules and I would not run onto this is HOWEVER the modules can run on different timeframes, hence this solution is sooo perfect thanks.
Hello, I am assuming that as soon as the Open() quote has arrived, it means a new bar has arrived. IMHO it is easier or am I missing something?
Imagine a situation when the opening price of a new bar is equal to the opening price of the previous bar.... There is a high probability to catch such a situation on small TFs.
I have no simpler method than this one.
Tracks the opening time of the current bar and compares them at each tick.