Download MetaTrader 5
To add comments, please log in or register
Millions of traders download codes from MQL5 library. Share your app with them!
MetaQuotes Software Corp.
Moderator
178234
MetaQuotes Software Corp. 2010.10.11 15:26 

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: Константин

behzad jafari
360
behzad jafari 2013.01.12 17:21  

thnx
Alain Verleyen
Moderator
29652
Alain Verleyen 2013.03.26 16:56  

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.

Jordi Bassaganas
3521
Jordi Bassaganas 2013.08.23 11:36  

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
Alain Verleyen
Moderator
29652
Alain Verleyen 2013.08.23 11:59  
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);
/
To add comments, please log in or register