In MT5, closing a position is done by a trade deal in the opposite direction in reference to an open position. Here is a simple example (NB! position must be previously selected) ...
// Close selected position bool PositionClose( void ) { MqlTradeRequest stCloseRequest = {}; switch( (ENUM_POSITION_TYPE) PositionGetInteger( POSITION_TYPE ) ) { case POSITION_TYPE_BUY: stCloseRequest.type = ORDER_TYPE_SELL; stCloseRequest.price = SymbolInfoDouble( _Symbol, SYMBOL_BID ); break; case POSITION_TYPE_SELL: stCloseRequest.type = ORDER_TYPE_BUY; stCloseRequest.price = SymbolInfoDouble( _Symbol, SYMBOL_ASK ); break; default: return false; }; stCloseRequest.action = TRADE_ACTION_DEAL; stCloseRequest.position = PositionGetInteger( POSITION_TICKET ); stCloseRequest.volume = PositionGetDouble( POSITION_VOLUME ); stCloseRequest.symbol = _Symbol; stCloseRequest.magic = i_nMagicNumber; stCloseRequest.deviation = i_nMaxDeviation; stCloseRequest.type_filling = g_eOrderFillType; MqlTradeResult stCloseResult = {}; if( !OrderSend( stCloseRequest, stCloseResult ) ) { PrintFormat( "Error %d closing selected position with return code %d (%s)", _LastError, stCloseResult.retcode, stCloseResult.comment ); return false; }; return true; };
Thank you for the code, but I don't see how I can use it.
Where and how is one supposed to use it?
I need to be able to close an order by ticket.
How do I use this code to tell MT5 to please close position with order ticket number 12345678?
How do I use this code to tell MT5 to please close position with order ticket number 12345678?
bool PositionCloseByTicket( const ulong Ticket ) { bool Res = false; if (PositionSelectByTicket(Ticket)) { MqlTradeRequest Request = {}; MqlTradeResult Result; Request.action = TRADE_ACTION_DEAL; Request.position = Ticket; Request.symbol = PositionGetString(POSITION_SYMBOL); Request.volume = PositionGetDouble(POSITION_VOLUME); Request.price = PositionGetDouble(POSITION_PRICE_CURRENT); Request.type = (ENUM_ORDER_TYPE)(1 - PositionGetInteger(POSITION_TYPE)); // Request.type_filling = Res = OrderSend(Request, Result); } return(Res); } void OnStart() { const ulong Ticket = PositionGetTicket(0); // 12345678 PositionCloseByTicket(Ticket); }
P.S.
#include <MT4Orders.mqh> // https://www.mql5.com/en/code/16006 void OnStart() { const ulong Ticket = PositionGetTicket(0); // 12345678 if (OrderSelect(Ticket, SELECT_BY_TICKET)) OrderClose(OrderTicket(), OrderLots(), OrderClosePrice(), 0); }
Select the position by ticket first, then you can call the the function in my example.
PositionSelectByTicket( nTicket );
PositionClose();
In MT5, closing a position is done by a trade deal in the opposite direction in reference to an open position. Here is a simple example (NB! position must be previously selected) ...
Hi Fernando,
Your example code is perfect and functional!
I'm facing an specific issue that maybe you've experienced before...
I have an EA that opens position during the normal trading hours of B3 (Brazilian Exchange), but the closing of this position shall be during the "closing auction". I'm using hedging account.
Your example code works perfectly in this scenario for demo account, since the closing order is sent during the closing auction. In fact, it executes instantaneously and I know it wouldn't happen when in real account.
But in live trading /real account, the order is accepted by the broker, but it's executed only next day after the opening aucion (and not right after the end of closing auction at the final closing price).
Still trying to find a solution, I tried to use limit orders instead +/-20% in order to ensure execution, but I had a side effect which is the opening of a new opposite position, and not the closing of the desired original one (maybe because I'm using hedging account).
Have you ever seen this case or do you have any ideas for solve it without having to switch to netting account?
Thank you very much!
- Free trading apps
- Over 8,000 signals for copying
- Economic news for exploring financial markets
You agree to website policy and terms of use
I am trying to learn how to close a position.
I found at least a dozen examples, more like two dozen, but it seems that ALL of them either require CTrade or present a solution that involves iterating over many open orders and selecting by position, which confuses me.
Can someone please provide an example of closing a position in MQL5 that does not involve CTrade (or any other include) and that only applies to ONE position by ticket number specifically? It's seems to be really hard to find.