history_orders_get

取引履歴から注文を取得します(チケットまたはポジションでフィルターする機能あり)。3つの呼び出しオプションがあります。

時間間隔を指定して呼び出します。指定された時間間隔内にあるすべての注文を返します。

history_orders_get(
  date_from,           // 注文がリクエストされている最初の日
  date_to,             // 注文がリクエストされている最後の日
  group="GROUP"        // 注文を銘柄で選択するためのフィルター
  )

注文チケットを指定した呼び出し:指定されたチケットを持つすべての注文を返します。

history_orders_get(
  ticket=TICKET       // 注文チケット
)

ポジションチケットを指定した呼び出し:ORDER_POSITION_IDプロパティに指定されたポジションチケットを持つすべての注文を返します。

history_orders_get(
  position=POSITION   // ポジションチケット
)

パラメータ

date_from

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

date_to

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

group="GROUP"

[in] 必要な銘柄のグループを配置するためのフィルター。名前付きオプションパラメータ。グループが指定されている場合、関数は銘柄名の指定された基準を満たす注文のみを返します。

ticket=TICKET

[in] 受信される注文チケット。オプションパラメータ。指定されていない場合、フィルターは適用されません。

position=POSITION

[in] すべての注文が受信されるポジションのチケット(ORDER_POSITION_IDに格納)。オプションパラメータ。指定されていない場合、フィルターは適用されません。

戻り値

名前付きタプル構造(namedtuple)の形式で情報を返します。エラーの場合はNoneを返します。エラーに関する情報はlast_error()を使用して取得できます。

注意事項

この関数を使用すると、指定した履歴期間内のすべての注文を、HistoryOrdersTotalおよび HistoryOrdersSelect関数を呼び出すのに似た形で受け取ることができます。

groupパラメータには、複数のコンマ区切りの条件を含めることができます。条件は「*」を使用してマスクとして設定できます。論理否定記号「!」は除外に使用できます。すべての条件は順番に適用されます。つまり、グループに含める条件を最初に指定してから、除外条件を指定する必要があります。例えば、「group="*, !EUR"」では、全銘柄の取引を最初に選択してから、銘柄名に「EUR」が含まれる取引を後で除外します。

例:

from datetime import datetime
import MetaTrader5 as mt5
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()
history_orders=mt5.history_orders_get(from_date, to_date, group="*GBP*")
if history_orders==None:
  print("No history orders with group=\"*GBP*\", error code={}".format(mt5.last_error()))
elif len(history_orders)>0:
  print("history_orders_get({}, {}, group=\"*GBP*\")={}".format(from_date,to_date,len(history_orders)))
print()
 
# ポジションチケットごとにすべての履歴注文を表示する
position_id=530218319
position_history_orders=mt5.history_orders_get(position=position_id)
if position_history_orders==None:
  print("No orders with position #{}".format(position_id))
   print("error code =",mt5.last_error())
elif len(position_history_orders)>0:
  print("Total history orders on position #{}: {}".format(position_id,len(position_history_orders)))
   # 指定されたポジションチケットを持つすべての履歴注文を表示する
  for position_order in position_history_orders:        
      print(position_order)
   print()
  # pandas.DataFrameを使用してこれらの注文を表として表示する
   df=pd.DataFrame(list(position_history_orders),columns=position_history_orders[0]._asdict().keys())
   df.drop(['time_expiration','type_time','state','position_by_id','reason','volume_current','price_stoplimit','sl','tp'], axis=1, inplace=True)
   df['time_setup'] = pd.to_datetime(df['time_setup'], unit='s')
   df['time_done'] = pd.to_datetime(df['time_done'], unit='s')
  print(df)
 
# MetaTrader 5ターミナルへの接続をシャットダウンする
mt5.shutdown()
 
結果:
MetaTrader5 package author:  MetaQuotes Software Corp.
MetaTrader5 package version:  5.0.29
 
history_orders_get(2020-01-01 00:00:00, 2020-03-25 17:17:32.058795, group="*GBP*")=14
 
Total history orders on position #530218319: 2
TradeOrder(ticket=530218319, time_setup=1582282114, time_setup_msc=1582282114681, time_done=1582303777, time_done_msc=1582303777582, time_expiration=0, ...
TradeOrder(ticket=535548147, time_setup=1583176242, time_setup_msc=1583176242265, time_done=1583176242, time_done_msc=1583176242265, time_expiration=0, ...
 
     ticket          time_setup  time_setup_msc           time_done  time_done_msc  type  type_filling  magic  position_id  volume_initial  price_open  price_current  symbol comment external_id
0  530218319 2020-02-21 10:48:34   1582282114681 2020-02-21 16:49:37  1582303777582     2             2      0    530218319            0.01     0.97898        0.97863  USDCHF                    
1  535548147 2020-03-02 19:10:42   1583176242265 2020-03-02 19:10:42  1583176242265     1             0      0    530218319            0.01     0.95758        0.95758  USDCHF  

参照

history_deals_totalhistory_deals_get