Equity Trailing Stop
A per-position trailing stop protects one trade. It does nothing for an account that is up overall but where the winning position closed early and a new losing one opened afterwards. This EA trails the account instead: once equity has grown by InpActivationPercent from the equity recorded when it was attached, trailing arms and a floor is set InpTrailPercent below the highest equity seen since. The floor only ever rises with a new peak - it never moves down - and if equity falls back to touch it, the EA closes every open position on the account, on every symbol, because equity is an account-wide number and a floor breach is not a single symbol's problem. Two decisions matter beyond the basic mechanism. First, the floor tracks equity, not balance, so it reacts to unrealised losses while they are still open rather than waiting for them to be booked - the harder, earlier trigger. Second, once it arms, it does not rearm from a fresh baseline unless a trip has actually happened; growth above the activation level does not reset the starting point. The state that makes trailing continuous - baseline equity, peak equity, and whether it has armed - normally lives in memory only, which means an EA restart (a crash, a terminal update, a VPS reboot, even reattaching the EA to apply a parameter change) forgets it and starts over from whatever equity exists at that moment. InpPersistViaGlobalVar switches to storing that state in terminal global variables keyed by account number instead, so a restart picks the trailing back up from where it left off. Read the notes below before relying on this for real capital - persistence here is partial, not complete. Notes and limitations - Persistence is partial. InpPersistViaGlobalVar saves baseline equity, peak equity and the armed flag - it does not save whether the breaker has tripped. If the EA restarts shortly after a trip, it reloads the prior peak and armed state, sees current equity still at or below the trailing floor, and typically trips again on the very next tick rather than resuming in a disabled state. In practice this means a restart cannot be used to quietly bypass InpDisableAfterTrip. - Without persistence, any restart - including simply reattaching the EA to change an input - rebases both baseline and peak to whatever equity exists at that instant and clears the armed flag. Protection that was armed and trailing well above the original entry level is silently reset to zero and has to grow InpActivationPercent again before it re-arms. On a VPS that reboots unexpectedly mid-session, this can leave an account unprotected for longer than the operator realises. - Terminal global variables are local to this terminal installation only and are not shared with another terminal, another PC, or a different account number. MetaTrader also deletes global variables that go unused for a period (four weeks by default) - not a concern for an EA left running, but relevant if the chart sits idle between sessions. - The equity read and floor check run on every tick, not on a new bar, since equity itself moves with every quote on every open position. - If AutoTrading is off when the floor is hit, the EA cannot flatten the account. It logs and comments this plainly rather than pretending the account is protected. - This EA only ever closes; it never opens a position on its own.
Expert advisor · MT4 · v1.0 · Free
About Equity Trailing Stop
Equity Trailing Stop is a script for MetaTrader 4. It can alert you with a pop-up, sound, email or push message. It has 8 settings, such as Activation percent, Trail percent and Disable after trip. MQL source code included.
Files in the download: EquityTrailingStop.ex4, EquityTrailingStop.mq4, EquityTrailingStop.txt.
Settings
| Setting | Default | What it does |
|---|---|---|
| Activation percent | 5.0 | Equity growth % (from start) before trailing arms |
| Trail percent | 3.0 | Floor distance below peak equity, in % |
| Disable after trip | true | Block further trading for the rest of this session after a trip |
| Persist via global var | false | Survive EA/terminal restart using global variables |
| Slippage pips | 3 | Slippage tolerance when flattening, in pips |
| Max retries | 5 | Close attempts per order before giving up |
| Retry wait ms | 500 | Pause between retry attempts, milliseconds |
| Status log seconds | 60 | How often the status line repeats in the journal |
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: Trailing Stop & Break-even · Alerts · MT4 Expert Advisor · MQL4 Source Code