Download MetaTrader 5

The Structure of a Trade Request Result (MqlTradeResult)

As result of a trade request, a trade server returns data about the trade request processing result as a special predefined structure of MqlTradeResult type.

struct MqlTradeResult
   uint     retcode;          // Operation return code
   ulong    deal;             // Deal ticket, if it is performed
   ulong    order;            // Order ticket, if it is placed
   double   volume;           // Deal volume, confirmed by broker
   double   price;            // Deal price, confirmed by broker
   double   bid;              // Current Bid price
   double   ask;              // Current Ask price
   string   comment;          // Broker comment to operation (by default it is filled by description of trade server return code)
   uint     request_id;       // Request ID set by the terminal during the dispatch 
   uint     retcode_external// Return code of an external trading system

Fields description




Return code of a trade server


Deal ticket,  if a deal has been performed. It is available for a trade operation of TRADE_ACTION_DEAL type


Order ticket, if a ticket has been placed. It is available for a trade operation of TRADE_ACTION_PENDING type


Deal volume, confirmed by broker. It depends on the order filling type


Deal price, confirmed by broker. It depends on the deviation field of the trade request and/or on the trade operation


The current market Bid price (requote price)


The current market Ask price (requote price)


The broker comment to operation (by default it is filled by description of trade server return code)


Request ID set by the terminal when sending to the trade server


The code of the error returned by an external trading system. The use and types of these errors depend on the broker and the external trading system, to which trading operations are sent.

The trade operation result is returned to a variable of the MqlTradeResult type, which is passed as the second parameter to OrderSend() to perform trade operations.

The terminal fixes request ID in request_id field when sending it to the trade server using OrdersSend() and OrderSendAsync() functions. The terminal receives messages about performed transactions from the trade server and submits them for processing by OnTradeTransaction() function containing the following components as parameters:

  • description of the trade transaction in MqlTradeTransaction structure;
  • description of the trade request sent from OrderSend() or OrdersSendAsync() function. Request ID is sent by the terminal to the trade server, while the request itself and its request_id are stored in the terminal memory;
  • the trade request execution result as MqlTradeResult structure with request_id field containing ID of this request.

OnTradeTransaction() function receives three input parameters but the last two should be analyzed only for transactions having TRADE_TRANSACTION_REQUEST type. In all other cases, data on the trade request and its execution result are not filled. Example of parameters analysis can be found at Structure of a Trade Request.

Setting request_id by the terminal for the trade request when sending it to the server is mainly introduced for working with OrderSendAsync() asynchronous function. This identifier allows to associate the performed action (OrderSend or OrderSendAsync functions call) with the result of this action sent to OnTradeTransaction().


//| Sending a trade request with the result processing               |
bool MyOrderSend(MqlTradeRequest request,MqlTradeResult result)
//--- reset the last error code to zero
//--- send request
   bool success=OrderSend(request,result);
//--- if the result fails - try to find out why
      int answer=result.retcode;
      Print("TradeLog: Trade request failed. Error = ",GetLastError());
         //--- requote
         case 10004:
            Print("request.price = ",request.price,"   result.ask = ",
                  result.ask," = ",;
         //--- order is not accepted by the server
         case 10006:
            Print("request.price = ",request.price,"   result.ask = ",
                  result.ask," = ",;
         //--- invalid price
         case 10015:
            Print("request.price = ",request.price,"   result.ask = ",
                  result.ask," = ",;
         //--- invalid SL and/or TP
         case 10016:
            Print(" = ",," = ",;
            Print("result.ask = ",result.ask," = ",;
         //--- invalid volume
         case 10014:
            Print("request.volume = ",request.volume,"   result.volume = ",
         //--- not enough money for a trade operation 
         case 10019:
            Print("request.volume = ",request.volume,"   result.volume = ",
                  result.volume,"   result.comment = ",result.comment);
         //--- some other reason, output the server response code 
            Print("Other answer = ",answer);
      //--- notify about the unsuccessful result of the trade request by returning false
//--- OrderSend() returns true - repeat the answer

Updated: 2016.07.08