Trailing Stop Engine
Most trailing stop code mixes two jobs together: deciding what the new stop should be, and sending it to the server. That coupling is why trailing logic is one of the hardest parts of an EA to test - the only way to see whether the decision was right is to watch it happen on a live or demo account, tick by tick. This header does only the first job. ComputeTrailingStop takes the position's direction, its open price, its current stop, the current Bid/Ask, and a chosen strategy, and returns whether the stop should move and to what. It never sends a trade request, selects an order, or touches anything that reaches the trade server - it has no side effects at all, which means it can be exercised with hand-picked synthetic prices, on any account, at any time, and the result is exactly what it would decide on a live chart with those same numbers. The caller is the one who sends the actual modify, using whatever retry and error handling their own EA already has. Four modes are supported through one ENUM_TRAIL_MODE input. TRAIL_FIXED_PIPS follows the price continuously at a constant pip distance. TRAIL_ATR_MULTIPLE follows it at a distance equal to a caller-supplied ATR value times a multiplier - the ATR itself is read by the caller, this header only does the arithmetic. TRAIL_MA_VALUE anchors the stop directly to a moving average level the caller passes in, for a trail that follows the average rather than a fixed offset. TRAIL_STEP reproduces the terminal's own native trailing behaviour: the stop does nothing until price is a trigger distance in profit, then it only moves again once price has advanced a further, separate step distance beyond where the stop last landed - deliberately NOT the same as TRAIL_FIXED_PIPS, which moves on every tick. Every mode funnels through the same two guards before a candidate is accepted: it must be an improvement over the current stop (a buy's stop only ever rises, a sell's only ever falls - this header will never loosen a stop), and it must respect the symbol's minimum stop distance, so a candidate the server would reject is rejected here first instead. Notes and limitations - This library computes, it does not trade. It has zero side effects by design - no trade calls, no order/position selection, no Print. The demo's own output is entirely from the demo script, not the library. - Bid and Ask are supplied by the caller, not read internally. This keeps the function pure and testable, but it also means a caller who forgets to refresh them before calling will trail against a stale price. - TRAIL_MA_VALUE trusts the caller's maValue completely. If the average is on the wrong side of price the stop-level guard will simply reject it as "too close" or worse, not report a specific "wrong side" error. - TRAIL_STEP's trigger and step distances are both measured in pips from the open price and from the last accepted stop respectively; it does not track partial progress between calls itself; the caller's currentSL is the only state carried between ticks, which is why passing the real current stop-loss value each time matters. - Neither version opens, closes, or aggregates positions, so there is no netting/hedging trap to fall into anywhere in this header.
Script · MT4 · v1.0 · Free
About Trailing Stop Engine
Trailing Stop Engine is a script for MetaTrader 4. MQL source code included.
Files in the download: TrailingEngine.mqh, TrailingEngine.txt, TrailingEngineDemo.mq4.
How to install
- Download the ZIP from your PolyMaria library and unzip it.
- In MetaTrader 4 open File → Open Data Folder, then go to MQL4 → Scripts.
- Copy the .mq4 file(s) there. Put any .mqh files in MQL4 → Include.
- Open the .mq4 file in MetaEditor and press Compile (F7) if MetaTrader does not show it.
- Drag the script onto a chart to run it once.
Tags: Moving Average · ATR · Trailing Stop & Break-even · MT4 Script · MQL4 Source Code