HistoryDealSelect

通过调用恰当函数选择历史交易。如果函数成功完成,返回真值,如果函数失败返回错误值。有关错误更多细节,调用 GetLastError()

bool  HistoryDealSelect(
   ulong  ticket      // 交易订单号
   );

参量

ticket

[in] 交易订单号。

返回值

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

注释

不要弄混 订单交易仓位。 每笔交易都按照订单执行,每个仓位都是一个或多个交易的值。

HistoryDealSelect()在MQL5程序从历史记录中删除一系列订单,如果成功执行HistoryDealSelect(),可用调用和复制的单个交易,如果需要通过 HistorySelect() 运行所有订单,最好使用 HistoryDealGetTicket()

示例:

#define   TICKET    2620919264   // 任何已知交易的编号, 例如来自于终端账户历史中的交易
 
long      ExtTicket=TICKET;      // 把脚本测试中宏替换的指定编号设置到变量中,
                                 // 该宏替换也可能用于在 EA 中的 OnTradeTransaction() 处理函数中处理交易:
//+------------------------------------------------------------------+
//| Expert TradeTransaction handler                                  |
//+------------------------------------------------------------------+
void OnTradeTransaction(const MqlTradeTransactiontrans,
                        const MqlTradeRequestrequest,
                        const MqlTradeResultresult)
  {
   //--- 如果事务是在历史中添加一个交易
   if(trans.type==TRADE_TRANSACTION_DEAL_ADD)
     {
      //--- 根据编号选择交易, 取得它的数据并在日志中显示交易的描述
      HistoryDealSelectProcess(trans.deal);
     }
  }
 
//+------------------------------------------------------------------+
//| 脚本程序起始函数                                                   |
//+------------------------------------------------------------------+
void OnStart()
  {
//--- 根据编号选择交易, 取得它的数据并在日志中显示交易的描述
   HistoryDealSelectProcess(ExtTicket);
   /*
  结果:
   (Position ID #2645974677EURUSD Deal Out 0.10 Buy #2620919264 by order #2646028969 at 1.091782024.07.15 18:16:32.570
   */
  }
//+------------------------------------------------------------------+
//| 根据编号选择交易并在日志中打印交易数据                                 |
//+------------------------------------------------------------------+
void HistoryDealSelectProcess(const ulong deal_ticket)
  {
//--- 根据在 deal_ticket 中指定的编号选择历史交易
   ResetLastError();
   if(!HistoryDealSelect(deal_ticket))
     {
      PrintFormat("HistoryDealSelect(%I64u) failed. Error %d"deal_ticketGetLastError());
      return;
     }
 
//--- 如果成功选择了交易, 就取得它的数据并在日志中显示交易的描述
   ENUM_DEAL_TYPE    deal_type  = (ENUM_DEAL_TYPE)HistoryDealGetInteger(ExtTicketDEAL_TYPE);
   ENUM_DEAL_ENTRY   deal_entry = (ENUM_DEAL_ENTRY)HistoryDealGetInteger(ExtTicketDEAL_ENTRY);
   ENUM_DEAL_REASON  deal_reason= (ENUM_DEAL_REASON)HistoryDealGetInteger(ExtTicketDEAL_REASON);
   long              deal_time  = HistoryDealGetInteger(ExtTicketDEAL_TIME_MSC);
   long              deal_order = HistoryDealGetInteger(ExtTicketDEAL_ORDER);
   long              deal_pos_idHistoryDealGetInteger(ExtTicketDEAL_POSITION_ID);
   string            deal_symbolHistoryDealGetString(ExtTicketDEAL_SYMBOL);
   double            deal_volumeHistoryDealGetDouble(ExtTicketDEAL_VOLUME);
   double            deal_price = HistoryDealGetDouble(ExtTicketDEAL_PRICE);
   int               digits     = (int)SymbolInfoInteger(deal_symbolSYMBOL_DIGITS);
   
   PrintFormat("(Position ID #%I64d) %s Deal %s %.2f %s #%I64u by order #%I64d at %.*f, %s",
               deal_pos_iddeal_symbolDealEntryDescription(deal_entry), deal_volume,
               DealTypeDescription(deal_type), ExtTicketdeal_orderdigitsdeal_price,
               TimeMscToString(deal_time));
  }
//+------------------------------------------------------------------+
//| 返回交易类型的描述                                                  |
//+------------------------------------------------------------------+
string DealTypeDescription(const ENUM_DEAL_TYPE type)
  {
   switch(type)
     {
      case DEAL_TYPE_BUY                     :  return("Buy");
      case DEAL_TYPE_SELL                    :  return("Sell");
      case DEAL_TYPE_BALANCE                 :  return("Balance");
      case DEAL_TYPE_CREDIT                  :  return("Credit");
      case DEAL_TYPE_CHARGE                  :  return("Additional charge");
      case DEAL_TYPE_CORRECTION              :  return("Correction");
      case DEAL_TYPE_BONUS                   :  return("Bonus");
      case DEAL_TYPE_COMMISSION              :  return("Additional commission");
      case DEAL_TYPE_COMMISSION_DAILY        :  return("Daily commission");
      case DEAL_TYPE_COMMISSION_MONTHLY      :  return("Monthly commission");
      case DEAL_TYPE_COMMISSION_AGENT_DAILY  :  return("Daily agent commission");
      case DEAL_TYPE_COMMISSION_AGENT_MONTHLY:  return("Monthly agent commission");
      case DEAL_TYPE_INTEREST                :  return("Interest rate");
      case DEAL_TYPE_BUY_CANCELED            :  return("Canceled buy deal");
      case DEAL_TYPE_SELL_CANCELED           :  return("Canceled sell deal");
      case DEAL_DIVIDEND                     :  return("Dividend operations");
      case DEAL_DIVIDEND_FRANKED             :  return("Franked (non-taxable) dividend operations");
      case DEAL_TAX                          :  return("Tax charges");
      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("Reverce");
      case DEAL_ENTRY_OUT_BY  :  return("Out by");
      case DEAL_ENTRY_STATE   :  return("Status record");
      default                 :  return("Unknown deal entry: "+(string)entry);
     }
  }
//+------------------------------------------------------------------+
//| 返回包含毫秒信息的时间                                              |
//+------------------------------------------------------------------+
string TimeMscToString(const long time_mscint flags=TIME_DATE|TIME_MINUTES|TIME_SECONDS)
  {
   return(TimeToString(time_msc/1000flags) + "." + IntegerToString(time_msc %10003, '0'));
  }

另见

HistorySelect()HistoryDealGetTicket()交易属性