HistorySelectByPosition

检索特殊位置标识符交易和订单历史记录。

bool  HistorySelectByPosition(
   ulong   position_id     // 仓位标识符 - POSITION_IDENTIFIER
   );

参量

position_id

[in]  位置标识符设置每个已执行命令和每笔交易。

返回值

如果成功返回真值,否则返回错误值。

注释

不能把出现在客户端"工具箱"的"交易" 标签中的当前挂单和交易历史订单弄乱。取消的或者导致的交易的订单列表可以在"工具箱"客户端中的"历史记录"标签浏览。

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()订单属性