OrderSend Wrapper With Retries
A requote, a busy trade context, a broker-busy response, or a brief connection drop are all transient. The same request usually succeeds a moment later, and an EA that gives up on the first failure loses trades to noise the market never actually rejected. But retrying everything is just as wrong in the other direction: an invalid volume or a disabled symbol fails identically every time, and looping on it wastes the retry budget instead of surfacing the real problem. This header draws that line explicitly and only retries the transient list. For a market order, price is refreshed from the current Bid/Ask before every attempt. Resending the price from the first attempt is a common reason a retry loop fails every single time on a price-changed rejection - the market has already moved, and the stale price is invalid again before it is even sent. A pending order's price is left exactly as the caller supplied it on every attempt, because that price is a deliberate choice, not "the market right now". The wrapper returns the resulting ticket on success or -1 on failure. The specific error/retcode from the last attempt is available afterwards through SafeOrderSendLastError, in the same style as the built-in GetLastError. On MQL5 specifically, this header also carries the one trap that genuinely matters when OPENING new exposure: on a netting account there is at most one position per symbol, and a new market order on a symbol that already carries a position SILENTLY MERGES into it - even one opened by a different EA (different magic) or manually. SafeOrderSend checks for this before sending and refuses outright rather than taking over foreign exposure. See Differences below for exactly when this applies. 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 - This wrapper retries the SEND. It does not verify the position afterwards against slippage or partial fills beyond what the send call itself reports. - Trading-disabled states are treated as retryable because they are frequently momentary - AutoTrading toggled off for a moment, or a confirmation dialog on screen - not a permanent block. If trading is disabled for the whole session, every attempt fails the same way and the function correctly returns -1 after exhausting maxRetries. - Sleep() blocks the calling thread for the wait duration on both platforms. On a strategy tester this is fine; on a live chart it means the terminal is unresponsive for that EA during the retry window. - The netting-refusal check (MQL5 only) only inspects OPEN positions, not resting pending orders on the same symbol - a pending order from another magic does not block a new market send, since it has not created any position yet to merge into. - MQL4 uses a single global (g_SafeOrderSendLastError) 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 OrderSend Wrapper With Retries
OrderSend Wrapper With Retries is an expert advisor (trading robot) for MetaTrader 5. It places pending buy/sell stop or limit orders. It has 3 settings, such as Place real order, Max retries and Retry wait ms. MQL source code included.
Files in the download: SafeOrderSend.mq5.mqh, SafeOrderSend.txt, SafeOrderSendDemo.ex5, SafeOrderSendDemo.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 sends 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 · Pending Orders · MT5 Script · MQL5 Source Code