This is very short article with very little code. Let see in next part if it makes sense have part 1, 2 and so on.
SYMBOL_TICKS_BOOKDEPTH gives the maximal number of requests shown in Depth of Market. Is incorrect that this property gives the same result as counting the number of levels in the DOM. It gives the maximal number not precise number.
You can very that using this script:
//+------------------------------------------------------------------+ //| TestOnderBook.mq5 | //| Copyright 2025, Samuel Manoel De Souza | //| https://www.mql5.com/en/users/samuelmnl | //+------------------------------------------------------------------+ #property copyright "Copyright 2025, Samuel Manoel De Souza" #property link "https://www.mql5.com/en/users/samuelmnl" #property version "1.00" //+------------------------------------------------------------------+ //| Script program start function | //+------------------------------------------------------------------+ int OnInit(void) { MarketBookAdd(_Symbol); return(INIT_SUCCEEDED); } //+------------------------------------------------------------------+ //| | //+------------------------------------------------------------------+ void OnDeinit(const int reason) { MarketBookRelease(_Symbol); } //+------------------------------------------------------------------+ //| | //+------------------------------------------------------------------+ void OnTick(void) { } //+------------------------------------------------------------------+ //| | //+------------------------------------------------------------------+ void OnBookEvent(const string& symbol) { double tick_size = SymbolInfoDouble(_Symbol, SYMBOL_TRADE_TICK_SIZE); MqlBookInfo book[]; MarketBookGet(_Symbol, book); int total = ArraySize(book); if(total == 0) { Print("there is no order available on the book"); ExpertRemove(); return; } int buy_levels = 0, sell_levels = 0; int buy_gaps = 0, sell_gaps = 0, gaps = 0; for(int i = 0; i < total; i++) { Print("price: ", book[i].price, ", volume: ", book[i].volume, ", type: ", EnumToString(book[i].type)); buy_levels += book[i].type == BOOK_TYPE_BUY ? 1 : 0; sell_levels += book[i].type == BOOK_TYPE_SELL ? 1 : 0; if(i > 0) { bool is_gap = fabs(book[i].price - book[i - 1].price) >= 2 * tick_size; gaps += is_gap ? 1 : 0; buy_gaps += is_gap && book[i].type == book[i - 1].type && book[i].type == BOOK_TYPE_BUY ? 1 : 0; sell_gaps += is_gap && book[i].type == book[i - 1].type && book[i].type == BOOK_TYPE_SELL ? 1 : 0; } } Print("max levels: ", SymbolInfoInteger(_Symbol, SYMBOL_TICKS_BOOKDEPTH)); Print("levels: ", total); Print("buy levels: ", buy_levels); Print("sell levels: ", sell_levels); Print("gaps: ", gaps); Print("buy gaps: ", buy_gaps); Print("sell gap: ", sell_gaps); ExpertRemove(); } //+------------------------------------------------------------------+
This is a very short article with very little code. We'll see in the next part if it makes sense to have part 1, 2 and so on.
SYMBOL_TICKS_BOOKDEPTH gives the maximum number of requests displayed in the Depth of Market. It is incorrect that this property gives the same result as counting the number of levels in the DOM. It gives a maximum number, not an exact number.
You can do this with this script:
The article is super! I also wanted to write a TS on the stack - more precisely with the use of the quotes stack!

- Free trading apps
- Over 8,000 signals for copying
- Economic news for exploring financial markets
You agree to website policy and terms of use
Check out the new article: Developing a Trading System Based on the Order Book (Part I): Indicator.
Depth of Market is undoubtedly a very important element for executing fast trades, especially in High Frequency Trading (HFT) algorithms. In this series of articles, we will look at this type of trading events that can be obtained through a broker on many tradable symbols. We will start with an indicator, where you can customize the color palette, position and size of the histogram displayed directly on the chart. We will also look at how to generate BookEvent events to test the indicator under certain conditions. Other possible topics for future articles include how to store price distribution data and how to use it in a strategy tester.
Let's recap what Depth of Market is. It is a series of pending limit orders. These orders represent the trading intentions of market participants and often do not result in an actual transaction. This is because traders have the ability to cancel their previously placed orders for various reasons. These may include changes in market conditions and the resulting loss of interest in executing the order at the previously specified price and quantity.
The value returned by the function SymbolInfoInteger(_Symbol, SYMBOL_TICKS_BOOKDEPTH) corresponds precisely to the depth of the order book and represents half of the array that will be populated with price levels to be analyzed. Half of this array is allocated for the number of limit sell orders, while the other half is for limit buy orders that have been placed. According to the documentation, for assets that do not have an order queue, the value of this property is zero. An example of this can be seen in the figure below, which shows the order book with the depth of 10, showing all available price levels.
It should be noted that depth can be obtained from the symbol and not necessarily from the market depth. Using the SymbolInfoInteger function is sufficient to retrieve the property value, without resorting to the OnBookEvent handler or related functions such as MarketBookAdd. Of course, we could arrive at the same result by counting the number of elements in the MqlBookInfo array that the OnBookEvent handler populates, as we will explore in more detail later.
Author: Daniel Santos