Loading…

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

SettingDefaultWhat it does
Lookback bars20Bars used to define a fresh breakout high/low
Trail distance pips15Distance the entry level trails behind price, in pips
Max watch bars150Bars to keep an unfilled watch alive before cancelling it (0 = never)
Stop loss pips40Stop loss distance from entry, in pips
Take profit pips80Take profit distance from entry, in pips (0 = none)
Use risk sizingfalseSize by percent risk instead of a fixed lot
Risk percent1.0Percent of equity risked per trade, when risk sizing is on
Fixed lots0.10Lot size when risk sizing is off, or as a fallback
Magic number20260802Magic number for orders opened by this EA
Slippage pips3Slippage tolerance, in pips
Max retries5Attempts per trade action 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: Breakout · Trailing Stop & Break-even · Risk & Lot Size · MT5 Expert Advisor · MQL5 Source Code

Expert advisors (EA) for MT4 & MT5 · PolyMaria