Broker API HubMenu

MQL5 OrderSend()

functionOrderSend()MQL5Sending orders

Sends a trade request to the trade server and waits for its answer. One function covers a market order, a pending order, a change, a cancellation and a close: the action field of the request says which.

Risk warning: CFDs and spread bets are complex instruments and come with a high risk of losing money rapidly due to leverage. You can lose money on these products, and each provider publishes its own loss figure under FCA rules. You should consider whether you understand how they work and whether you can afford to take the high risk of losing your money. Automated and API trading can amplify losses through bugs, connectivity failures and runaway orders.

Independent and unofficialmql5.comchecked

This is an MQL5 function, not an HTTP endpoint. It has no method, no path and no address to send a request to: it runs inside the MetaTrader 5 terminal, in a program written in MQL5.

This function can place a real order on whichever account the terminal is logged in to, demo or live. The page recommends checking a request with OrderCheck() before sending it, and says the result can be followed in the OnTradeTransaction handler, which is called several times for one request. The MqlTradeRequest page, opened the same day, lists the actions TRADE_ACTION_DEAL, TRADE_ACTION_PENDING, TRADE_ACTION_SLTP, TRADE_ACTION_MODIFY and TRADE_ACTION_REMOVE, and shows a position being closed with TRADE_ACTION_DEAL in the opposite direction.

Signature

The form of the call on the vendor's page, with the vendor's comments taken out.

bool OrderSend(
  MqlTradeRequest& request,
  MqlTradeResult& result
)

Arguments

NameTypeDescription
requestrequiredMqlTradeRequest&The trade request, an MqlTradeRequest structure passed by reference. Its action field sets the kind of operation and the rest of its fields describe it.
resultrequiredMqlTradeResult&An MqlTradeResult structure passed by reference, which the function fills in with the outcome: the return code, and the deal and order tickets where there are any.

Return value

Type
bool
What comes back
True when the request passed the basic check of its structures and, the page says, the server accepted the order. The page is firm that true is not a sign the trade was carried out: the outcome is in the fields of the result structure.

Sample

Written by this site from the signature and arguments recorded on this page. It has not been run against a terminal, a server or an account, and the values in it are placeholders. It holds no credential and needs none: the program runs inside a terminal that is already logged in to an account. Nothing is entered on this site.

// Script. Run it only on a demo account: it sends a market order.
void OnStart()
  {
   MqlTradeRequest request={};
   MqlTradeResult  result={};
   request.action   =TRADE_ACTION_DEAL;
   request.symbol   =_Symbol;
   request.volume   =0.1;
   request.type     =ORDER_TYPE_BUY;
   request.price    =SymbolInfoDouble(_Symbol,SYMBOL_ASK);
   request.deviation=5;
   request.magic    =123456;
   if(!OrderSend(request,result))
      PrintFormat("OrderSend error %d",GetLastError());
   PrintFormat("retcode=%u deal=%I64u order=%I64u",result.retcode,result.deal,result.order);
  }

Return codes documented for this function

The page sends the reader to the fields of the result structure for the outcome, and its example reads result.retcode as the return code of the trade server's reply. The MqlTradeResult page calls that field the return code of a trade server. Each of those codes recorded here is below. mql5.comchecked