Set All Stops To ATR Distance
A fixed pip stop means one thing on a quiet range day and something very different during a volatility spike - the same 30 pips can be inside normal noise on one day and outside it on another. This script replaces that with a stop measured in the symbol's own recent average true range, which widens and narrows with conditions automatically. The ATR is read from the last fully closed bar on the chosen timeframe, never the forming one, so the distance does not shift mid-calculation while the current bar is still building. The stop is placed at InpAtrMultiple times that ATR from the live market - the Bid for a buy, the Ask for a sell - which re-anchors an existing position's stop to today's volatility rather than recreating whatever stop the trade happened to be opened with. By default the script moves every matching stop unconditionally, because re-anchoring to current volatility is the whole point and a stale ATR is not something to compare against defensively. Set InpOnlyTighten if you want the safer behaviour of only accepting the ATR distance when it protects more than the stop already in place. Every candidate stop is validated against the broker's stop and freeze distance before being sent, and one ATR reading is cached per symbol so a book with several tickets on the same pair does not repeat the indicator call for each one. Notes and limitations - The distance is measured from the current market price, not the entry price. Running the script repeatedly as price moves in your favour trails the stop; running it against you widens it, which is the honest consequence of anchoring to the live market rather than the entry. - With the default InpOnlyTighten = false, the script can loosen a stop that was previously tighter, if the current ATR distance happens to be wider. Set InpOnlyTighten = true if that behaviour is not wanted. - A symbol without enough history for the chosen period/timeframe is skipped and reported, never given a fallback distance. - Pending orders are not touched. - No account for trend direction or trade age - this is a pure volatility measure, not a trailing-stop strategy.
Script · MT5 · v1.0 · Free
About Set All Stops To ATR Distance
Set All Stops To ATR Distance is an expert advisor (trading robot) for MetaTrader 5 based on ATR. It has a news filter. It has 8 settings, such as Timeframe, Atr period and Atr multiple. MQL source code included.
Files in the download: SetStopsToAtrDistance.ex5, SetStopsToAtrDistance.mq5, SetStopsToAtrDistance.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 the ATR is measured on |
| Atr period | 14 | ATR averaging period |
| Atr multiple | 2.0 | Stop distance as a multiple of ATR |
| Only tighten | false | Only move a stop if the ATR distance 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) |
| 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: ATR · News Trading · MT5 Script · MQL5 Source Code