获取活动订单的数量和列表

Python API 提供了以下用于处理活动订单的函数。

int orders_total()

orders_total 函数返回活动订单的数量。

该函数类似于 订单总计

可以使用 orders_get 函数获得关于每个订单的详细信息,该函数有几个选项,能够按交易品种或订单号进行筛选。无论哪种方式,该函数都返回命名元组 TradeOrder 的数组(字段名称匹配 ENUM_ORDER_PROPERTY_enumerations 不带 "ORDER_" 前缀并简化为小写)。如果出现错误,则结果为 None

namedtuple[] orders_get()

namedtuple[] orders_get(symbol = <"SYMBOL">)

namedtuple[] orders_get(group = <"PATTERN">)

namedtuple[] orders_get(ticket = <TICKET>)

不含参数的 orders_get 函数返回所有交易品种的订单。

可选的命名参数 symbol 可以为订单选择指定特定的交易品种名称。

可选的命名参数 group 旨在使用通配符 '*'(替代任意数量的任何字符,包括模式中给定位置的零字符)和条件逻辑非字符 '!' 来指定搜索模式。筛选模板的操作原理在 获取有关金融工具的信息一节中说明。

如果指定了 ticket 参数,则搜索某个订单。

在一次函数调用中,您可以获取所有活动订单。它类似于 OrdersTotalOrderSelect,OrderGet 函数的组合使用。

在下面的例子 (MQL5/Scripts/MQL5Book/Python/ordersget.py) 中,我们使用不同的方式请求有关订单的信息。

import MetaTrader5 as mt5
import pandas as pd
pd.set_option('display.max_columns'500# how many columns to show
pd.set_option('display.width'1500)      # max. table width to display
   
# let's establish a connection to the MetaTrader 5 terminal
if not mt5.initialize(): 
   print("initialize() failed, error code =", mt5.last_error())
   quit()
   
# display information about active orders on the GBPUSD symbol
orders = mt5.orders_get(symbol = "GBPUSD")
if orders is None:
   print("No orders on GBPUSD, error code={}".format(mt5.last_error()))
else:
   print("Total orders on GBPUSD:"len(orders))
   # display all active orders
   for order in orders:
      print(order)
print()
   
# getting a list of orders on symbols whose names contain "*GBP*"
gbp_orders = mt5.orders_get(group="*GBP*")
if gbp_orders is None
   print("No orders with group=\"*GBP*\", error code={}".format(mt5.last_error()))
else
   print("orders_get(group=\"*GBP*\")={}".format(len(gbp_orders)))
   # display orders as a table using pandas.DataFrame
   df = pd.DataFrame(list(gbp_orders), columns = gbp_orders[0]._asdict().keys())
   df.drop(['time_done''time_done_msc''position_id''position_by_id',
   'reason''volume_initial''price_stoplimit'], axis = 1, inplace = True)
   df['time_setup'] = pd.to_datetime(df['time_setup'], unit = 's')
   print(df)
   
# complete the connection to the MetaTrader 5 terminal
mt5.shutdown()

结果示例如下:

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