MT5 error 10038: Close volume exceeds the position (TRADE_RETCODE_INVALID_CLOSE_VOLUME)

You asked to close more volume than the position holds. Nothing was closed; read the position's current volume and resend.

10038TRADE_RETCODE_INVALID_CLOSE_VOLUME
After a fixSafe to retry?
NoCould it have executed?

What it means

You see this code in the retcode of a trade result, from OrderSend() in MQL5 or order_send() in Python.

A close or partial close can't be larger than what is open. 10038 means the volume in your close request is more than the position's current volume.

Positions can shrink after partial closes, so a volume read earlier may be stale.

Common causes

  • A volume stored when the position opened, used after a partial close.
  • A partial close computed from the wrong position.
  • Rounding up instead of down.

How to fix it

  • Read the position again right before closing and use its volume.
  • For a partial close, round down to the volume step and keep it at or below the open volume.

Example in Python:

import math
import MetaTrader5 as mt5

pos = mt5.positions_get(ticket=ticket)[0]
step = mt5.symbol_info(pos.symbol).volume_step
half = math.floor(pos.volume / 2 / step) * step
half = round(min(half, pos.volume), 8)

Is it safe to retry?

After you correct the volume.

Could the order have executed?

No. Nothing was closed.

All MetaTrader error codes

One API for MT4 and MT5

Clear trade outcomes, idempotent orders and an audit log. Read the error reference or the quickstart.