Snap All Stops To Last Swing
A stop parked at a round number or an arbitrary pip count ignores the one thing the market has actually told you: where the last swing was. This script re-anchors every matching position's stop to structure instead - just beyond the most recent confirmed fractal low for a buy, or high for a sell - with a small buffer you choose. "Confirmed" is the operative word. A naive fractal search that includes the forming bar rewrites its answer on every tick as that bar's high or low keeps moving, which makes the stop jump around exactly when you need it stable. This script only considers a swing once the two bars on either side of it have closed - the standard five-bar fractal - so the level it finds is settled, not a moving target. By default the script only moves a stop when the swing is more protective than the one already in place. A swing that happens to sit further from the market than the current stop is not applied, because silently loosening risk is not what "snap to swing" should mean by default; turn InpOnlyTighten off if you specifically want the stop replaced regardless of direction. Every candidate stop is checked against the broker's minimum stop distance before being sent, and an existing stop already inside the freeze distance is left alone and reported rather than attempted and rejected. Multiple tickets on the same symbol share one swing lookup per run, so a book with several positions on the same pair does not repeat the bar scan for each one. Notes and limitations - If no confirmed fractal exists within InpSearchBars, that ticket is skipped and reported - the script never falls back to an arbitrary distance instead of a real swing. - The swing search runs once per symbol per script run, not continuously. Run the script again to pick up a newer swing that formed since the last run. - Pending orders are not touched. - A swing that is very far from the market produces a very wide stop. The script does not cap the distance - review the result before relying on it on a symbol prone to long, quiet stretches between swings. - With InpOnlyTighten on, a position without an existing stop always receives the swing stop, since there is nothing to compare against.
Script · MT5 · v1.0 · Free
About Snap All Stops To Last Swing
Snap All Stops To Last Swing is an expert advisor (trading robot) for MetaTrader 5. It has a news filter. It has 8 settings, such as Timeframe, Buffer pips and Only tighten. MQL source code included.
Files in the download: SnapStopsToSwing.ex5, SnapStopsToSwing.mq5, SnapStopsToSwing.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 |
|---|---|---|
| Timeframe | PERIOD_CURRENT | Timeframe to search for the swing on |
| Buffer pips | 3.0 | Extra distance beyond the swing, pips |
| Only tighten | true | Only move a stop if the swing is more protective than the current one |
| Current symbol only | false | Restrict to this chart's symbol |
| Magic filter | -1 | Only this magic (-1 means every magic) |
| Search bars | 200 | How many bars back to search for a confirmed fractal |
| Max retries | 5 | Attempts per position 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: Fractals · News Trading · MT5 Script · MQL5 Source Code