Broker API HubMenu

MQL4 OrderClose()

functionOrderClose()MQL4Sending orders

Closes an open order, in full or for a given number of lots, at a price you pass.

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 unofficialdocs.mql4.comchecked

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

In MetaTrader 4 an open trade is itself called an order, so this is the call that closes what other platforms call a position.

Signature

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

bool OrderClose(
  int ticket,
  double lots,
  double price,
  int slippage,
  color arrow_color
)

Arguments

NameTypeDescription
ticketrequiredintThe order's ticket number.
lotsrequireddoubleThe number of lots to close.
pricerequireddoubleThe closing price.
slippagerequiredintThe most price slippage to accept, in points.
arrow_colorcolorThe colour of the closing arrow on the chart. Left out or set to CLR_NONE, no arrow is drawn.

Return value

Type
bool
What comes back
True on success, false otherwise. The page points to GetLastError() for the reason.

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.

// Run it only on a demo account: it closes open buy orders in this symbol.
void OnStart()
  {
   for(int i=OrdersTotal()-1; i>=0; i--)
     {
      if(!OrderSelect(i,SELECT_BY_POS,MODE_TRADES))
         continue;
      if(OrderSymbol()==Symbol() && OrderType()==OP_BUY)
         if(!OrderClose(OrderTicket(),OrderLots(),Bid,3,clrNONE))
            Print("OrderClose failed with error #",GetLastError());
     }
  }