OrderClose Wrapper With Retries
This header retries a close the same way it retries a send: requote, off-quotes, busy context, timeout and the rest of the transient trade errors are retried; invalid-volume-style permanent errors are returned immediately so the caller is not stuck looping on a request that cannot succeed. Closing has one problem sending does not: the ticket can vanish out from under the caller between the decision to close it and this function actually running, because it hit its own stop loss or take profit in the meantime. That is the outcome the caller wanted, not a failure, and treating it as an error produces exactly the kind of code that retries a close that has nothing left to close. SafeOrderClose checks for this FIRST, before attempting anything, and again on every retry attempt, and returns true the moment it finds the ticket already gone - whether that is because it hit its stop, was closed elsewhere, or never existed on this account at all. A pending order is refused outright rather than silently mishandled. Closing does not apply to a pending order - cancelling one is a different operation this header does not attempt - so passing a pending ticket returns false with SafeOrderCloseLastError reporting a sentinel value documented in the header, rather than attempting a close that would fail in a way that looks like a transient error and gets retried for no reason. This header does not include SafeOrderSend from this catalogue, even though the retry classification is identical. Per this catalogue's rule, every library has to work as a standalone download, so the classification function is duplicated here rather than shared. The header is self-contained. On MQL5 it uses CTrade from the standard library, which ships with every terminal, so this does not break self-containment; no other dependencies, no DLLs. Notes and limitations - SafeOrderClose only closes an open market order/position. A pending ticket is refused with the header's pending-order sentinel as the last error - cancel a pending order/request through a different operation. - Partial close volumes are clamped to never exceed the remaining lots, but are NOT rounded to the broker's lot step - pass a value already valid for the symbol, or normalise it first with a lot-rounding library. - "Already closed" covers several different situations (hit its own SL/TP, closed by other code, or a ticket that never existed) and SafeOrderClose deliberately does not distinguish between them. If a caller needs to know which one happened, check trade history after the call rather than relying on the return value alone. - Sleep() blocks the calling thread for the wait duration on both platforms - fine in the strategy tester, briefly unresponsive on a live chart during the retry window. - MQL4 uses a single global (g_SafeOrderCloseLastError) reflecting the most recent call only, in the same style as the built-in GetLastError. MQL5 keeps the same convention for the same reason.
Script · MT5 · v1.0 · Free
About OrderClose Wrapper With Retries
OrderClose Wrapper With Retries is an expert advisor (trading robot) for MetaTrader 5. It has 3 settings, such as Place real order, Max retries and Retry wait ms. MQL source code included.
Files in the download: SafeOrderClose.mq5.mqh, SafeOrderClose.txt, SafeOrderCloseDemo.ex5, SafeOrderCloseDemo.mq5.
Trading robots can lose money. Check every setting and test on a demo account before using real funds.
Settings
| Setting | Default | What it does |
|---|---|---|
| Place real order | false | WARNING: true opens and closes a real minimum-lot market position on this symbol |
| Max retries | 5 | Maximum retry attempts for a temporary error |
| Retry wait ms | 500 | Milliseconds to wait between retries |
How to install
- Download the ZIP from your PolyMaria library and unzip it.
- In MetaTrader 5 open File → Open Data Folder, then go to MQL5 → Experts.
- Copy the .mq5 file(s) there. Put any .mqh files in MQL5 → Include.
- Open the .mq5 file in MetaEditor and press Compile (F7) if MetaTrader does not show it.
- Drag the expert advisor from the Navigator onto a chart, check the settings, allow Algo/Auto Trading and press OK.
- Test it first on a demo account or in the Strategy Tester.
Tags: Volume · MT5 Script · MQL5 Source Code