Skip to content

Trade server return code

10040 TRADE_RETCODE_LIMIT_POSITIONS

Position limit reached: the account holds as many positions as the server allows

What it means

The broker limits how many positions an account may hold at once, and the limit is reached. The reference explains how it is counted: on netting accounts only open positions count, so new orders are still allowed on symbols that already have a position; on hedging accounts pending orders count together with open positions.

Common causes

  • Grid and recovery EAs that open a new position at every level.
  • Several EAs on one account, each within its own limit but together above the broker's.
  • On hedging accounts, many pending orders placed in advance, which count toward the limit too.

A real scenario

A grid EA on a hedging account places pending orders for 30 levels in advance and opens 12 positions on top. The broker's limit is 40, so the 43rd request fails with 10040 and the basket loses the level it was built around. Counting positions and pending orders against a limit set in the EA kept the grid inside it.

How to fix it

  1. Count your EA's positions (and, on hedging accounts, pending orders) before opening another, against a limit in your inputs.
  2. Read ACCOUNT_LIMIT_ORDERS ("Maximum allowed number of active pending orders"; 0 means no limit) for the pending side.
  3. Do not retry after 10040: the limit stays until a position closes.

Fixed MQL5 code

MQL5
input int  InpMaxPositions = 10;
input long InpMagic        = 20261130;

int CountOurPositions()
{
   int n = 0;
   for(int i = PositionsTotal() - 1; i >= 0; i--)
   {
      ulong ticket = PositionGetTicket(i);             // also selects the position
      if(ticket > 0 && PositionGetInteger(POSITION_MAGIC) == InpMagic)
         n++;
   }
   return n;
}

bool MayOpenAnother()
{
   if(CountOurPositions() >= InpMaxPositions)
      return false;
   int pendingLimit = (int)AccountInfoInteger(ACCOUNT_LIMIT_ORDERS);   // 0 = no limit on pending orders
   if(pendingLimit > 0 && OrdersTotal() >= pendingLimit)
      return false;
   return true;
}

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 10040? 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.