Loading…

Attach Re Entry Order At Stop Level

A stop loss protects capital, but a stop hit by a shakeout - a brief spike through the level followed by the market resuming the original direction - also ends the trade at exactly the wrong moment. This script attaches a pending re-entry order at each matching position's own stop-loss level, so that if the stop-out happens and price later trades back through that level in the original direction, a new position opens automatically to catch the continuation. The mechanics are simple but the placement matters: the re-entry is a stop order, not a limit, and it sits just beyond the ORIGINAL stop-loss price on the triggering side - above it for a buy re-entry, below it for a sell re-entry. Only price trading back past that level fills it, which is what distinguishes a genuine recovery from the shakeout still being in progress. Placing the re-entry any closer would trigger while the shakeout is still underway, defeating the point. Each re-entry carries its own protective stop, placed the same distance behind its new entry as the trigger buffer, and an optional expiry so a re-entry that never triggers does not sit in the book forever. Running the script again on a book that already has re-entries attached does not stack duplicates - each pending order's comment records which original ticket it belongs to, and a position already covered is left alone. Notes and limitations - A position without a stop loss has nothing to build a re-entry from and is skipped outright - the script never invents a level. - The re-entry is attached to the position's stop-loss level at the moment the script runs. If the stop is moved afterwards - by hand, or by another script such as a trailing stop - the existing re-entry order is NOT moved with it; run this script again to relocate it, after removing the stale one if it has not triggered. - If the original position is closed normally (target hit, manually closed) rather than stopped out, the re-entry order is left in the book and can still trigger on its own, opening a fresh position unrelated to any existing exposure. Remove it by hand if that is not wanted. - Lot sizing when InpUseFixedLots is false copies the ORIGINAL position's current size, not any resized amount from a partial close. - This script only ever adds one re-entry order per source position per run; it does not build a multi-level ladder of re-entries.

Script · MT5 · v1.0 · Free

About Attach Re Entry Order At Stop Level

Attach Re Entry Order At Stop Level is an expert advisor (trading robot) for MetaTrader 5. It places pending buy/sell stop or limit orders. It has 9 settings, such as Buffer pips, Use fixed lots and Fixed lots. MQL source code included.

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

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

Settings

SettingDefaultWhat it does
Buffer pips3.0Extra distance beyond the stop-loss level, pips
Use fixed lotsfalseUse InpFixedLots instead of matching the original position's size
Fixed lots0.10Lot size used when InpUseFixedLots is true
Expiry hours0Pending order lifetime in hours (0 = good till cancelled)
Magic number20600Magic number tagged on the re-entry orders
Current symbol onlyfalseRestrict to this chart's symbol
Magic filter-1Only rebuild for positions carrying this magic (-1 means every magic)
Max retries5Attempts per order 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