OrderCheck

The OrderCheck() function checks if there are enough money to execute a required trade operation. The check results are placed to the fields of the MqlTradeCheckResult structure.

bool  OrderCheck(
   MqlTradeRequest&       request,      // request structure
   MqlTradeCheckResult&   result        // result structure
   );

Parameters

request

[in]  Pointer to the structure of the MqlTradeRequest type, which describes the required trade action.

result

[in,out]  Pointer to the structure of the MqlTradeCheckResult type, to which the check result will be placed.

Return Value

If funds are not enough for the operation, or parameters are filled out incorrectly, the function returns false. In case of a successful basic check of structures (check of pointers), it returns true. However, this is not an indication that the requested trade operation is sure to be successfully executed. For a more detailed description of the function execution result, analyze the fields of the result structure.

In order to obtain information about the error, call the GetLastError() function.

Example:

#define   DEVIATION     5              // allowed deviation from the price
#define   VOLUME        1.0            // order volume
#define   EXPERT_MAGIC  123            // MagicNumber
#define   DIRECTION     ORDER_TYPE_BUY // opened position direction (ORDER_TYPE_BUY or ORDER_TYPE_SELL)
 
//+------------------------------------------------------------------+
//| Script program start function                                    |
//+------------------------------------------------------------------+
void OnStart()
  {
//--- declare and initialize the request, verification and result structures
   MqlTradeRequest     request={};
   MqlTradeCheckResult check  ={};
   MqlTradeResult      result ={};
   
//--- prepare trade request parameters
   PrepareRequest(_SymbolDIRECTIONVOLUMErequest);
   
//--- check trade request parameters
   ResetLastError();
   bool res=OrderCheck(requestcheck);
   if(!res)
     {
      PrintFormat("Trade request verification completed with error %d\nServer retcode: %u, comment: %s"GetLastError(), check.retcodecheck.comment);
      return;
     }
     
//--- trade request check was successful - display the description of the trade request verification structure fields
   Print("Trade request verification completed successfully");
   MqlTradeCheckResultPrint(check14);
   
//--- send a trade request
   if(!OrderSend(requestresult))
      Print("OrderSend error "GetLastError());    // if unable to send the request, display the error code
      
//--- information about the operation
   PrintFormat("Trade request result: retcode=%u, deal=%I64u, order=%I64u"result.retcoderesult.dealresult.order);
   /*
   result with disabled auto trading in the client terminal:
   Trade request verification completed with error 4752
   Server retcode10027commentAutoTrading disabled by client
   
   enable auto trading and check again on a closed market:
   Experts   automated trading is enabled
   Trade request verification completed successfully
   Retcode:      0
   Balance:      10779.50 USD
   Equity:       10779.50 USD
   Profit:       0.00 USD
   Margin:       1104.79 USD
   Margin free:  9674.71 USD
   Margin level975.71 %
   Comment:      Done
   OrderSend error 4756
   Trade request resultretcode=10018deal=0order=0
   
   check on the open market:
   Trade request verification completed successfully
   Retcode:      0
   Balance:      10779.50 USD
   Equity:       10779.50 USD
   Profit:       0.00 USD
   Margin:       110.46 USD
   Margin free:  10669.04 USD
   Margin level9758.74 %
   Comment:      Done
   Trade request resultretcode=10009deal=2777010968order=2802818813
   */
  }
//+------------------------------------------------------------------+
//| Prepare parameters for a trade request                           |
//+------------------------------------------------------------------+
void PrepareRequest(const string symbolconst ENUM_ORDER_TYPE order_typeconst double volumeMqlTradeRequest &request)
  {
   ENUM_ORDER_TYPE type=(DIRECTION !=ORDER_TYPE_BUY ? ORDER_TYPE_SELL : DIRECTION);
   double price=(DIRECTION==ORDER_TYPE_BUY ? SymbolInfoDouble(Symbol(), SYMBOL_ASK) : SymbolInfoDouble(Symbol(), SYMBOL_BID));
//--- request parameters
   request.action    = TRADE_ACTION_DEAL// trading operation type
   request.symbol    = symbol;            // symbol
   request.volume    = volume;            // volume
   request.type      = type;              // order type
   request.price     = price;             // open price
   request.deviation = DEVIATION;         // allowed deviation from the price
   request.magic     = EXPERT_MAGIC;      // order MagicNumber
  }
//+------------------------------------------------------------------+
//| Print the fields of the trade request                            |
//| verification result in the journal                               |
//+------------------------------------------------------------------+
void MqlTradeCheckResultPrint(const MqlTradeCheckResult &checkconst uint header_width=0)
  {
//--- get the account currency and the number of decimal places for the account currency
   string currency=AccountInfoString(ACCOUNT_CURRENCY);
   int    digits  =(int)AccountInfoInteger(ACCOUNT_CURRENCY_DIGITS);
   
//--- define the header text and the width of the header field
//--- if the header width is passed to the function equal to zero, then the width will be the size of the header line + 1
   string header="Retcode:";
   uint w=(header_width==0 ? header.Length()+1 : header_width);
//--- display the return code with the header of the specified width in the journal
   PrintFormat("%-*s%-u"wheadercheck.retcode);
   
//--- display the balance value after executing a trade operation in the journal
   header="Balance:";
   w=(header_width==0 ? header.Length()+1 : header_width);
   PrintFormat("%-*s%-.*f %s"wheaderdigitscheck.balancecurrency);
   
//--- display the equity value after executing a trade operation in the journal
   header="Equity:";
   w=(header_width==0 ? header.Length()+1 : header_width);
   PrintFormat("%-*s%-.*f %s"wheaderdigitscheck.equitycurrency);
      
//--- display the floating profit value after executing a trading operation in the journal
   header="Profit:";
   w=(header_width==0 ? header.Length()+1 : header_width);
   PrintFormat("%-*s%-.*f %s"wheaderdigitscheck.profitcurrency);      
      
//--- display the amount of margin, required for the necessary trading operation, in the journal
   header="Margin:";
   w=(header_width==0 ? header.Length()+1 : header_width);
   PrintFormat("%-*s%-.*f %s"wheaderdigitscheck.margincurrency);      
      
//--- display the value of equity to be left after conducting a trading operation in the journal
   header="Margin free:";
   w=(header_width==0 ? header.Length()+1 : header_width);
   PrintFormat("%-*s%-.*f %s"wheaderdigitscheck.margin_freecurrency);      
      
//--- display the margin level to be set after completing the required trading operation in the journal
   header="Margin level:";
   w=(header_width==0 ? header.Length()+1 : header_width);
   PrintFormat("%-*s%-.2f %%"wheadercheck.margin_level);      
      
//--- display the comment on the response code and error description in the journal
   header="Comment:";
   w=(header_width==0 ? header.Length()+1 : header_width);
   PrintFormat("%-*s%-s"wheadercheck.comment);      
  }

See also

OrderSend(), Trade Operation Types, Trade Request Structure, Structure of Request Check Results, Structure of a Trade Request Result