读取订单和交易的历史
也可以使用 Python 脚本处理账户历史中的订单和交易。为了达到这些目的,提供了函数 history_orders_total、history_orders_get、history_deals_total 和 history_deals_get。
int history_orders_total(date_from, date_to)
history_orders_total函数返回指定时间间隔内交易历史中的订单数量。每个参数都由 datetime 对象设置,或者设置为自 1970.01.01 以来经历的秒数。
该函数类似于 HistoryOrdersTotal。
history_orders_get 函数有多个版本,支持按交易品种名称、订单号或位置 ID 中的子字符串进行订单筛选。所有变量都返回一个命名元组 TradeOrder 的数组(字段名称匹配 ENUM_ORDER_PROPERTY_enumerations 不带 "ORDER_" 前缀并且是小写的)。如果没有匹配的订单,数组将为空。如果出现错误,该函数将返回 None。
namedtuple[] history_orders_get(date_from, date_to, group = <"PATTERN">)
namedtuple[] history_orders_get(ticket = <ORDER_TICKET>)
namedtuple[] history_orders_get(position = <POSITION_ID>)
第一个版本选择指定时间范围内的订单(类似于 history_orders_total)。在可选的命名参数 group 中,你可以为交易品种名称的子字符串指定搜索模式(你可以在其中使用通配符 '*' 和非 '!',参见 获取有关金融工具的信息一节)。
第二个版本旨在按订单号搜索特定订单。
最后一个版本按仓位 ID(ORDER_POSITION_ID 特性)选择订单。
任一选项都相当于调用多个 MQL5 函数:HistoryOrdersTotal、 HistoryOrderSelect 和 HistoryOrderGet 函数。
这里是一个 historyordersget.py 脚本的例子,让我们看看如何获取不同条件下的历史订单的数量和列表。
from datetime import datetime
|
脚本的结果(用缩写给出):
history_orders_get(2022-09-01 00:00:00, 2022-09-26 21:50:04, group="*GBP*")=15 of total 44
Total history orders on position #0: 14 TradeOrder(ticket=1437318706, time_setup=1661348065, time_setup_msc=1661348065049, time_done=1661348083, time_done_msc=1661348083632, time_expiration=0, type=2, type_time=0, type_filling=2, state=2, magic=0, position_id=0, position_by_id=0, reason=3, volume_initial=0.01, volume_current=0.01, price_open=0.99301, sl=0.0, tp=0.0, price_current=0.99311, price_stoplimit=0.0, symbol='EURUSD', comment='', external_id='') TradeOrder(ticket=1437331579, time_setup=1661348545, time_setup_msc=1661348545750, time_done=1661348551, time_done_msc=1661348551354, time_expiration=0, type=2, type_time=0, type_filling=2, state=2, magic=0, position_id=0, position_by_id=0, reason=3, volume_initial=0.01, volume_current=0.01, price_open=0.99281, sl=0.0, tp=0.0, price_current=0.99284, price_stoplimit=0.0, symbol='EURUSD', comment='', external_id='') TradeOrder(ticket=1437331739, time_setup=1661348553, time_setup_msc=1661348553935, time_done=1661348563, time_done_msc=1661348563412, time_expiration=0, type=2, type_time=0, type_filling=2, state=2, magic=0, position_id=0, position_by_id=0, reason=3, volume_initial=0.01, volume_current=0.01, price_open=0.99285, sl=0.0, tp=0.0, price_current=0.99286, price_stoplimit=0.0, symbol='EURUSD', comment='', external_id='') ...
ticket time_setup time_done type ..._initial price_open price_current symbol comment 0 1437318706 2022-08-24 13:34:25 2022-08-24 13:34:43 2 0.01 0.99301 0.99311 EURUSD 1 1437331579 2022-08-24 13:42:25 2022-08-24 13:42:31 2 0.01 0.99281 0.99284 EURUSD 2 1437331739 2022-08-24 13:42:33 2022-08-24 13:42:43 2 0.01 0.99285 0.99286 EURUSD ... |
我们可以看到,在 9 月份,只有 44 个订单,其中 15 个订单包括英镑货币(由于未平仓仓位,这个数字是一个奇数)。历史记录包含 14 个取消的订单。
int history_deals_total(date_from, date_to)
history_deals_total 函数返回指定期间的历史交易数。
该函数类似于 HistoryDealsTotal。
history_deals_get函数有多种形式,用于选择能够按订单订单号或仓位 ID 筛选的交易。所有形式的函数都返回一个命名元组 TradeDeal的数组,其中的字段反映 ENUM_DEAL_PROPERTY_enumerations 中的特性(前缀 "DEAL_" 已从字段名中删除,并应用小写字母)。如果出错,则返回 None。
namedtuple[] history_deals_get(date_from, date_to, group = <"PATTERN">)
namedtuple[] history_deals_get(ticket = <ORDER_TICKET>)
namedtuple[] history_deals_get(position = <POSITION_ID>)
该函数的第一种形式类似于使用 history_orders_get请求历史订单。
第二种形式允许按订单号选择由特定订单生成的交易(DEAL_ORDER 特性)。
最后,第三个表单请求已经形成仓位的交易,该仓位具有给定 ID(DEAL_POSITION_ID 特性)。
该函数允许你在一次调用中获取所有事务及其特性,这类似于 HistoryDealsTotal、 HistoryDealSelect和 HistoryDealGet函数。
下面是测试脚本historydealsget.py的主要部分。
# set the time range
|
结果示例:
history_deals_get(from_date, to_date, group="*,!*EUR*,!*GBP*") = 12 TradeDeal(ticket=1109160642, order=0, time=1632188460, time_msc=1632188460852, type=2, entry=0, magic=0, position_id=0, reason=0, volume=0.0, price=0.0, commission=0.0, swap=0.0, profit=10000.0, fee=0.0, symbol='', comment='', external_id='') TradeDeal(ticket=1250629232, order=1268074569, time=1645709385, time_msc=1645709385815, type=0, entry=0, magic=0, position_id=1268074569, reason=0, volume=0.01, price=1970.98, commission=0.0, swap=0.0, profit=0.0, fee=0.0, symbol='XAUUSD', comment='', external_id='') TradeDeal(ticket=1250639814, order=1268085019, time=1645709950, time_msc=1645709950618, type=1, entry=1, magic=0, position_id=1268074569, reason=0, volume=0.01, price=1970.09, commission=0.0, swap=0.0, profit=-0.89, fee=0.0, symbol='XAUUSD', comment='', external_id='') TradeDeal(ticket=1250639928, order=1268085129, time=1645709955, time_msc=1645709955502, type=1, entry=0, magic=0, position_id=1268085129, reason=0, volume=0.01, price=1969.98, commission=0.0, swap=0.0, profit=0.0, fee=0.0, symbol='XAUUSD', comment='', external_id='') TradeDeal(ticket=1250640111, order=1268085315, time=1645709965, time_msc=1645709965148, type=0, entry=1, magic=0, position_id=1268085129, reason=0, volume=0.01, price=1970.17, commission=0.0, swap=0.0, profit=-0.19, fee=0.0, symbol='XAUUSD', comment='', external_id='') TradeDeal(ticket=1250640309, order=1268085512, time=1645709973, time_msc=1645709973623, type=1, entry=0, magic=0, position_id=1268085512, reason=0, volume=0.1, price=1970.09, commission=0.0, swap=0.0, profit=0.0, fee=0.0, symbol='XAUUSD', comment='', external_id='') TradeDeal(ticket=1250640400, order=1268085611, time=1645709978, time_msc=1645709978701, type=0, entry=1, magic=0, position_id=1268085512, reason=0, volume=0.1, price=1970.22, commission=0.0, swap=0.0, profit=-1.3, fee=0.0, symbol='XAUUSD', comment='', external_id='') TradeDeal(ticket=1250640616, order=1268085826, time=1645709988, time_msc=1645709988277, type=1, entry=0, magic=0, position_id=1268085826, reason=0, volume=1.1, price=1969.95, commission=0.0, swap=0.0, profit=0.0, fee=0.0, symbol='XAUUSD', comment='', external_id='') TradeDeal(ticket=1250640810, order=1268086019, time=1645709996, time_msc=1645709996990, type=0, entry=1, magic=0, position_id=1268085826, reason=0, volume=1.1, price=1969.88, commission=0.0, swap=0.0, profit=7.7, fee=0.0, symbol='XAUUSD', comment='', external_id='') TradeDeal(ticket=1445796125, order=1468026008, time=1664199450, time_msc=1664199450488, type=0, entry=0, magic=234000, position_id=1468026008, reason=3, volume=0.1, price=144.132, commission=0.0, swap=0.0, profit=0.0, fee=0.0, symbol='USDJPY', comment='python script op', external_id='') TradeDeal(ticket=1445796155, order=1468026041, time=1664199452, time_msc=1664199452567, type=1, entry=1, magic=234000, position_id=1468026008, reason=3, volume=0.1, price=144.124, commission=0.0, swap=0.0, profit=-0.56, fee=0.0, symbol='USDJPY', comment='python script cl', external_id='') TradeDeal(ticket=1446217804, order=1468454363, time=1664217233, time_msc=1664217233239, type=1, entry=0, magic=0, position_id=1468454363, reason=0, volume=0.01, price=0.99145, commission=0.0, swap=0.0, profit=0.0, fee=0.0, symbol='USDCHF', comment='', external_id='')
ticket order time t… e… … position_id volume price profit symbol comment external_id 0 1109160642 0 2021-09-21 01:41:00 2 0 0 0.00 0.00000 10000.00 1 1250629232 1268074569 2022-02-24 13:29:45 0 0 1268074569 0.01 1970.98000 0.00 XAUUSD 2 1250639814 1268085019 2022-02-24 13:39:10 1 1 1268074569 0.01 1970.09000 -0.89 XAUUSD 3 1250639928 1268085129 2022-02-24 13:39:15 1 0 1268085129 0.01 1969.98000 0.00 XAUUSD 4 1250640111 1268085315 2022-02-24 13:39:25 0 1 1268085129 0.01 1970.17000 -0.19 XAUUSD 5 1250640309 1268085512 2022-02-24 13:39:33 1 0 1268085512 0.10 1970.09000 0.00 XAUUSD 6 1250640400 1268085611 2022-02-24 13:39:38 0 1 1268085512 0.10 1970.22000 -1.30 XAUUSD 7 1250640616 1268085826 2022-02-24 13:39:48 1 0 1268085826 1.10 1969.95000 0.00 XAUUSD 8 1250640810 1268086019 2022-02-24 13:39:56 0 1 1268085826 1.10 1969.88000 7.70 XAUUSD 9 1445796125 1468026008 2022-09-26 13:37:30 0 0 1468026008 0.10 144.13200 0.00 USDJPY python script op 10 1445796155 1468026041 2022-09-26 13:37:32 1 1 1468026008 0.10 144.12400 -0.56 USDJPY python script cl 11 1446217804 1468454363 2022-09-26 18:33:53 1 0 1468454363 0.01 0.99145 0.00 USDCHF
|