Regime Switched Exit Manager
This is a trade manager, not a signal generator - it opens no positions, only manages ones already open. A single exit method applied across every volatility condition is the naive version of this problem: a trailing stop tight enough to make sense in a calm market gets shaken out constantly once volatility rises, and a fixed target sized for normal conditions sits absurdly close in a genuinely quiet market or absurdly far in a violent one. The regime read is a percentile RANK, not a raw ATR threshold, because a raw ATR number means nothing without context - 30 pips of ATR(14) is enormous on one pair and unremarkable on another, and even the same pair's own volatility drifts across sessions and years. This EA instead counts how many of the last InpRegimeLookback closed-bar ATR readings the current one beats or ties, expressed as a fraction from 0 to 1, and classifies the regime from that fraction against InpLowThreshold/InpHighThreshold - which stays meaningful on any symbol without per-instrument tuning. Each managed position then gets exactly one exit mechanism active at a time, chosen by the CURRENT regime as re-evaluated every closed bar: LOW regime - a tight trailing stop a fixed pip distance behind price, since quiet markets are prone to reversal and protecting a small gain matters more than giving it room. NORMAL regime - a fixed target at InpNormalTargetAtrMult times ATR from the entry price, a measured distance that makes sense when the market is moving in an ordinary, trending way. HIGH regime - a time-box: the position is closed outright after InpHighRegimeMaxBars closed bars if still open, because a genuinely unstable market makes both a fixed target and a trailing stop unreliable, and duration risk becomes the dominant concern. Notes and limitations - This EA never opens a position, only manages ones already open. - The regime is re-evaluated every closed bar, so a position can move between exit methods over its lifetime as volatility itself changes - this is deliberate, not a bug, and is the entire point of the design. - The LOW-regime trail only ever tightens; it never loosens an existing stop. - The NORMAL-regime target and HIGH-regime time-box both act with a market order, so slippage applies exactly as it would to any other market close. - Needs InpRegimeLookback + InpAtrPeriod + a small margin of closed bars of history before it produces its first regime read; until then it takes no action.
Expert advisor · MT4 · v1.0 · Free
About Regime Switched Exit Manager
Regime Switched Exit Manager is a script for MetaTrader 4 based on ATR. It can alert you with a pop-up, sound, email or push message. It has 11 settings, such as Atr period, Regime lookback and Low threshold. MQL source code included.
Files in the download: RegimeSwitchedExitManager.ex4, RegimeSwitchedExitManager.mq4, RegimeSwitchedExitManager.txt.
Settings
| Setting | Default | What it does |
|---|---|---|
| Atr period | 14 | ATR period used both for the regime read and target sizing |
| Regime lookback | 100 | Closed bars of ATR history used to rank the current regime |
| Low threshold | 0.33 | Percentile rank at/below which the regime is LOW |
| High threshold | 0.67 | Percentile rank at/above which the regime is HIGH |
| Low regime trail pips | 8.0 | LOW regime: trailing stop distance behind price, in pips |
| Normal target atr mult | 1.5 | NORMAL regime: profit target distance, as a multiple of ATR from entry |
| High regime max bars | 12 | HIGH regime: close the position after this many closed bars if still open |
| Magic number | -1 | Magic number to manage; -1 manages every position on this symbol |
| Max retries | 5 | Order action attempts before giving up |
| Retry wait ms | 500 | Pause between retry attempts, milliseconds |
| Slippage pips | 3 | Slippage tolerance for the market close used by the exits, in pips |
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: ATR · Buy/Sell Arrows · Trailing Stop & Break-even · Alerts · MT4 Expert Advisor · MQL4 Source Code