订阅订单簿变更

Python API 包括三个用于处理 订单簿的函数。

bool market_book_add(symbol)

market_book_add 函数订阅以接收关于订单簿更改(指定交易品种)的事件。所需金融工具的名称在单个未命名的参数中指定。

该函数返回一个布尔成功指示。

该函数类似于 MarketBookAdd。在完成订单簿的处理后,应该通过调用 market_book_release 来取消订阅(见下文)。

tuple[] market_book_get(symbol)

market_book_get 函数请求指定交易品种的订单簿的当前内容。结果作为 BookInfo 记录的元组(数组)返回。每个条目都类似于 MqlBookInfo 结构体,从 Python 的角度来看,这是一个带有 "type"、"price"、"volume"、"volume_real" 字段的命名元组。如果出现错误,将返回 None 值。

注意,出于某种原因,在 Python 中,该字段被称为 volume_dbl,但在 MQL5 中,相应的字段被称为 volume_real

要使用此函数,你必须首先使用 market_book_add 函数订阅以接收订单簿事件。

该函数类似于 MarketBookGet。请注意,Python 脚本不能直接接收 OnBookEvent 事件,而应该以循环方式轮询订单薄的内容。

bool market_book_release(symbol)

market_book_release 函数取消对指定交易品种的订单簿更改事件的订阅。如果成功,该函数将返回 True。该函数类似于 MarketBookRelease

我们举一个简单的例子(参见 MQL5/Scripts/MQL5Book/Python/eurusdbook.py)。

import MetaTrader5 as mt5
import time               # connect a pack for the pause
   
# let's establish a connection to the MetaTrader 5 terminal
if not mt5.initialize():
   print("initialize() failed, error code =", mt5.last_error())
   mt5.shutdown()
   quit()
   
# subscribe to receive DOM updates for the EURUSD symbol
if mt5.market_book_add('EURUSD'):
   # run 10 times a loop to read data from the order book
   for i in range(10):
      # get the contents of the order book
      items = mt5.market_book_get('EURUSD')
      # display the entire order book in one line as is
      print(items)
      # now display each price level separately in the form of a dictionary, for clarity
      for it in items or []:
         print(it._asdict())
      # let's pause for 5 seconds before the next request for data from the order book
      time.sleep(5
   # unsubscribe to order book changes
   mt5.market_book_release('EURUSD')
else:
   print("mt5.market_book_add('EURUSD') failed, error code =", mt5.last_error())
   
# complete the connection to the MetaTrader 5 terminal
mt5.shutdown()

结果示例:

(BookInfo(type=1, price=1.20036, volume=250, volume_dbl=250.0), BookInfo(type=1, price=1.20029, volume=100, volume...

{'type': 1, 'price': 1.20036, 'volume': 250, 'volume_dbl': 250.0}

{'type': 1, 'price': 1.20029, 'volume': 100, 'volume_dbl': 100.0}

{'type': 1, 'price': 1.20028, 'volume': 50, 'volume_dbl': 50.0}

{'type': 1, 'price': 1.20026, 'volume': 36, 'volume_dbl': 36.0}

{'type': 2, 'price': 1.20023, 'volume': 36, 'volume_dbl': 36.0}

{'type': 2, 'price': 1.20022, 'volume': 50, 'volume_dbl': 50.0}

{'type': 2, 'price': 1.20021, 'volume': 100, 'volume_dbl': 100.0}

{'type': 2, 'price': 1.20014, 'volume': 250, 'volume_dbl': 250.0}

(BookInfo(type=1, price=1.20035, volume=250, volume_dbl=250.0), BookInfo(type=1, price=1.20029, volume=100, volume...

{'type': 1, 'price': 1.20035, 'volume': 250, 'volume_dbl': 250.0}

{'type': 1, 'price': 1.20029, 'volume': 100, 'volume_dbl': 100.0}

{'type': 1, 'price': 1.20027, 'volume': 50, 'volume_dbl': 50.0}

{'type': 1, 'price': 1.20025, 'volume': 36, 'volume_dbl': 36.0}

{'type': 2, 'price': 1.20023, 'volume': 36, 'volume_dbl': 36.0}

{'type': 2, 'price': 1.20022, 'volume': 50, 'volume_dbl': 50.0}

{'type': 2, 'price': 1.20021, 'volume': 100, 'volume_dbl': 100.0}

{'type': 2, 'price': 1.20014, 'volume': 250, 'volume_dbl': 250.0}

(BookInfo(type=1, price=1.20037, volume=250, volume_dbl=250.0), BookInfo(type=1, price=1.20031, volume=100, volume...

{'type': 1, 'price': 1.20037, 'volume': 250, 'volume_dbl': 250.0}

{'type': 1, 'price': 1.20031, 'volume': 100, 'volume_dbl': 100.0}

{'type': 1, 'price': 1.2003, 'volume': 50, 'volume_dbl': 50.0}

{'type': 1, 'price': 1.20028, 'volume': 36, 'volume_dbl': 36.0}

{'type': 2, 'price': 1.20025, 'volume': 36, 'volume_dbl': 36.0}

{'type': 2, 'price': 1.20023, 'volume': 50, 'volume_dbl': 50.0}

{'type': 2, 'price': 1.20022, 'volume': 100, 'volume_dbl': 100.0}

{'type': 2, 'price': 1.20016, 'volume': 250, 'volume_dbl': 250.0}

...