OrderModify Wrapper With Retries
Most hand-rolled modify wrappers get one specific case wrong: when the server reports that the requested price, stop loss and take profit already match the ticket exactly. Nothing was wrong with the request - there was simply nothing left to change. A wrapper that treats every non-success return as a failure will retry that request, get the same no-change response again, and loop until it runs out of attempts, reporting a failure for a modify that had already succeeded before it was even sent. This header treats that response as success-no-change, which is what it actually means - on MQL4 that is ERR_NO_RESULT, on MQL5 the directly equivalent TRADE_RETCODE_NO_CHANGES. The second half of the problem is telling temporary failures from permanent ones. A busy trade context, a requote, or an off-quotes tick will usually clear on the next attempt. Invalid stops, a closed ticket, or trading disabled will not, no matter how many times you ask. Retrying the second kind only delays the caller finding out. This header classifies the error and only retries the transient list. Stops are validated before the first attempt is even sent, checked against both the broker's minimum stop distance and its freeze distance for the symbol. That check is reimplemented directly in this header rather than pulled in from elsewhere, because every submission in this catalogue is self-contained and stands alone when downloaded on its own. Notes and limitations - The pre-flight stop check measures distance from Bid (buy side) or Ask (sell side) at the moment of the call. A fast-moving market can still produce an invalid-stops rejection on the actual send if price moves between the check and the request reaching the server - that case is still classified as retryable and handled by the retry loop, not the pre-flight check. - Pending order price distance is validated the same way as an open position's stop distance, against the current market rather than the pending price itself. Some brokers apply a slightly different rule for pending order distance - if in doubt, test against the target broker. - This header does not open, close, or select tickets on your behalf beyond what is needed to read the ticket it was asked to modify. - Waits between retries use Sleep(), which blocks the script/EA thread for that duration on both platforms. Fine for a script or a slow-moving EA loop; be aware of it in anything latency sensitive.
Script · MT4 · v1.0 · Free
About OrderModify Wrapper With Retries
OrderModify Wrapper With Retries is a script for MetaTrader 4. It has 2 settings, such as Max retries and Retry delay ms. MQL source code included.
Files in the download: SafeOrderModify.mqh, SafeOrderModify.txt, SafeOrderModifyDemo.mq4.
Settings
| Setting | Default | What it does |
|---|---|---|
| Max retries | 5 | Retry attempts before giving up |
| Retry delay ms | 500 | Milliseconds to wait between retries |
How to install
- Download the ZIP from your PolyMaria library and unzip it.
- In MetaTrader 4 open File → Open Data Folder, then go to MQL4 → Scripts.
- Copy the .mq4 file(s) there. Put any .mqh files in MQL4 → Include.
- Open the .mq4 file in MetaEditor and press Compile (F7) if MetaTrader does not show it.
- Drag the script onto a chart to run it once.
Tags: MT4 Script · MQL4 Source Code