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    // 포지션 티켓
)

Parameters

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에 저장됨) 티켓. 선택적 파라미터. 지정하지 않으면 필터가 적용되지 않습니다.

반환 값

명명된 튜플 구조(이름 지정된 튜플)의 형식으로 정보를 반환합니다. 오류가 발생하면 None을 반환합니다. 오류에 대한 정보는 last_error()를 사용하여 얻을 수 있습니다.

참고

이 함수를 사용하면 HistoryDealsTotalHistoryDealSelect 탠덤과 유사한 단일 요청에서 지정된 기간 내의 모든 거래 이력을 받을 수 있습니다.

그룹 매개 변수를 사용해 심볼을 기준으로 거래를 정렬할 수 있습니다. '*'은 문자열의 시작과 끝에 사용될 수 있습니다

그룹 매개 변수는 쉼표로 구분된 여러 조건을 포함할 수 있습니다. 조건은 '*'를 사용하여 마스크로 설정할 수 있습니다. 논리적 부정 심볼 '!'을(를) 제외에 사용할 수 있습니다. 모든 조건은 순차적으로 적용되며, 이는 그룹에 포함되는 조건 다음에 배제 조건이 먼저 지정되어야 함을 의미합니다. 예를 들면, 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 패키지 작성자: ",mt5.__author__)
print("MetaTrader5 패키지 버전: ",mt5.__version__)
print()
# MetaTrader 5 터미널과의 연결 설정
if not mt5.initialize():
    print("initialize() 실패, 오류 코드 =",mt5.last_error())
    quit()
 
# 내역에서 딜 수 가져오기
from_date=datetime(2020,1,1)
to_date=datetime.now()
# 이름에 지정된 간격 내에 "GBP"가 포함된 심볼에 대한 딜 가져오기
deals=mt5.history_deals_get(from_dateto_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("딜이 없습니다, 에러 코드={}".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 패키지 작성자:  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   

더 보기

history_deals_total, history_orders_get