- Installing Python and the MetaTrader5 package
- Overview of functions of the MetaTrader5 package for Python
- Connecting a Python script to the terminal and account
- Error checking: last_error
- Getting information about a trading account
- Getting information about the terminal
- Getting information about financial instruments
- Subscribing to order book changes
- Reading quotes
- Reading tick history
- Calculating margin requirements and evaluating profits
- Checking and sending a trade order
- Getting the number and list of active orders
- Getting the number and list of open positions
- Reading the history of orders and deals
Subscribing to order book changes
The Python API includes three functions for working with the order book.
bool market_book_add(symbol)
The market_book_add function subscribes to receive events about order book changes for the specified symbol. The name of the required financial instrument is indicated in a single unnamed parameter.
The function returns a boolean success indication.
The function is an analog of MarketBookAdd. After completing work with the order book, the subscription should be canceled by calling market_book_release (see further).
tuple[] market_book_get(symbol)
The market_book_get function requests the current contents of the order book for the specified symbol. The result is returned as a tuple (array) of BookInfo records. Each entry is an analog of the MqlBookInfo structure, and from the Python point of view, this is a named tuple with the fields "type", "price", "volume", "volume_real". In case of an error, the None value is returned.
Note that for some reason in Python, the field is called volume_dbl, although in MQL5 the corresponding field is called volume_real.
To work with this function, you must first subscribe to receive order book events using the market_book_add function.
The function is an analog of MarketBookGet. Please note that a Python script cannot receive OnBookEvent events directly and should poll the contents of the glass in a loop.
bool market_book_release(symbol)
The market_book_release function cancels the subscription for order book change events for the specified symbol. On success, the function returns True. The function is an analog of MarketBookRelease.
Let's take a simple example (see MQL5/Scripts/MQL5Book/Python/eurusdbook.py).
import MetaTrader5 as mt5
|
An example of the result:
(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} ... |