Universal Trailing Stop
Most trailing stop tools are bundled inside a strategy EA and only manage the positions that EA itself opened. This one is deliberately generic: attach it to any chart, set a magic number filter (or -1 for every magic, including manual trades with no magic number at all), and it manages whatever is already open, regardless of what opened it. Three trail methods are selectable from one enum input rather than three separate programs, because the position-matching, retry and stop/freeze-level logic is identical for all three - only the candidate stop calculation changes. TRAIL_PIPS trails a fixed distance behind price. TRAIL_ATR trails a multiple of Average True Range, which widens the stop automatically in a volatile market instead of getting stopped out on ordinary noise. TRAIL_MA trails to a moving average's current value directly, which is the classic "ride the trend line" approach. The ATR and moving average methods read the indicator at shift 1, the last completed bar, never shift 0. Reading a still-forming bar's indicator value mid-bar means the "current" value keeps changing as the bar builds, which is close enough to a repainting trap that this EA does not do it for any method - all three trail on the same once-per-bar cadence for one consistent, explainable behaviour, even though TRAIL_PIPS alone would be safe to evaluate every tick. The stop only ever tightens. A candidate level worse than the position's existing stop - which can happen when ATR spikes and then contracts, or a moving average curls back - is skipped, not applied. And every candidate is checked against the broker's stop and freeze level before it is sent; a position too close to the market for its broker to accept a new stop is reported by ticket in the log rather than failing silently. Notes and limitations - This EA never opens, closes or resizes a position. It only ever moves a stop loss, and only in the improving direction. - A position with no stop loss at all is trailed from its first qualifying candidate; there is no "existing stop" to compare against yet. - ATR and moving average values need enough history to be meaningful. On a freshly opened chart with too few bars, the candidate is skipped and logged until enough bars exist. - Turning InpTrailOncePerBar off makes TRAIL_PIPS react intra-bar (fine) but also makes TRAIL_ATR and TRAIL_MA re-read their indicator on every tick of the still-forming bar, which is not recommended - left on by default for a reason. - A FIFO or netting restriction is a server-side rule this EA does not second-guess; any modify the broker refuses is reported with its error code.
Expert advisor · MT5 · v1.0 · Free
About Universal Trailing Stop
Universal Trailing Stop is an expert advisor (trading robot) for MetaTrader 5 based on Moving Average and ATR. It moves the stop loss behind price (trailing stop / break-even). It has 13 settings, such as Trail method, Trail pips and Atr period. MQL source code included.
Files in the download: UniversalTrailingStop.ex5, UniversalTrailingStop.mq5, UniversalTrailingStop.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 |
|---|---|---|
| Trail method | TRAIL_PIPS | Trailing method |
| Trail pips | 20 | Trail distance in pips (TRAIL_PIPS) |
| Atr period | 14 | ATR period (TRAIL_ATR) |
| Atr multiple | 2.0 | ATR multiple (TRAIL_ATR) |
| Ma period | 20 | Moving average period (TRAIL_MA) |
| Ma method | MODE_EMA | Moving average method (TRAIL_MA) |
| Ma applied price | PRICE_CLOSE | MA applied price (TRAIL_MA) |
| Min profit pips | 0 | Only trail once position is this far in profit (pips) |
| Magic filter | -1 | Magic number filter (-1 = every magic) |
| Current symbol only | true | Restrict to this chart's symbol |
| Trail once per bar | true | Re-evaluate only on a new bar, not every tick |
| Max retries | 5 | Attempts per modify 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: Moving Average · ATR · Trailing Stop & Break-even · MT5 Expert Advisor · MQL5 Source Code