Trailing Entry Stop System
A trailing stop on an open position follows price in the direction of profit and closes the trade the moment price reverses onto it. This system reuses that exact mechanic before any position exists. Once price makes a fresh N-bar high or low, the EA arms a trailing entry level a fixed pip distance behind the extreme. As price keeps extending in the breakout direction, the level tightens toward it - upward behind a rising high, downward above a falling low - never loosening back away from price, exactly like a real trailing stop. Only when price reverses and touches that level does the EA send a market order in the breakout direction, with a fixed stop loss and take profit attached. The trap this avoids is treating "price crossed a fixed level" as if it were trailing behaviour. A naive version would just draw one static line at breakout-plus-distance and fire the instant price touched it - indistinguishable from an ordinary breakout entry with no trailing involved at all, and prone to firing on the very first pullback tick before the setup has proven anything. This EA instead counts how many times the level has actually tightened since it was armed, and refuses to fire until at least one genuine trail update has happened. That guarantees price extended further after the breakout, retreated, and only then touched a level that had moved - a real breakout-confirmation entry, not a bare price cross. Tracking is synthetic: no BUY STOP or SELL STOP order rests with the broker while the EA watches. A resting pending order was considered and rejected for this design, because the touch condition needs a watermark and a trail-update counter that a plain pending-order price cross cannot express, and a synthetic watch consumes no margin or pending-order slot while nothing has happened yet. The trade-off, stated plainly, is that the watch state lives only in the EA's memory - restarting the terminal or the EA mid-watch loses an unfilled setup and starts fresh, whereas a broker-side pending order would have survived a restart. For a breakout-confirmation entry that only ever watches, never risks capital until it fires, that trade-off favours the simpler, margin-free design. Only one entry-stop cycle is tracked per symbol and magic number at a time. While a position from this EA is open, no new watch is armed; the moment that position closes, the watch resets cleanly and the EA starts looking for the next breakout from scratch. A stale, never-touched watch is also cancelled automatically after InpMaxWatchBars bars, so an old level from a breakout that never came back does not sit stale on the chart forever. Notes and limitations This is a single-position system: it does not pyramid, and it will not arm a new watch while a position from this EA is already open on the symbol. The watch state is memory-only and is lost on an EA or terminal restart mid-watch - a partially-trailed setup simply starts over rather than resuming, which is the honest cost of the synthetic-tracking design chosen over a broker-side pending order. The breakout detection uses simple N-bar highs/lows, not a volatility filter, so very quiet markets can arm shallow, easily-touched watches; widen InpLookbackBars or InpTrailDistancePips to filter that out. As with any breakout-based entry, false breakouts and choppy ranges will produce losing trades - the trail-update requirement reduces but does not eliminate whipsaw risk.
Expert advisor · MT5 · v1.0 · Free
About Trailing Entry Stop System
Trailing Entry Stop System is an expert advisor (trading robot) for MetaTrader 5. It moves the stop loss behind price (trailing stop / break-even), can size trades from your balance and risk percent and trades breakouts. It has 12 settings, such as Lookback bars, Trail distance pips and Max watch bars. MQL source code included.
Files in the download: TrailingEntryStopSystem.ex5, TrailingEntryStopSystem.mq5, TrailingEntryStopSystem.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 |
|---|---|---|
| Lookback bars | 20 | Bars used to define a fresh breakout high/low |
| Trail distance pips | 15 | Distance the entry level trails behind price, in pips |
| Max watch bars | 150 | Bars to keep an unfilled watch alive before cancelling it (0 = never) |
| Stop loss pips | 40 | Stop loss distance from entry, in pips |
| Take profit pips | 80 | Take profit distance from entry, in pips (0 = none) |
| Use risk sizing | false | Size by percent risk instead of a fixed lot |
| Risk percent | 1.0 | Percent of equity risked per trade, when risk sizing is on |
| Fixed lots | 0.10 | Lot size when risk sizing is off, or as a fallback |
| Magic number | 20260802 | Magic number for orders opened by this EA |
| Slippage pips | 3 | Slippage tolerance, in pips |
| Max retries | 5 | Attempts per trade action 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 · Trailing Stop & Break-even · Risk & Lot Size · MT5 Expert Advisor · MQL5 Source Code