Discussion of article "MQL5 Cookbook: Handling BookEvent"

 

New article MQL5 Cookbook: Handling BookEvent has been published:

This article considers BookEvent - a Depth of Market event, and the principle of its processing. An MQL program, handling states of Depth of Market, serves as an example. It is written using the object-oriented approach. Results of handling are displayed on the screen as a panel and Depth of Market levels.

As is well known, the MetaTrader 5 trading terminal is a multi-market platform, that facilitates trading on Forex, stock markets, Futures and Contracts for Difference. According to the Freelance section stats, the number of traders trading not only on Forex market is growing.

In this article I would like to introduce novice MQL5 programmers to the BookEvent handling. This event is connected with Depth of Market—an instrument for trading stock assets and their derivatives. Forex traders, however, may find Depth of Market useful too. In ECN accounts, liquidity providers supply data on the orders, though only within their aggregator model. These accounts are becoming more popular.


1. BookEvent

According to the Documentation, this event is generated when Depth of Market status changes. Let us agree that BookEvent is a Depth of Market event.

Depth of Market is an array of orders, which differ in direction (sell and buy), price and volume. Prices in Depth of Market are close to the market ones and therefore are considered as the best.

Fig.1 Depth of Market in MetaTrader 5

Fig.1 Depth of Market in MetaTrader 5

In MetaTrader 5 an "order book" is named as the "Depth of Market" (Fig.1). Detailed information about Depth of Market can be found in the User Guide to the Client Terminal.


Author: Dennis Kirichenko

 

Thanks for this article.

A little bit about the error:

Все программы, работающие по данным стакана, по форме будут являться советниками, ведь обработчик события BookEvent есть только в советниках. Правда, можно сделать связку "советник-индикатор", где истинный индикатор сможет принимать данные от советника и обрабатывать состояние стакана.

This is not true, the indicator can handle OnBookEvent too.

 

angevoyageur:

A bit about the error:

This is not true, the indicator can handle OnBookEvent too.

Alain, thanks for the message. Yes, that's right, I'm wrong. It works in indicators too... I relied on the list of handlers when creating an indicator, among which there is no OnBookEvent().

And the most important thing is that the Documentation says:

The OnBookEvent() function is a handler of the BookEvent event. The BookEvent event is generated only for Expert Advisors when the state of the Depth of Market changes.


 
denkir :

Alain, thanks for the message. Yes, that's right, I'm wrong. It works in indicators too... I relied on the list of handlers when creating an indicator, among which there is no OnBookEvent().

And the most important thing is that the Documentation says:

Right, the documentation should be corrected. Do you write ServiceDesk about it?
 
angevoyageur:
Right, the documentation needs to be corrected. Are you writing ServiceDesk about this?
No, haven't written one yet... Will do and make changes to the article material...
 

Just for information, there is a little error in this article :

All programs working with Depth of Market data will have a form of an Expert Advisor, as only Expert Advisors feature the event handler of BookEvent. There is a possibility, however, to write an "Expert-Indicator" pair , where the indicator can receive data from the EA and process the Depth of Market state.

It's not true, indicators can also process BookEvent. This error is based on a documentation's error and should be corrected soon. The article's author and ServiceDesk have been contacted.

 

Good afternoon, thanks for the article, very useful!

such a question, let's say I process the BookEvent event, and my handler will delete, place orders, which in turn will again initiate this event..... then at the moment when the deadline for placing (deleting orders) and a new BookEvent event is formed again, my very initial procedure will be interrupted? because I noticed that the code execution does not reach the end.... not everything is executed ... I hope I've made the idea clear ))

 
thejobber:

Good afternoon, thanks for the article, very useful!

such a question, let's say I process the BookEvent event, and my handler will delete, place orders, which in turn will again initiate this event..... then at the moment when the deadline for placing (deleting orders) and a new BookEvent event is formed again, my very initial procedure will be interrupted? because I noticed that the code execution does not reach the end.... not everything is executed ... I hope I've made myself clear ))

Thanks for your opinion!

No, while one event is being processed, the program control is not automatically transferred to another handler when a new event is generated... you can check this with the Print() function ( and it is better to add a pause Sleep()).

There is such a concept as "event queue".

According to the Documentation:

The programme receives events only from the graph on which it is running. All events are processed one after another in the order of receipt. If there is already a NewTick event in the queue or this event is in the processing state, the new NewTick event is not put into the mql5-program queue. Similarly, if there is already a ChartEvent event in the mql5-program queue or this event is being processed, a new event of this type is not put into the queue. Timer events are processed according to the same scheme - if a Timer event is in the queue or is already processed, a new Timer event is not queued.

Event queues have a limited but sufficient size, so queue overflow is unlikely for a correctly written programme. If the queue overflows, new events are discarded without being queued.

 

Event queues have a limited but sufficient size, so queue overflow is unlikely for a correctly written programme. When the queue overflows, new events are discarded without being queued.

so my fears are not groundless after all )) I wrote a spreader for usd\rub futures to keep the best prices in the stack all the time.... then there will definitely be an overflow of the queue, because the bet is updated a lot of times per second, and this is without the fact that I also put something there, delete... and with me it will definitely not be in time..... and this is only demo... and on the real market there's a crazy bet))

thanks for directing my thoughts in the right direction )

 

Good afternoon. Very informative article. I have the following question.

There is a task of filtering deals executed on an instrument according to the type of orders they were executed with (market/limited).

How can I link OnBookEvent events and OnTick events to solve this task, i.e. how can I determine what types of orders were involved in the deal? As far as I understand, this task cannot be solved by means of OnBookEvent only.

 

Rubick:

...How to link OnBookEvent events and OnTick events to solve this problem, i.e. how to determine what types of orders were involved in the transaction? As far as I understand, such a task cannot be solved only by means of OnBookEvent.

Most likely you need not a glass, but a handler of market operations.