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年1月1日からの経過秒数として設定されます。名前なし必須パラメータが最初に指定されます。

date_to

[in] 注文がリクエストされている最後の日。「datetime」オブジェクトまたは1970年1月1日からの経過秒数として設定されます。名前なし必須パラメータが2番目に指定されます。

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) # 表示される列の数
pd.set_option('display.width', 1500)     # 表示する表の最高幅
# 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))
  # 取得したすべての取引を「そのまま」表示する
  for deal in deals:
      print("  ",deal)
  print()
  # 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)))
  # 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 package author:  MetaQuotes Software Corp.
MetaTrader5 package version:  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  

参照

history_deals_totalhistory_orders_get