Loading…

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 · MT5 · v1.0 · Free

About Regime Switched Exit Manager

Regime Switched Exit Manager is an expert advisor (trading robot) for MetaTrader 5 based on ATR. It moves the stop loss behind price (trailing stop / break-even) and has a news filter. It has 11 settings, such as Atr period, Regime lookback and Low threshold. MQL source code included.

Files in the download: RegimeSwitchedExitManager.ex5, RegimeSwitchedExitManager.mq5, RegimeSwitchedExitManager.txt.

Trading robots can lose money. Check every setting and test on a demo account before using real funds.

Settings

SettingDefaultWhat it does
Atr period14ATR period used both for the regime read and target sizing
Regime lookback100Closed bars of ATR history used to rank the current regime
Low threshold0.33Percentile rank at/below which the regime is LOW
High threshold0.67Percentile rank at/above which the regime is HIGH
Low regime trail pips8.0LOW regime: trailing stop distance behind price, in pips
Normal target atr mult1.5NORMAL regime: profit target distance, as a multiple of ATR from entry
High regime max bars12HIGH regime: close the position after this many closed bars if still open
Magic number-1Magic number to manage; -1 manages every position on this symbol
Max retries5Order action attempts before giving up
Retry wait ms500Pause between retry attempts, milliseconds
Slippage pips3Slippage tolerance for the market close used by the exits, in pips

How to install

  1. Download the ZIP from your PolyMaria library and unzip it.
  2. In MetaTrader 5 open File → Open Data Folder, then go to MQL5 → Experts.
  3. Copy the .mq5 file(s) there. Put any .mqh files in MQL5 → Include.
  4. Open the .mq5 file in MetaEditor and press Compile (F7) if MetaTrader does not show it.
  5. Drag the expert advisor from the Navigator onto a chart, check the settings, allow Algo/Auto Trading and press OK.
  6. Test it first on a demo account or in the Strategy Tester.

Download the PDF guide

Tags: ATR · Buy/Sell Arrows · Trailing Stop & Break-even · News Trading · MT5 Expert Advisor · MQL5 Source Code

Expert advisors (EA) for MT4 & MT5 · PolyMaria