Skip to content

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

  1. Do not rely on tickets kept in memory: find the EA's positions by symbol and magic number when you need them.
  2. Always check the return of PositionSelectByTicket() and treat "not found" as "already closed".
  3. Rebuild the EA's state from the open positions in OnInit().

Fixed MQL5 code

MQL5
#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.

EA Doctor rules that catch it

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.