When we close a market order, we are exiting the trade at the current market price, which would be the current Bid price for buy orders and the current Ask price for sell orders. Learn in this complete MQL4 OrderClose Function article how to program the EA to close open orders if exit conditions are met.
To close market orders the used code is the OrderClose() function, which has the following syntax:
bool OrderClose (int Ticket, double Lots, double Price, int Slippage, color Arrow);
The table below describes each of these parameters:
Parameter | Description |
---|---|
Ticket | The ticket number of the market order to close |
Lots | The number of lots to close. |
Price | The preferred price at which to close the trade. |
Slippage | The allowed pip slippage from closing price. |
Color | A color constant for the closing arrow. |
The example below closes a buy market order, borrowed from the MACD Sample:
for(cnt=0; cnt<total ;cnt++)
{
OrderSelect(cnt, SELECT_BY_POS, MODE_TRADES);
if(OrderType()<=OP_SELL && // check for opened position
OrderSymbol()==Symbol()) // check for symbol
{
if(OrderType()==OP_BUY) // long position is opened
{
// should it be closed?
if(MacdCurrent>0 && MacdCurrentSignalPrevious &&
MacdCurrent>(MACDCloseLevel*Point))
{
OrderClose(OrderTicket(),OrderLots(),Bid,3,Violet); // close position
return(0); // exit
}
Explanation
Here the close code starts with the for operator, continuously looping through the block of code to calculate the closing conditions.
The function OrderSelect() is used to examine the pool of opened orders, looking for the matching symbol, as well as the order types (OP_BUY or OP_SELL).
The last, and most important condition for this block of code, is to watch for the strategy conditions for exiting, which in the example above is the MACD exit conditions.
Once the open position symbol and order types are identified, and the MACD exit conditions are met, then the OrderClose() function can be deployed, the parameters of which are discussed in the table above.
Custom Close Function
It is can be useful to work with a custom close function because then you can can invoke it easily from within your Start() function whenever you have the need to close an order by a any set of conditions. Your custom close function should examine the pool of currently open orders, identifying your order types along with their magic numbers.
MT4 Code Snippet
void close(int type){
if(OrdersTotal()>0){
for(Counter=OrdersTotal()-1;Counter>=0;Counter–){
OrderSelect(Counter,SELECT_BY_POS,MODE_TRADES);
if(type==OP_BUY && OrderType()==OP_BUY){
if(OrderSymbol()==Symbol() && OrderMagicNumber()==MagicNumber) {
RefreshRates();
OrderClose(OrderTicket(),OrderLots(),NormalizeDouble(Bid,Digits), vSlippage);
} }
if(type==OP_SELL && OrderType()==OP_SELL){
if(OrderSymbol()==Symbol() && OrderMagicNumber()==MagicNumber) {
RefreshRates(); OrderClose(OrderTicket(),OrderLots(),NormalizeDouble(Ask,Digits),vSlippage);
}}
}}}
MT4 Usage
if (OType==0 && CloseBuy==true)
{
close (OP_BUY); // Close Buy
Explanation
We have chosen close() as the name of our custom close function, and whenever we want to invoke it, we simply have to insert that one word after our close conditions, as in the usage example above.
The second line initiates the function if there is more than zero total open positions. The third line examines the pool of currently open orders, counting them. The fourth line invoke the OrderSelect() function in order to select these counted orders for further processing.
We have constructed only one parameter for the custom close function, and that is the variable int type in parenthesis (). This parameter is going to be the identifier of our order type. In the custom function, we indicate that type will equal (==) either OP_BUY or OP_SELL, and so we when we invoke close(), we must indicate if close() is close (OP_BUY) or close (OP_SELL), as in the usage example above. In the function, we associate type==OP_BUY and OrderType==OP_BUY together with the ampersand (&&).
We also want to make sure that we are selecting buy orders of the correct symbol and magic number, as you can in line 6:
if(OrderSymbol()==Symbol() && OrderMagicNumber()==MagicNumber) {
Refresh Rates() is a function that refreshes the rates of the currency pair, so at to get filled at the most current rates.
Lastly, we deploy the OrderClose() function, the parameters of which can be found in the table above.