MFE Giveback Guard
A fixed trailing stop approximates giveback badly at both ends: wide enough to let a small winner ride comfortably, and it lets a large winner surrender far more than it should before closing; tight enough to protect the large winner properly, and it clips the small winner's normal wiggle room long before it has a chance to develop. This EA tracks each position's maximum favourable excursion (MFE) - the best profit, in pips, it has ever shown - and closes it once current profit falls to InpGivebackPct percent of that peak, so the exit distance scales with how far the trade actually ran rather than sitting at one fixed number. The rule only arms once a trade has reached InpMinMfePips of profit, so a trade still near breakeven is not closed on its first small pullback. Once armed, giveback is judged purely against the trade's own peak, which is persisted per ticket in a terminal global variable rather than kept only in memory - a chart reload or EA restart does not forget how high a trade had climbed and briefly treat a battle-tested winner as fresh. Notes and limitations - If InpUseGlobalVars is off and the terminal or EA restarts mid-trade, the peak resets to whatever profit the trade shows at that moment, which can understate a genuinely larger prior peak. - This EA only ever closes a trade outright; it never partially closes or adjusts a stop or target. - Persisted global variables are named with an "MfeGivebackGuard_" prefix and cleaned up automatically once their ticket closes, so they do not accumulate indefinitely. - Uses live Bid/Ask to judge profit, so both the peak and the giveback comparison are read against real-time price. - This EA never opens a new position; it only manages trades already open that match its filters.
Expert advisor · MT4 · v1.0 · Free
About MFE Giveback Guard
MFE Giveback Guard is a script for MetaTrader 4. It can alert you with a pop-up, sound, email or push message. It has 9 settings, such as Min mfe pips, Giveback pct and Magic filter. MQL source code included.
Files in the download: MfeGivebackGuard.ex4, MfeGivebackGuard.mq4, MfeGivebackGuard.txt.
Settings
| Setting | Default | What it does |
|---|---|---|
| Min mfe pips | 15 | MFE must reach at least this many pips before the rule arms |
| Giveback pct | 50 | Percent of peak profit surrendered that triggers a close |
| Magic filter | -1 | Magic number filter (-1 = every magic) |
| Current symbol only | true | Restrict to this chart's symbol |
| Use global vars | true | Persist each ticket's MFE across restarts via global variables |
| Slippage points | 30 | Slippage tolerance for the guard exit |
| Max retries | 5 | OrderClose attempts before giving up |
| Retry wait ms | 500 | Pause between attempts, milliseconds |
| Check interval sec | 1 | Seconds between guard scans |
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