Reprice Pendings Keeping Distance
A pending order is placed at a distance from the market for a reason - a breakout level, a retracement zone, a buffer past a swing point. Left alone while the market drifts sideways over the following hours or days, the price the order sits at stays fixed but its meaning does not: a breakout order that was 40 pips away is now 10 pips away, no longer testing what it was built to test. This script solves that without needing a running EA. The first time it sees a pending ticket, it records the distance between the order's price and the market - the Ask for a buy-side order, the Bid for a sell-side one - as that ticket's baseline, stored in a global variable keyed by ticket number so it survives between runs. On every later run, it recalculates where the order would need to be to keep that exact baseline distance from the current market, and moves it there if it has drifted. Nothing happens on a timer. The script only acts when you run it, and the baseline itself only changes when InpResetBaseline is explicitly turned on for that run - otherwise a baseline recorded once stays the reference point indefinitely, however many times the script is run afterward. Stop loss and take profit move by the same amount as the order price by default, so the risk profile relative to the entry stays the same shape it always was. If keeping the baseline distance would put the order on the wrong side of the market for its type - which can happen after a large move - the script leaves it alone and says so, rather than sending a price the broker would reject. RepairWrongSidePendings, a separate script in this catalogue, is the tool for that specific situation. Notes and limitations - The baseline is stored in a terminal global variable named RPKD_<ticket>. It is removed automatically once that ticket is no longer a live pending order (filled, cancelled or expired). - Global variables persist for the terminal's configured expiry period, which is normally long enough to bridge days between runs, but a very old baseline can still be cleared by the terminal itself if the variable goes unused for long enough - re-run with InpResetBaseline if a baseline appears to be missing unexpectedly. - If the baseline distance now lands the order on the wrong side of the market for its type, the script skips it and reports why, rather than moving it somewhere invalid. - The script repositions the order; it does not detect or correct an order whose type has become geometrically wrong for its price - see RepairWrongSidePendings for that. - This acts once per run. It does not track the market continuously and does not trail the order tick by tick.
Script · MT5 · v1.0 · Free
About Reprice Pendings Keeping Distance
Reprice Pendings Keeping Distance is an expert advisor (trading robot) for MetaTrader 5. It places pending buy/sell stop or limit orders and has a news filter. It has 6 settings, such as Current symbol only, Magic filter and Reset baseline. MQL source code included.
Files in the download: RepricePendingsKeepDistance.ex5, RepricePendingsKeepDistance.mq5, RepricePendingsKeepDistance.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 |
|---|---|---|
| Current symbol only | false | Restrict to this chart's symbol |
| Magic filter | -1 | Only this magic (-1 means every magic) |
| Reset baseline | false | Capture the current distance as the new baseline instead of repricing |
| Shift stops | true | Shift stop loss / take profit by the same amount as the price |
| Max retries | 5 | Attempts per ticket before giving up |
| Retry wait ms | 500 | Pause between attempts, milliseconds |
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: Breakout · Pending Orders · News Trading · MT5 Script · MQL5 Source Code