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

 

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.

 
Is it possible create a book event for forex market to see the pending orders? Naturally for a single liquidity provider.
 
BlackBart:
Is it possible create a book event for forex market to see the pending orders? Naturally for a single liquidity provider.
You need a broker which provides these informations.
 

"It is obvious that out of all sell orders, order #6 had the largest volume with the price of 7700 Rub and volume of 1011 lots. Order #39 had the largest volume out of all buy orders with the price of 7653 Rub and volume of 534 lots."

It is not order #6, neither order #39. These are levels, which can be accessed by

last_bookArray[6]

or

last_bookArray[39]

MqlBookArrays are comprised of sell offers from [0] to [ArraySize(bookArray)/2-1], and buy offers from [ArraySize(bookArray)/2] to [ArraySize(bookArray)-1]. As far as I know, books have 2n levels. And, if I am not wrong, levels start with index 0 (zero), so the mathematical mode (the highest volume, the most frequent sell offer) index is 5 (volume 1011, level/price 7700), whereas the buy offer mode is 38 (volume 534, level/price of 7653).

The buy offer mode could be taken as support. And the sell offer mode could be resistance.

I wish I could know how could I increase the number of levels...

 
Hi Denis ;

I have tried to use your indicator, the BookEventProcessor2.mq5., but when trying to compile it within the mql5 meta Editor, it gives me the following error, you could be kind enough to solve those little problems to see how it works in these times. Could you also tell me if it can also include the limit orders of sale and purchase at the same time but with another color.

regards,

Michael
Files:
 
mzapata6724:
Hi Denis ;

I have tried to use your indicator, the BookEventProcessor2.mq5., but when trying to compile it within the mql5 meta Editor, it gives me the following error, you could be kind enough to solve those little problems to see how it works in these times. Could you also tell me if it can also include the limit orders of sale and purchase at the same time but with another color.

regards,

Michael

Michael, thanks for your questions. First of all, BookEventProcessor2 is not an indicator - it is an Expert Advisor. All you need is to place the source and header files into one folder. Have a look at the attached pictures below.


If you'd like to change colors quickly go to the method CBookBarsPanel::Init() :

//+------------------------------------------------------------------+
//| Initialization                                                   |
//+------------------------------------------------------------------+
bool CBookBarsPanel::Init(const uint _width,const uint _height)
  {
//--- panel size
   this.m_width=_width;
   this.m_height=_height;

//--- allocate memory
   if(this.m_obj_arr.Reserve(this.m_arr_size))
     {
      //--- the memory management flag 
      this.m_obj_arr.FreeMode(true);
      //--- panel label
      CChartObjectRectLabel *ptr_rect_label=new CChartObjectRectLabel;
      if(CheckPointer(ptr_rect_label)==POINTER_DYNAMIC)
         if(ptr_rect_label.Create(0,"Panel",0,15,15,this.m_width,this.m_height))
            if(ptr_rect_label.BorderType(BORDER_RAISED))
               if(this.m_obj_arr.Add(ptr_rect_label))
                 {
                  //--- bar label 
                  uint curr_x=this.m_width/5;
                  uint curr_y=35;
                  uint mid_idx=this.m_arr_size/2-1;
                  for(uint idx=0;idx<this.m_arr_size;idx++)
                    {
                     color rec_color=(idx<(mid_idx+1))?clrRed:clrGreen;
                     //---
                     CBookRecord *ptr_record=new CBookRecord;
                     if(CheckPointer(ptr_record)==POINTER_DYNAMIC)
                        if(ptr_record.Create(IntegerToString(idx+1),rec_color,curr_x+15,
                           curr_y,curr_x*3,10))
                           if(this.m_obj_arr.Add(ptr_record))
                              curr_y+=(idx==mid_idx)?24:13;
                    }
                 }
     }

//---
   return this.m_obj_arr.Total()==(this.m_arr_size+1);
  }

Or you may slightly change the method parameters like this:

//+------------------------------------------------------------------+
//| Initialization                                                   |
//+------------------------------------------------------------------+
bool CBookBarsPanel::Init(const uint _width,const uint _height, const color _buy_clr, const color _sell_clr)
  {
//--- panel size
   this.m_width=_width;
   this.m_height=_height;

//--- allocate memory
   if(this.m_obj_arr.Reserve(this.m_arr_size))
     {
      //--- the memory management flag 
      this.m_obj_arr.FreeMode(true);
      //--- panel label
      CChartObjectRectLabel *ptr_rect_label=new CChartObjectRectLabel;
      if(CheckPointer(ptr_rect_label)==POINTER_DYNAMIC)
         if(ptr_rect_label.Create(0,"Panel",0,15,15,this.m_width,this.m_height))
            if(ptr_rect_label.BorderType(BORDER_RAISED))
               if(this.m_obj_arr.Add(ptr_rect_label))
                 {
                  //--- bar label 
                  uint curr_x=this.m_width/5;
                  uint curr_y=35;
                  uint mid_idx=this.m_arr_size/2-1;
                  for(uint idx=0;idx<this.m_arr_size;idx++)
                    {
                     color rec_color=(idx<(mid_idx+1))?_sell_clr:_buy_clr;
                     //---
                     CBookRecord *ptr_record=new CBookRecord;
                     if(CheckPointer(ptr_record)==POINTER_DYNAMIC)
                        if(ptr_record.Create(IntegerToString(idx+1),rec_color,curr_x+15,
                           curr_y,curr_x*3,10))
                           if(this.m_obj_arr.Add(ptr_record))
                              curr_y+=(idx==mid_idx)?24:13;
                    }
                 }
     }

//---
   return this.m_obj_arr.Total()==(this.m_arr_size+1);
  }
 

Hi Dennis


Thanks for soon reply, the expert advisor now is working pretty good.  But I was trying to talk about this reference:

When sending a trade request using the OrderSend() function, some operations require the indication of the order type. The order type is specified in the type field of the special structure MqlTradeRequest, and can accept values of the ENUM_ORDER_TYPE enumeration.

ENUM_ORDER_TYPE

Identifier

Description

ORDER_TYPE_BUY

Market Buy order

ORDER_TYPE_SELL

Market Sell order

ORDER_TYPE_BUY_LIMIT

Buy Limit pending order

ORDER_TYPE_SELL_LIMIT

Sell Limit pending order

ORDER_TYPE_BUY_STOP

Buy Stop pending order

ORDER_TYPE_SELL_STOP

Sell Stop pending order

ORDER_TYPE_BUY_STOP_LIMIT

Upon reaching the order price, a pending Buy Limit order is placed at the StopLimit price

ORDER_TYPE_SELL_STOP_LIMIT

Upon reaching the order price, a pending Sell Limit order is placed at the StopLimit price

ORDER_TYPE_CLOSE_BY

Order to close a position by an opposite one


I can see into the terminal of Mql5, that the exper advisor show ORDER_TYPE_BUY AND  ORDER_TYPE_BUY, but my question is you can show the transaccion in the bookevent about and change those color, because I know how to see the buy order, but wher is the big boss putting there money , with Iceberg and hug pending order at certain price level.


is that possible ???


best regards Michael. 

RDER_TYPE_SELL_LIMIT

Sell Limit pending order

ORDER_TYPE_BUY_STOP

Buy Stop pending order

Documentation on MQL5: Constants, Enumerations and Structures / Data Structures / Trade Request Structure
Documentation on MQL5: Constants, Enumerations and Structures / Data Structures / Trade Request Structure
  • www.mql5.com
Trade Request Structure - Data Structures - Constants, Enumerations and Structures - MQL5 Reference - Reference on algorithmic/automated trading language for MetaTrader 5
 
mzapata6724:

...I can see into the terminal of Mql5, that the exper advisor show ORDER_TYPE_BUY AND  ORDER_TYPE_BUY, but my question is you can show the transaccion in the bookevent about and change those color, because I know how to see the buy order, but wher is the big boss putting there money , with Iceberg and hug pending order at certain price level.

is that possible ???

Possible what? Can't grasp your idea.

 

Hello Denis!


Quick question. Can we access all types of pending orders not only buy below market price but also sell below it and vice versa, buy above market price. In other words Im trying to map out all forms of liquidity orders in the book. Similar like Oanda broker used to have in the FX lab tools.


Thanks a lot

 

good day friend
i saw your block
I couldn't copy the file


I await your prompt response, thank you


Reason: