Watch how to download trading robots for free
Find us on Facebook!
Join our fan page
Interesting script?
So post a link to it -
let others appraise it
You liked the script? Try it in the MetaTrader 5 terminal
Experts

Trailing Stop Manager - ATR, Chandelier, swing, SAR, MA, break-even with costs - expert for MetaTrader 5

Muhammad Muqeem Korejo
Muhammad Muqeem Korejo
Hi, I'm Muhammad Muqeem Korejo, an MQL4/MQL5 developer from Pakistan with 1.5 years of trading experience.
I build and fix trading tools for MetaTrader 4 and MetaTrader 5:
• Expert Advisors (EAs) built from your strategy rules
• Custom indicators, alerts and on-chart panels
Views:
82
Rating:
(1)
Published:
MQL5 Freelance Need a robot or indicator based on this code? Order it on Freelance Go to Freelance

A trailing stop sounds simple, but many trailing EAs cause trouble: they send a new stop on every tick (the broker answers "too many requests" or switches off automated trading for the account), they are refused with "invalid stops" because they ignore the stop level of the broker, they place the stop of a sell position inside the spread, or they forget what they were doing after a restart. This EA is written to avoid all of that.

Trailing Stop Manager in the visual Strategy Tester, EURUSD H1

Trailing methods (one at a time)

  • Fixed distance from the price (best with distances in points: in ATR multiples it is the same as the ATR method).
  • ATR distance: price minus ATR x k for a buy (default 2.5 x ATR(14)), so the distance follows the volatility.
  • Chandelier exit: the highest high since the entry minus 3 x ATR(22) for a buy, the lowest low since the entry plus 3 x ATR(22) for a sell. It is read from the bars since the position was opened, so a restart does not reset it.
  • Swing: below the last confirmed swing low (a low with 3 higher bars on each side, by default) for a buy, above the last swing high for a sell, plus a buffer.
  • Previous candle: below the low of the last closed candle (above its high for a sell), plus a buffer.
  • Parabolic SAR: the SAR value, only while it is on the right side of the price.
  • Moving average: the MA minus a buffer (default EMA 20).
  • Break-even only: no trailing.

All distances (distance, buffer, start, step, break-even) are in points or in multiples of ATR, chosen with one input. With ATR multiples the same settings fit EURUSD, gold and indices.

Break-even that is really break-even

When the profit reaches the trigger (default 1 x ATR), the stop is moved to the entry plus a small lock. With "Add commission and swap" on, the EA reads the commission of the entry deal from the history (counted twice, for the entry and the exit) and the swap of the position, and moves the stop further by the price distance that pays them back. A stop at the entry price alone still closes the trade at a loss on a commission account.

Rules that protect your account

  • A stop is only moved in the direction of profit, and only when the new stop is better by at least one step (default 0.1 x ATR). It is never moved back.
  • The stop of a buy is checked against Bid, the stop of a sell against Ask, because those are the prices that trigger them.
  • The stop level and the freeze level of the symbol are read on every pass (they change during news) plus 2 points of margin. A stop that is too close is moved only as far as allowed. If the price is inside the freeze level of the stop or the take profit, nothing is sent, because the server would refuse it.
  • Nothing is sent while the market of the symbol is closed (weekend, daily break), read from the trade sessions of the symbol.
  • At most one change per position every 5 seconds, a daily limit (500 by default), and a 60 second pause after the answer "too many requests".
  • Spread guard: no change while the spread is more than 3 times its recent average (rollover, news), so a stop is not pulled tight on a wide spread.
  • The take profit is kept as it is.

Which positions

Manual trades only (magic number 0), one magic number, or all positions; the chart symbol only or all symbols (positions of other symbols are checked once per second by a timer). Works on hedging and netting accounts. It keeps no state of its own: everything is read from the positions, the history and the bars, so a restart of the terminal or a change of the chart changes nothing.

Panel and log

A small panel shows the method, the number of positions managed, the changes of the day and the last action. Every change is written to the Experts log, for example "#123 buy EURUSD SL 1.08000 -> 1.08120 (ATR distance)", and so is every refusal with its return code.

Strategy Tester

In the Strategy Tester only (never on a live or demo chart) the EA opens small demo trades every 10 bars (minimum lot, in the direction of a 50-bar slope), so the trailing can be seen and tested. Switch it off with the last input.

Inputs: positions to manage, magic number, all symbols, method, timeframe, units, distance, buffer, start, step, ATR period, Chandelier period and multiple, swing strength and lookback, SAR step and maximum, MA period and method, break-even on/off, trigger, lock, costs, seconds between changes, daily limit, spread guard, extra points, panel, tester demo trades.

How it was tested

  • 15 Strategy Tester runs: EURUSD H1, XAUUSD D1, GBPUSD M30 and EURUSD M1, every method on EURUSD H1 (June to October 2026), fixed distances in points on XAUUSD M15, the Chandelier exit on GBPUSD M5, and a stress run with a 3-point distance on EURUSD M15 (every stop runs into the stop level of the broker). A script read every stop change from the logs: 4,117 changes, none moved a stop back, none was refused by the server, no errors.
  • The first version sent changes while the market was closed (weekend and the daily break of gold) and was refused with "market closed". It now checks the trade sessions first.
  • The screenshot is a real visual Strategy Tester run of a demo account.

The EA manages stops only. It does not decide entries and cannot make a losing strategy profitable.

EventTracer EventTracer

An Expert Advisor for MetaTrader 5 that prints one line each time one of its event handlers runs, on a chart, in the Strategy Tester and during an optimization. It never trades.

Volume Profile with Naked POC - day, week, month or year profiles built from M1 bars Volume Profile with Naked POC - day, week, month or year profiles built from M1 bars

Volume profiles per day, week, month or year with POC, value area high and low (classic two-rows method), naked POC lines that stay until price trades through them, and a developing POC. Built from M1 bars. 8 buffers for EAs.

Rollover Cost Table - swap mode from the symbol, triple-swap day from the broker Rollover Cost Table - swap mode from the symbol, triple-swap day from the broker

An MQL5 indicator that draws a table on the chart with one row per open position and shows what one more night of holding it costs, in the deposit currency where the symbol's swap mode allows it. The swap mode and the two rates come from the symbol's own specification, and so does the triple-swap weekday; no rate is typed into the code. Where the symbol charges in points, a horizon column turns that night into the number of normal nights needed to repay one round turn, and a figure the terminal cannot supply is a dash with a reason, never a zero.

MACD Signals MACD Signals

Indicator edition for new platform.