Runtime error
4753 ERR_ TRADE_ POSITION_ NOT_ FOUND
Position not found: the ticket points at a position that is gone
What it means
The position the code refers to does not exist (any more). The usual reason is that the EA kept a ticket in a variable and the position was closed by the server, by the user or by a stop-out in the meantime; after a restart, global variables are reset.
Common causes
- A position closed by its stop loss or take profit while the EA still holds its ticket.
- The EA restarted (terminal restart, recompilation, timeframe change) and lost the tickets kept in global variables.
- Using an order or deal ticket where a position ticket is expected (MQL4 habits; see 4108).
A real scenario
An EA stores the ticket of its open trade in a global variable and closes it on an opposite signal. After a VPS restart the variable is 0, PositionSelectByTicket(0) fails with 4753 and the position stays open with no management. Finding the EA's position by symbol and magic number on every tick works after restarts too.
How to fix it
- Do not rely on tickets kept in memory: find the EA's positions by symbol and magic number when you need them.
- Always check the return of
PositionSelectByTicket()and treat "not found" as "already closed". - Rebuild the EA's state from the open positions in
OnInit().
Fixed MQL5 code
#include <Trade/Trade.mqh>
CTrade trade;
input long InpMagic = 20261130;
// Close this EA's position on a symbol, wherever its ticket came from.
bool CloseOurPosition(const string symbol)
{
for(int i = PositionsTotal() - 1; i >= 0; i--)
{
ulong ticket = PositionGetTicket(i); // also selects the position
if(ticket == 0)
continue;
if(PositionGetString(POSITION_SYMBOL) == symbol && PositionGetInteger(POSITION_MAGIC) == InpMagic)
return trade.PositionClose(ticket);
}
return true; // nothing open: it was closed already (SL, TP, stop-out or by hand)
}
A focused example for MetaTrader 5: adapt names, magic number and limits to your EA, and test it on a demo account first.
Related codes
- 4108Timer or Invalid TicketERR_CHART_CANNOT_CREATE_TIMERA timer that could not be created (MQL5), an invalid ticket (MQL4)
- 10045FIFO Close RuleTRADE_RETCODE_FIFO_CLOSEFIFO rule: positions must be closed oldest first
EA Doctor rules that catch it
- HighPositions closed in a loop that counts upA loop from 0 to PositionsTotal() closes positions, so every close shifts the list and the loop skips the next position.
Sources
Get your EA fixed
Still failing with 4753? Send us the EA and the journal lines: we find the cause in the code and fix it. Or check it yourself first with the EA Doctor.