Loading…

Repair Wrong Side Pendings

A Buy Limit only makes sense below the market and a Sell Stop only makes sense below the market too, in the opposite direction; every pending type has exactly one side of price it is geometrically valid on. Normal ticks cannot break this - a Buy Limit fills the instant price reaches it - but a large gap, typically over a weekend or during a thin-liquidity spike, can jump straight past the level without filling the order, leaving it stranded on the side that makes no sense for its type. MQL4 gives OrderModify no way to change what an order's price is measured against. Once a pending order is on the wrong side, every modify attempt on it fails with error 130, permanently, because the order's shape is what is wrong, not any particular value being sent. There is no in-place fix - the only way to correct it is to delete the broken order and send a new one with the type the current geometry actually calls for: a Buy Limit stranded above the market becomes a Buy Stop, a Sell Stop stranded above the market becomes a Sell Limit, and so on. The buy or sell side never changes, only the trigger mechanism. The script lists every order that needs this before touching anything and asks for confirmation, since delete-then-recreate is not reversible if the second half fails. If a stop loss or take profit no longer fits the corrected type - it would sit on the wrong side of the new entry - that one level is dropped and reported rather than silently sent anyway; the rest of the order is still recreated. If the recreate step itself fails after the original was already deleted, that specific ticket is called out with an Alert, since it is now genuinely gone and needs a manual decision, not a queued retry that might repeat the same failure. Notes and limitations - This only repairs orders that are already geometrically invalid. An order that has simply drifted closer to or further from the market without becoming invalid is untouched - use RepricePendingsKeepDistance, a separate script in this catalogue, for that. - If the corrected type is itself now too close to the market - inside the broker's minimum distance - the ticket is skipped and reported rather than sent anyway. - Stop loss and take profit are only dropped when they no longer fit the corrected order geometrically or within the broker's minimum distance; otherwise they are carried across unchanged. - The replacement ticket is a new order number. Nothing in MetaTrader links it back to the original except the comment, which records the original ticket for an audit trail. - If a ticket is deleted successfully but the recreate fails, the exposure that order represented is gone until placed manually - the script raises an Alert specifically for this case so it cannot be missed in a busy journal.

Script · MT5 · v1.0 · Free

About Repair Wrong Side Pendings

Repair Wrong Side Pendings is an expert advisor (trading robot) for MetaTrader 5. It places pending buy/sell stop or limit orders. It has 5 settings, such as Current symbol only, Magic filter and Confirm first. MQL source code included.

Files in the download: RepairWrongSidePendings.ex5, RepairWrongSidePendings.mq5, RepairWrongSidePendings.txt.

Trading robots can lose money. Check every setting and test on a demo account before using real funds.

Settings

SettingDefaultWhat it does
Current symbol onlyfalseRestrict to this chart's symbol
Magic filter-1Only this magic (-1 means every magic)
Confirm firsttrueList repairs and ask for confirmation before acting
Max retries5Attempts per leg (delete or recreate) before giving up
Retry wait ms500Pause between attempts, milliseconds

How to install

  1. Download the ZIP from your PolyMaria library and unzip it.
  2. In MetaTrader 5 open File → Open Data Folder, then go to MQL5 → Experts.
  3. Copy the .mq5 file(s) there. Put any .mqh files in MQL5 → Include.
  4. Open the .mq5 file in MetaEditor and press Compile (F7) if MetaTrader does not show it.
  5. Drag the expert advisor from the Navigator onto a chart, check the settings, allow Algo/Auto Trading and press OK.
  6. Test it first on a demo account or in the Strategy Tester.

Download the PDF guide

Tags: Pending Orders · MT5 Script · MQL5 Source Code

MQL4 & MQL5 scripts · PolyMaria