Why an automatic exit can be rejected, and the order that fixes it
Closing a position sounds like one order. In live trading it is five steps, and doing them in the wrong order can leave you in the trade.
Last reviewed November 2026
What went wrong
A kill switch we built tripped on a daily limit and sent market exits. Every one was rejected for margin, while the software logged them as sent.
The cause: each position already had a stop-loss SELL resting. A second SELL for the same quantity meant the broker saw twice the position being sold. It treated the extra half as a new short and asked for margin the account didn't have.
The order of steps
- Cancel every resting stop and pending order on the account first.
- Confirm each cancel in the order book. A cancel request is not a cancel until the order shows as cancelled.
- Re-read the positions. A stop may have filled while you were cancelling. If you skip this, you can sell a position that is already closed and open a short.
- Exit what is still open, with the product type the position was opened with. An intraday position closed as delivery can be refused.
- Confirm the fill in the order book. If the exit was rejected, say so loudly, with the broker's reason, and retry.
Our test broker now models this exact margin rejection. The old code fails the test, the fixed code passes.
Related traps
- Don't re-arm the stop. Once tripped, the auto stop-loss must stand down. Otherwise it places a new stop every second, and every exit hits the same conflict.
- Two checks at once. If two parts of the software check the account at the same moment, both can place a stop for the same position. One lock around the whole check stops it.
- After a timeout, look before you resend. A timed-out order may still have been placed. Search the order book (by your own order tag) before sending another.
- Very large exits. An order above the exchange's freeze quantity is refused. Split it, or use the broker's own slicing, and confirm every slice.
General information from building and running these systems, not legal or investment advice. Brokers change their APIs and rules; check your broker's current documentation before relying on any detail here.
Want this handled in your own software?
Tell us what you trade and what you'd like the software to do. You'll get questions or a rough scope within one working day.