history_deals_get
history_deals_get
获取可通过单号或持仓筛选的指定时间间隔内的交易历史中的交易。
调用指定时间间隔。返回指定时间间隔内的所有交易。
history_deals_get(
date_from, // 请求交易的开始日期
date_to, // 请求交易的结束日期
group="GROUP" // 为交易品种选择交易的过滤器
)调用指定订单单号。返回在DEAL_ORDER属性中具有指定订单单号的所有交易。
history_deals_get(
ticket=TICKET //订单单号
)调用指定持仓单号。返回在DEAL_POSITION_ID属性中具有指定持仓单号的所有交易。
history_deals_get(
position=POSITION //持仓单号
)参数
- date_from
[in] 请求订单的开始日期。通过’datetime’对象或根据1970.01.01以来过去的秒数设置。首先指定所需的未命名参数。
- date_to
[in] 请求订单的结束日期。通过’datetime’对象或根据1970.01.01以来过去的秒数设置。其次指定所需的未命名参数。
- group=“GROUP”
[in] 用于跑了一组必要交易品种的过滤器。可选的命名参数。如果指定了组,则函数只返回满足交易品种名称指定条件的交易。
- ticket=TICKET
[in] 应接收所有交易的订单单号(存储在DEAL_ORDER)。可选参数。如果没有指定,则不应用过滤器。
- position=POSITION
[in] 应接收所有交易的持仓单号(存储在DEAL_POSITION_ID)。可选参数。如果没有指定,则不应用过滤器。
返回值
以指定元组结构(namedtuple)的形式返回信息。错误情况下返回None。可使用last_error()获取错误信息。
注意
该函数允许在一个类似于HistoryDealsTotal和HistoryDealSelect的串联调用中接收指定时间段内的所有历史交易。
group参数可以根据交易品种对交易进行分类。’*‘可以用在字符串的开头和结尾。
group参数可以包含多个逗号分隔条件。条件可以使用’‘设置为掩码。逻辑否定字符’!‘可以用来表示排除条件。所有条件依序应用,这意味着应该先指定包含到组中的条件,然后再指定排除条件。例如,group=", !EUR"意味着应该先选择所有交易品种的交易,然后排除交易品种名称中包含"EUR"的交易。
例如:
import MetaTrader5 as mt5
from datetime import datetime
import pandas as pd
pd.set_option('display.max_columns', 500) # number of columns to be displayed
pd.set_option('display.width', 1500) # max table width to display
# 显示有关MetaTrader 5程序包的数据
print("MetaTrader5 package author: ",mt5.__author__)
print("MetaTrader5 package version: ",mt5.__version__)
print()
# 建立与MetaTrader 5程序端的连接
if not mt5.initialize():
print("initialize() failed, error code =",mt5.last_error())
quit()
# 获取历史中的交易数量
from_date=datetime(2020,1,1)
to_date=datetime.now()
# 获取指定时间间隔内且名称包含"GBP"的交易品种的交易
deals=mt5.history_deals_get(from_date, to_date, group="*GBP*")
if deals==None:
print("No deals with group=\"*USD*\", error code={}".format(mt5.last_error()))
elif len(deals)> 0:
print("history_deals_get({}, {}, group=\"*GBP*\")={}".format(from_date,to_date,len(deals)))
# 获取名称中既不包含"EUR"也不包含"GBP"的交易品种的交易
deals = mt5.history_deals_get(from_date, to_date, group="*,!*EUR*,!*GBP*")
if deals == None:
print("No deals, error code={}".format(mt5.last_error()))
elif len(deals) > 0:
print("history_deals_get(from_date, to_date, group=\"*,!*EUR*,!*GBP*\") =", len(deals))
# display all obtained deals 'as is'
for deal in deals:
print(" ",deal)
print()
# display these deals as a table using pandas.DataFrame
df=pd.DataFrame(list(deals),columns=deals[0]._asdict().keys())
df['time'] = pd.to_datetime(df['time'], unit='s')
print(df)
print("")
# 获取#530218319持仓相关的所有交易
position_id=530218319
position_deals = mt5.history_deals_get(position=position_id)
if position_deals == None:
print("No deals with position #{}".format(position_id))
print("error code =", mt5.last_error())
elif len(position_deals) > 0:
print("Deals with position id #{}: {}".format(position_id, len(position_deals)))
# display these deals as a table using pandas.DataFrame
df=pd.DataFrame(list(position_deals),columns=position_deals[0]._asdict().keys())
df['time'] = pd.to_datetime(df['time'], unit='s')
print(df)
# 断开与MetaTrader 5程序端的连接
mt5.shutdown()
结果:
MetaTrader5程序包作者:MetaQuotes Software Corp.
MetaTrader5程序包版本:5.0.29
history_deals_get(from_date, to_date, group="*GBP*") = 14
history_deals_get(from_date, to_date, group="*,!*EUR*,!*GBP*") = 7
TradeDeal(ticket=506966741, order=0, time=1582202125, time_msc=1582202125419, type=2, entry=0, magic=0, position_id=0, reason=0, volume=0.0, pri ...
TradeDeal(ticket=507962919, order=530218319, time=1582303777, time_msc=1582303777582, type=0, entry=0, magic=0, position_id=530218319, reason=0, ...
TradeDeal(ticket=513149059, order=535548147, time=1583176242, time_msc=1583176242265, type=1, entry=1, magic=0, position_id=530218319, reason=0, ...
TradeDeal(ticket=516943494, order=539349382, time=1583510003, time_msc=1583510003895, type=1, entry=0, magic=0, position_id=539349382, reason=0, ...
TradeDeal(ticket=516943915, order=539349802, time=1583510025, time_msc=1583510025054, type=0, entry=0, magic=0, position_id=539349802, reason=0, ...
TradeDeal(ticket=517139682, order=539557870, time=1583520201, time_msc=1583520201227, type=0, entry=1, magic=0, position_id=539349382, reason=0, ...
TradeDeal(ticket=517139716, order=539557909, time=1583520202, time_msc=1583520202971, type=1, entry=1, magic=0, position_id=539349802, reason=0, ...
ticket order time time_msc type entry magic position_id reason volume price commission swap profit fee symbol comment external_id
0 506966741 0 2020-02-20 12:35:25 1582202125419 2 0 0 0 0 0.00 0.00000 0.0 0.0 100000.00 0.0
1 507962919 530218319 2020-02-21 16:49:37 1582303777582 0 0 0 530218319 0 0.01 0.97898 0.0 0.0 0.00 0.0 USDCHF
2 513149059 535548147 2020-03-02 19:10:42 1583176242265 1 1 0 530218319 0 0.01 0.95758 0.0 0.0 -22.35 0.0 USDCHF
3 516943494 539349382 2020-03-06 15:53:23 1583510003895 1 0 0 539349382 0 0.10 0.93475 0.0 0.0 0.00 0.0 USDCHF
4 516943915 539349802 2020-03-06 15:53:45 1583510025054 0 0 0 539349802 0 0.10 0.66336 0.0 0.0 0.00 0.0 AUDUSD
5 517139682 539557870 2020-03-06 18:43:21 1583520201227 0 1 0 539349382 0 0.10 0.93751 0.0 0.0 -29.44 0.0 USDCHF
6 517139716 539557909 2020-03-06 18:43:22 1583520202971 1 1 0 539349802 0 0.10 0.66327 0.0 0.0 -0.90 0.0 AUDUSD
Deals with position id #530218319: 2
ticket order time time_msc type entry magic position_id reason volume price commission swap profit fee symbol comment external_id
0 507962919 530218319 2020-02-21 16:49:37 1582303777582 0 0 0 530218319 0 0.01 0.97898 0.0 0.0 0.00 0.0 USDCHF
1 513149059 535548147 2020-03-02 19:10:42 1583176242265 1 1 0 530218319 0 0.01 0.95758 0.0 0.0 -22.35 0.0 USDCHF