- 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
Getting the number and list of active orders
The Python API provides the following functions for working with active orders.
int orders_total()
The orders_total function returns the number of active orders.
The function is an analog of Orders Total.
Detailed information about each order can be obtained using the orders_get function, which has several options with the ability to filter by symbol or ticket. Either way, the function returns the array of named tuples TradeOrder (field names match ENUM_ORDER_PROPERTY_enumerations without the "ORDER_" prefix and reduced to lowercase). In case of an error, the result is None.
namedtuple[] orders_get()
namedtuple[] orders_get(symbol = <"SYMBOL">)
namedtuple[] orders_get(group = <"PATTERN">)
namedtuple[] orders_get(ticket = <TICKET>)
The orders_get function without parameters returns orders for all symbols.
The optional named parameter symbol makes it possible to specify a specific symbol name for order selection.
The optional named parameter group is intended for specifying a search pattern using the wildcard character '*' (as a substitute for an arbitrary number of any characters, including zero characters in the given place of the pattern) and the condition logical negation character '!'. The filter template operation principle was described in the section Getting information about financial instruments.
If the ticket parameter is specified, a certain order is searched.
In one function call, you can get all active orders. It is an analog of the combined use of OrdersTotal, OrderSelect, and OrderGet functions.
In the next example (MQL5/Scripts/MQL5Book/Python/ordersget.py), we request information about orders using different ways.
import MetaTrader5 as mt5
|
The sample result is below:
Total orders on GBPUSD: 2 TradeOrder(ticket=554733548, time_setup=1585153667, time_setup_msc=1585153667718, time_done=0, time_done_msc=0, time_expiration=0, type=3, type_time=0, ... TradeOrder(ticket=554733621, time_setup=1585153671, time_setup_msc=1585153671419, time_done=0, time_done_msc=0, time_expiration=0, type=2, type_time=0, ...
orders_get(group="*GBP*")=4 ticket time_setup time_setup_msc type ... volume_current price_open sl tp price_current symbol comment external_id 0 554733548 2020-03-25 16:27:47 1585153667718 3 ... 0.2 1.25379 0.0 0.0 1.16803 GBPUSD 1 554733621 2020-03-25 16:27:51 1585153671419 2 ... 0.2 1.14370 0.0 0.0 1.16815 GBPUSD 2 554746664 2020-03-25 16:38:14 1585154294401 3 ... 0.2 0.93851 0.0 0.0 0.92428 EURGBP 3 554746710 2020-03-25 16:38:17 1585154297022 2 ... 0.2 0.90527 0.0 0.0 0.92449 EURGBP |