OCO Pending Pair
Start with the limitation, because it is the honest core of this submission: neither MT4 nor MT5 offers server-side one-cancels-the-other for retail pending orders, and a script stops existing the moment OnStart returns. It follows that NO script can cancel the losing order when the winning one fills - anything on this site that claims to is either secretly an expert advisor or does not do what it says. What a script can honestly do is everything up to that moment, and do it properly. That is what this one does. Both orders are validated completely before either is sent: type against side of market (a buy limit belongs below price, a buy stop above), distance against the broker's stop and freeze levels, prices snapped to the tick-size grid rather than merely rounded to digits, volumes checked against the lot rules with any adjustment reported. An impossible pair is refused whole, with the nearest legal price printed, so you are corrected rather than half-executed. If order A places and order B then fails, A is rolled back - a lone pending is a directional bet you never asked for. Stops and targets are set per order as pip distances from that order's own price. Both orders carry a shared OCO-<id> comment, and the id is printed with both tickets in the journal. The companion OCO Order Manager expert from this series watches for exactly these tags and cancels the survivor when one leg fills; without it, the tag still makes the pair easy to find and cancel by hand. The pair is not restricted to opposite directions - a buy limit at support plus a buy stop at breakout ("whichever comes first") is a legitimate OCO too. Notes and limitations - Nothing cancels the surviving order when one fills. That needs a resident program (see the OCO Order Manager expert) or your own hand. This is a platform limitation and no script can remove it. - The OCO-<id> tag lives in the order comment. Some brokers overwrite or truncate comments during partial fills and internal operations; the journal line with both tickets is the durable record. - The two orders are placed one after the other, not in one transaction. The rollback closes the gap when the second fails, but for a heartbeat the first order exists alone. - Margin is not reserved by pending orders; if both trigger and the account cannot carry the second fill, the server rejects it at trigger time. - Prices are validated against the market at run time. On a fast market a price that was legal in the dialog can be inside the minimum distance a second later; the retry loop reports exactly what the server said.
Script · MT5 · v1.0 · Free
About OCO Pending Pair
OCO Pending Pair is an expert advisor (trading robot) for MetaTrader 5. It places pending buy/sell stop or limit orders. It has 16 settings, such as Kind a, Price a and Lots a. MQL source code included.
Files in the download: OcoPendingPair.ex5, OcoPendingPair.mq5, OcoPendingPair.txt.
Trading robots can lose money. Check every setting and test on a demo account before using real funds.
Settings
| Setting | Default | What it does |
|---|---|---|
| Kind a | PEND_BUY_STOP | Order A: type |
| Price a | 0.0 | Order A: price (required) |
| Lots a | 0.10 | Order A: lots |
| Stop pips a | 0.0 | Order A: stop loss, pips (0 = none) |
| Take pips a | 0.0 | Order A: take profit, pips (0 = none) |
| Kind b | PEND_SELL_STOP | Order B: type |
| Price b | 0.0 | Order B: price (required) |
| Lots b | 0.10 | Order B: lots |
| Stop pips b | 0.0 | Order B: stop loss, pips (0 = none) |
| Take pips b | 0.0 | Order B: take profit, pips (0 = none) |
| Expiry | D'1970.01.01' | Expiry (leave 1970 = no expiry) |
| Oco id | 0 | OCO pair id (0 = generate) |
| Magic | 0 | Magic number |
| Max retries | 5 | Attempts before giving up |
| Retry wait ms | 500 | Pause between attempts, ms |
| Confirm first | true | Ask for confirmation first |
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: Pending Orders · MT5 Script · MQL5 Source Code