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

 

Prival:

Rarely, but there are situations when quotes for one instrument freeze for a long time (I saw such a situation in the yen). And if the Expert Advisor hangs on this pair, you are in trouble if you have not translated all the code into OnTime().

If you work on OOP or use certain algorithms, you won't need to translate the code. But agree that it is more convenient to get real-time ticks for all pairs in the overview (and not to invent something of your own)....

Lizar:

Such an event can be received using TimeCuurent(), but what to do with it further, how to synchronise it is a question.

What does TimeCuurent() have to do with it?
 
Interesting:
What does TimeCuurent() have to do with this?

The help says:

In the OnTick() handler, this function will return the time of the incoming processed tick. In other cases (for example, call in OnInit(), OnDeinit(), OnTimer() handlers and so on) this is the time of arrival of the last quote for any symbol available in the "Market Watch" window, the same time that is shown in the title of this window.

 
Lizar:

The Synopsis reads:

In the OnTick() handler, this function will return the arrival time of the tick being processed. In other cases (for example, call in handlers OnInit(), OnDeinit(), OnTimer() and so on) this is the time of arrival of the last quote for any symbol available in the "Market Watch" window, the same time that is shown in the header of this window.

Yes, it is clear. only if we put it in OnTime we can update the time no more than once a minute, and if we put it in the current OnTick() we risk missing ticks on some of the pairs (in mults).

And what to do in the processing block is clear (for mults) - We get a new value, compare it with the existing one and draw conclusions whether there was a new tick or not.

But tell me, why do it in the Expert Advisor?

 
Lizar:

The Synopsis reads:

error. here is what the help says https://www.mql5.com/en/docs/basis/function/events

TheNewTick event is generated only for Expert Advisors when a new tick is received on the symbol, to whose chart the Expert Advisor is attached.

Once again. We are talking about an Expert Advisor that hangs on some symbol. It is triggered by the event

void OnTick() {}

If you are inside this function, and there are no ticks on this pair for more than an hour, you can't do anything within an hour. So far, the only way out is to do it in a timer, it starts regardless of the arrival of ticks on the instrument.

Документация по MQL5: Основы языка / Функции / Функции обработки событий
Документация по MQL5: Основы языка / Функции / Функции обработки событий
  • www.mql5.com
Основы языка / Функции / Функции обработки событий - Документация по MQL5
 
Interesting:

But if we put it in OnTime we can update the time no more than once a minute, and if we put it in the current OnTick() we risk missing ticks on some of the pairs (in mults).

That's the point, we can get the event, but we can't use it.
 
Prival:

error. Here's what the help says https://www.mql5.com/en/docs/basis/function/events.

Once again. We are talking about an Expert Advisor that hangs on some symbol. It is launched by the event

void OnTick() {}

If you are inside this function, and there are no ticks on this pair for more than an hour, you can't do anything within an hour. So far, the only way out is to do it in the timer, it is started regardless of the arrival of ticks on the instrument.

Yes, if we act within the framework of this https://www.mql5.com/en/docs/basis/function/events, I agree that the best option, as we see now, is OnTime. But it will only save us from the dependence on the arrival of ticks. And it will not save us from single-threadedness: while we are processing a signal for one instrument, we risk missing or delaying the processing of other instruments. This is especially true when "time of processing trade orders is from 2 to 7 seconds" + requotes.

I remembered about TimeCuurent() because Interesting started talking about real-time ticks for all pairs in the market review.

Документация по MQL5: Основы языка / Функции / Функции обработки событий
Документация по MQL5: Основы языка / Функции / Функции обработки событий
  • www.mql5.com
Основы языка / Функции / Функции обработки событий - Документация по MQL5
 
Good article, thank you.
 
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