HistorySelectByPosition

지정된 포지션 식별자가 있는 거래 및 주문 기록을 검색합니다.

bool  HistorySelectByPosition(
   ulong   position_id     // 포지션 식별자 - POSITION_IDENTIFIER
   );

매개 변수

position_id

[in]  실행된 모든 주문 및 모든 딜로 설정된 포지션 식별자.

반환값

성공하면 true를 반환하고, 그렇지 않으면 false를 반환합니다.

참고

거래 내역의 주문을 "도구 상자" 표시줄의 "거래" 탭에 표시되는 현재 보류 주문과 혼동하지 마십시오. 취소되었거나 거래로 이어진 주문 목록은 클라이언트 터미널의 "도구 상자"에 있는 "내역" 탭에서 볼 수 있습니다.

HistorySelectByPosition()은 mql5 프로그램에서 주문 목록과 지정된 포지션 식별자의 거래 목록을 만들어 해당 함수를 사용하여 목록의 요소를 추가로 참조합니다. 거래 목록의 크기를 알기 위해 함수 HistoryDealsTotal()를 사용하고, 내역에서의 주문 목록의 크기는 HistoryOrdersTotal()를 사용하여 구할 수 있습니다. 주문 목록의 요소를 살펴보려면, HistoryOrderGetTicket()HistoryDealGetTicket()에 대해 사용하십시오.

HistoryOrderSelect()를 사용한 후에는 mql5 프로그램에서 사용할 수 있는 기록 주문 목록이 재설정되고 티켓으로 주문 검색에 성공한 경우 발견된 주문으로 다시 채워집니다. 마찬가지로 mql5 프로그램에서 사용할 수 있는 거래 목록을 참조합니다 - 이 목록은 함수 HistoryDealSelect()에 의해 재설정되며, 티켓 번호에 의해 거래가 성공적으로 검색되면 다시 작성됩니다.

예:

//+------------------------------------------------------------------+
//| 스크립트 프로그램 시작 함수                                          |
//+------------------------------------------------------------------+
void OnStart()
  {
   long  pos_id_array[];         // 포지션 ID를 저장하기 위한 배열
 
//--- 전체 히스토리 요청
   if(!HistorySelect(0TimeCurrent()))
     {
      Print("HistorySelect() failed. Error "GetLastError());
      return;
     }
     
//--- 배열의 펜딩 주문에서만 모든 포지션 ID를 수집
   int total=HistoryOrdersTotal();
   for(int i=0i<totali++)
     {
      ulong ticket=HistoryOrderGetTicket(i);
      if(ticket==0)
         continue;
      ENUM_ORDER_TYPE type=(ENUM_ORDER_TYPE)HistoryOrderGetInteger(ticketORDER_TYPE);
      long pos_id=HistoryOrderGetInteger(ticketORDER_POSITION_ID);
      if(type<=ORDER_TYPE_SELL || pos_id==0)
         continue;
      
      int size=ArraySize(pos_id_array);
      if(ArrayResize(pos_id_arraysize+1)==size+1)
         pos_id_array[size]=pos_id;
     }
   
//--- 배열의 포지션 ID의 리스트로
   total=ArraySize(pos_id_array);
   for(int i=0i<totali++)
     {
      //--- 헤더와 포지션 주문 및 거래 리스트를 인쇄
      long position_id=pos_id_array[i];
      Print("List of orders and deals for position with ID: "position_id);
      HistorySelectByPositionProcess(position_id);
     }
   /*
   결과:
   List of orders and deals for position with ID1819629924
     [0Order Sell Limit #1819629924
     [1Order Buy #1819633194
     [0Entry In Deal Sell #1794972472
     [1Entry Out Deal Buy #1794975589
   List of orders and deals for position with ID1841753970
     [0Order Sell Stop #1841753970
     [1Order Buy #1842322160
     [0Entry In Deal Sell #1817242142
     [1Entry Out Deal Buy #1817765341
   */
  }
//+------------------------------------------------------------------+
//| 포지션 ID별로 주문 및 거래 내역을 선택하고                            |
//| 저널의 해당 위치에 대한 주문 및 거래 목록을 인쇄|                      |
//+------------------------------------------------------------------+
bool HistorySelectByPositionProcess(const long position_id)
  {
//--- 지정된 포지션 ID를 가진 거래 및 주문 내역을 요청
   if(!HistorySelectByPosition(position_id))
     {
      PrintFormat("HistorySelectByPosition(%I64d) failed. Error %d"position_idGetLastError());
      return(false);
     }
     
//--- 포지션 주문 리스트를 인쇄
   int orders_total=HistoryOrdersTotal();
   for(int i=0i<orders_totali++)
     {
      ulong ticket=HistoryOrderGetTicket(i);
      if(ticket==0)
         continue;
      ENUM_ORDER_TYPE order_type=(ENUM_ORDER_TYPE)HistoryOrderGetInteger(ticketORDER_TYPE);
      PrintFormat("  [%d] Order %s #%I64u"iOrderTypeDescription(order_type), ticket);
     }
     
//--- 저널의 포지션 거래 리스트를 인쇄
   int deals_total =HistoryDealsTotal();
   for(int i=0i<deals_totali++)
     {
      ulong ticket=HistoryDealGetTicket(i);
      if(ticket==0)
         continue;
      ENUM_DEAL_ENTRY deal_entry=(ENUM_DEAL_ENTRY)HistoryDealGetInteger(ticketDEAL_ENTRY);
      ENUM_DEAL_TYPE  deal_type= (ENUM_DEAL_TYPE)HistoryDealGetInteger(ticketDEAL_TYPE);
      if(deal_type!=DEAL_TYPE_BUY && deal_type!=DEAL_TYPE_SELL)
         continue;
      PrintFormat("  [%d] Entry %s Deal %s #%I64u"iDealEntryDescription(deal_entry), DealTypeDescription(deal_type), ticket);
     }
   return(true);
  }
//+------------------------------------------------------------------+
//| 주문 유형 설명을 반환                                               |
//+------------------------------------------------------------------+
string OrderTypeDescription(const ENUM_ORDER_TYPE type)
  {
   switch(type)
     {
      case ORDER_TYPE_BUY              :  return("Buy");
      case ORDER_TYPE_SELL             :  return("Sell");
      case ORDER_TYPE_BUY_LIMIT        :  return("Buy Limit");
      case ORDER_TYPE_SELL_LIMIT       :  return("Sell Limit");
      case ORDER_TYPE_BUY_STOP         :  return("Buy Stop");
      case ORDER_TYPE_SELL_STOP        :  return("Sell Stop");
      case ORDER_TYPE_BUY_STOP_LIMIT   :  return("Buy Stop Limit");
      case ORDER_TYPE_SELL_STOP_LIMIT  :  return("Sell Stop Limit");
      default                          :  return("Unknown order type: "+(string)type);
     }
  }
//+------------------------------------------------------------------+
//| 포지션 거래 유형 설명을 반환                                         |
//+------------------------------------------------------------------+
string DealTypeDescription(const ENUM_DEAL_TYPE type)
  {
   switch(type)
     {
      //--- 매수 및 매도 거래에 대한 설명만 반환
      //--- 다른 모든 유형은 해당 포지션에 적용되지 않으므로
      case DEAL_TYPE_BUY   :  return("Buy");
      case DEAL_TYPE_SELL  :  return("Sell");
      default              :  return("Unknown deal type: "+(string)type);
     }
  }
//+------------------------------------------------------------------+
//| 포지션 변경 메서드 반환                                             |
//+------------------------------------------------------------------+
string DealEntryDescription(const ENUM_DEAL_ENTRY entry)
  {
   switch(entry)
     {
      case DEAL_ENTRY_IN      :  return("In");
      case DEAL_ENTRY_OUT     :  return("Out");
      case DEAL_ENTRY_INOUT   :  return("InOut");
      case DEAL_ENTRY_OUT_BY  :  return("Out by");
      case DEAL_ENTRY_STATE   :  return("Status record");
      default                 :  return("Unknown deal entry: "+(string)entry);
     }
  }

참고 항목

HistorySelect(), HistoryOrderGetTicket(), Order Properties