Orders can fail for many reasons: requotes, invalid stops, insufficient margin, market closed. An EA that does not handle errors may retry endlessly, skip trades silently, or open duplicates.
A robust approach is to check the return code of every trade request and handle the main categories explicitly. For temporary issues such as requotes or busy servers, a limited number of retries with a short pause is reasonable. For permanent issues such as invalid stops, log the problem and stop retrying.
A worked example of a duplicate bug: an EA sends an order, the connection drops before confirmation, and the EA sends it again on reconnect. Checking open positions by magic number before sending prevents this.
It is also wise to verify stop distances against the broker's minimum stop level before sending, since this varies by broker and can change around news.
Testing error handling on demo by deliberately sending invalid orders helps confirm the logic works.
Which order errors have caused you the most trouble?
Handling requotes and order errors gracefully in MQL5
-
FXS London Scalper
- Posts: 975
- Joined: Sun Oct 18, 2026 12:25 pm
Handling requotes and order errors gracefully in MQL5
FXS Official Team | London Session Desk
-
Amanhasnoname
- Posts: 2060
- Joined: Fri Oct 30, 2026 9:45 am
Re: Handling requotes and order errors gracefully in MQL5
The duplicate order case is the one that catches most people, and checking positions by magic number is a good fix as long as it also covers pending orders and the history of the last few seconds. After a reconnect, the terminal can take a moment to sync, so a position that was opened just before the drop may not yet appear in the list. A short wait and a second check, or storing a unique comment or request id in each order and searching for it, makes the guard much more reliable.
It also helps to separate the retcode categories in code rather than with a long chain of ifs. Requotes, price changed and off quotes are worth a refresh of the price and one or two retries. Invalid stops, invalid volume and not enough money should never be retried blindly, because the result will not change. Trade disabled and market closed should pause the EA until conditions change.
Logging the full request alongside the retcode, including price, stops, deviation and the symbol's current stop level and freeze level, saves a lot of guesswork later. Freeze level in particular is often forgotten and causes modify errors near the market.
It also helps to separate the retcode categories in code rather than with a long chain of ifs. Requotes, price changed and off quotes are worth a refresh of the price and one or two retries. Invalid stops, invalid volume and not enough money should never be retried blindly, because the result will not change. Trade disabled and market closed should pause the EA until conditions change.
Logging the full request alongside the retcode, including price, stops, deviation and the symbol's current stop level and freeze level, saves a lot of guesswork later. Freeze level in particular is often forgotten and causes modify errors near the market.