Discussion of article ""New Bar" Event Handler"

 

New article "New Bar" Event Handler is published:

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.

Author: Константин

 

thnx
 

Very interesting article. Thank you for sharing all of this.

However, some remarks:

Talking about this function, you say :

If you call this prototype function from one place, then we have what we need. But if we want to use this function, for example, again in another place in the same calculation loop, it will always return false, which means that there is no bar. And this will not always be true. Static variable in this case imposes an artificial limit on the number of prototype function calls.

  • You are right. But calling several times a function like isnewbar() during 1 tick is bad practice. Call it only once and keep the result in a variable.
  • A problem is that the solution you provide, using a class without static variable, doesn't resolve what you have stressed. And even situation is now worst. You have to declare the instance of your class as a global variable (object), you have also to initialize it on the OnInit(). One who will use your class must know the operation:
  1. Always use a global variable
  2. Always initialize even if the symbol and period are the default (for time chart).
  • Finally, "if we want to use this function, for example, again in another place in the same calculation loop". Still it ALWAYS RETURN FALSE.

You have replaced a static local variable with a global variable, which is the same and you didn't resolve your problem.


Nevertheless, your article has the merit to push thinking and offer interesting ideas.

 

Good article, thanks for sharing! All that was very useful!

Anyway, I have taken your isNewBar function and it throws the following message when compiling: "possible loss of data due to type conversion".

So I have changed the var types from datetime to long this way:

//+------------------------------------------------------------------+
//| Returns true if a new bar has appeared for a symbol/period pair  |
//+------------------------------------------------------------------+
bool isNewBar()
  {
//--- memorize the time of opening of the last bar in the static variable
   static long last_time=0;
//--- current time
   long lastbar_time=SeriesInfoInteger(CurrencyPair,Period01,SERIES_LASTBAR_DATE);

//--- if it is the first call of the function
   if(last_time==0)
     {
      //--- set the time and exit
      last_time=lastbar_time;
      return(false);
     }

//--- if the time differs
   if(last_time!=lastbar_time)
     {
      //--- memorize the time and return true
      last_time=lastbar_time;
      return(true);
     }
//--- if we passed to this line, then the bar is not new; return false
   return(false);
  }

Now it compiles without any notice and seems to work ok. Thank you!


Documentation on MQL5: Language Basics / Data Types / Typecasting
  • www.mql5.com
Language Basics / Data Types / Typecasting - Documentation on MQL5
 
laplacianlab:

Good article, thanks for sharing! All that was very useful!

Anyway, I have taken your isNewBar function and it throws the following message when compiling: "possible loss of data due to type conversion".

So I have changed the var types from datetime to long this way:

Now it compiles without any notice and seems to work ok. Thank you!


It's probably clearer to change it this way :

   datetime lastbar_time=(datetime)SeriesInfoInteger(Symbol(),Period(),SERIES_LASTBAR_DATE);
 

This article is so good that it is even used in MQL5 programming classes, including comments and collaborations and bug fixes. Congratulations to all of you.

 

I wanted to thank the author of the class, recently I decided to remove the lines of code that used to identify a new bar and added the class of this article, the speed of my EAs increased by 30% in the period, in addition to reducing errors. Thank you very much. I have no words to thank.

 

May I request to know how is the following simple approach?

bool isNewBar() { 
   static long lastBarCount = -1;
   long currentBarCount =  Bars(Symbol(), 0);
   if(lastBarCount != currentBarCount) {
      lastBarCount = currentBarCount;
      return true;
   } else {
      return false;
   }
}
 
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.