Discussion of article ""New Bar" Event Handler" - page 5

 
tito.vinicius:

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.

 
thank you very much for this very nice piece of paper - your efforts are much appreciated!
 

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. 

 
Very informative article, thank you very much
 
Thanks, the best way to detect a new bar! :)
 
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.


 
double Old_open, New_open;
bool  NewBar = false;

int OnInit()
  {
        //---------- 

        Old_open = iOpen(Symbol(),PERIOD_CURRENT,0);
        
        //---------- 

        return(INIT_SUCCEEDED);
  }
void OnTick()
  {
        //---
        New_open = iOpen(Symbol(),PERIOD_CURRENT,0);
        if (New_open != Old_open)
           {
               Print(" There's a new bar coming in.", TimeCurrent());
               NewBar = true;
               Old_open = New_open;
           }
        else NewBar = false;
         
  }

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?

 
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;

void OnTick() {

   if(isNewBar()){

      Print("New Bar");

     }

}



bool isNewBar() {

   double thisTime= NormalizeDouble(iTime(_Symbol, PERIOD_CURRENT, 0), _Digits);

   if(lastTime != thisTime) {

      lastTime = thisTime;

      return true;

     }

   return false;

  }

Документация по MQL5: Преобразование данных / NormalizeDouble
Документация по MQL5: Преобразование данных / NormalizeDouble
  • www.mql5.com
NormalizeDouble - Преобразование данных - Справочник MQL5 - Справочник по языку алгоритмического/автоматического трейдинга для MetaTrader 5