Trend Cycle Mode Switch System
Most single-strategy EAs assume the market stays in one character the whole time they are attached, which is why a trend-following system bleeds small losses in a range and a mean-reversion system gets run over the moment a real trend starts. This EA measures which state the market is actually in and switches its trading logic to match, rather than running one rule set everywhere and hoping. The regime detector is ADX. When ADX(InpAdxPeriod) is above InpAdxTrendThreshold the EA treats the market as trending; below InpAdxCycleThreshold it treats it as cycling. Between the two thresholds the EA holds whatever regime it was already in - this dead zone is deliberate. Without it, ADX drifting back and forth across a single line would flip the whole trading logic every few ticks, closing and reopening trades on noise rather than on a genuine change of character. ADX was chosen over a rolling efficiency-ratio calculation because it is a built-in, already-smoothed MetaTrader indicator that can be checked against the platform's own published formula, rather than hand-rolled array math whose correctness the downloader has to take on trust. In trend mode the entry signal is a fast/slow EMA crossover, with the initial stop and an ongoing trailing stop both built from a multiple of ATR rather than a fixed pip count, so the stop distance adapts to how much the instrument is actually moving. The trade closes on a reverse crossover. In cycle mode the entry signal is RSI reaching InpRsiOversold or InpRsiOverbought, with a fixed, tighter pip stop (cycle trades are lower conviction than trend trades) and a take-profit set at the opposite boundary of the recent Donchian-style high/low range - the logic being that a mean-reversion trade taken at an extreme is aiming to close the gap back toward the other side of the recent range, not to run indefinitely the way a trend trade does. The detail that decides whether a two-mode EA is trustworthy is what happens to an open trade the instant the regime changes underneath it. This EA closes it immediately. A trend trade's ATR trailing stop and a cycle trade's fixed take-profit are both built on assumptions about the market state that stop being true the moment the measured regime has flipped, so this EA does not try to keep managing an old-mode trade with new-mode logic - it closes the position and lets the new mode's own signal open a fresh, correctly-stopped trade on its own terms. The EA tracks which mode opened a given trade with two adjacent magic numbers (InpMagicNumber for trend trades, InpMagicNumber + 1 for cycle trades), so this decision survives an EA restart or terminal reconnect without needing any saved state. Notes and limitations This EA trades live money whenever it is attached with AutoTrading enabled. Test on a demo account first and understand both rule sets before risking real funds. Past behaviour of either mode is no guarantee of future results, and the ADX regime detector can be measured wrong when the market changes character faster than a 14-period average can react - a sharp trend can begin one or two bars before ADX confirms it, and a trend can exhaust itself one or two bars before ADX falls back into cycle territory. The EA manages at most one trend-mode trade and one cycle-mode trade at a time per symbol (never both at once, since only one regime is active at a time). It does not pyramid or scale into a position. Only market orders are used - no pending orders are placed by either mode. Lot sizing, price normalisation and stop-distance validation all read live broker data (MODE_LOTSTEP, MODE_MINLOT, MODE_MAXLOT, MODE_DIGITS, MODE_STOPLEVEL, MODE_FREEZELEVEL, MODE_TICKVALUE, MODE_TICKSIZE) rather than assuming any fixed figures, so behaviour adapts correctly across FX pairs, metals and indices with different digit counts and contract sizes. No contract size is ever hardcoded. Every trade opened by this EA carries a real stop loss. Cycle-mode trades also carry a real take-profit set at OrderSend time; trend-mode trades rely on the ATR trailing stop rather than a fixed target, so they can run further in a strong trend.
Expert advisor · MT5 · v1.0 · Free
About Trend Cycle Mode Switch System
Trend Cycle Mode Switch System is an expert advisor (trading robot) for MetaTrader 5 based on RSI and Moving Average. It increases the lot size after a losing trade (martingale), moves the stop loss behind price (trailing stop / break-even) and can size trades from your balance and risk percent. It has 20 settings, such as Adx period, Adx trend threshold and Adx cycle threshold. MQL source code included.
It also has a news filter.
Files in the download: TrendCycleModeSwitch.ex5, TrendCycleModeSwitch.mq5, TrendCycleModeSwitch.txt.
Trading robots can lose money. Check every setting and test on a demo account before using real funds.
Settings
| Setting | Default | What it does |
|---|---|---|
| Adx period | 14 | ADX period |
| Adx trend threshold | 25.0 | ADX above this = TREND regime |
| Adx cycle threshold | 20.0 | ADX below this = CYCLE regime (between = hold previous regime) |
| Fast ma period | 10 | Trend mode: fast MA period |
| Slow ma period | 30 | Trend mode: slow MA period |
| Ma method | MODE_EMA | Trend mode: MA method for both lines |
| Atr period | 14 | Trend mode: ATR period for stops |
| Atr stop multiplier | 2.5 | Trend mode: initial and trailing stop, in ATR multiples |
| Donchian period | 20 | Cycle mode: bars used for the recent high/low range |
| Rsi period | 14 | Cycle mode: RSI period |
| Rsi oversold | 30.0 | Cycle mode: RSI below this arms a buy at the range low |
| Rsi overbought | 70.0 | Cycle mode: RSI above this arms a sell at the range high |
| Cycle stop loss pips | 25 | Cycle mode: fixed stop loss, in pips (tighter than trend mode) |
| Use risk sizing | false | Size by percent risk instead of a fixed lot |
| Risk percent | 1.0 | Percent of equity risked per trade, when risk sizing is on |
| Fixed lots | 0.10 | Lot size when risk sizing is off, or as a fallback |
| Magic number | 20260802 | Magic number for trend-mode trades (cycle-mode uses this + 1) |
| Slippage pips | 3 | Slippage tolerance, in pips |
| Max retries | 5 | Attempts per trade action before giving up |
| Retry wait ms | 500 | Pause between attempts, milliseconds |
How to install
- Download the ZIP from your PolyMaria library and unzip it.
- In MetaTrader 5 open File → Open Data Folder, then go to MQL5 → Experts.
- Copy the .mq5 file(s) there. Put any .mqh files in MQL5 → Include.
- Open the .mq5 file in MetaEditor and press Compile (F7) if MetaTrader does not show it.
- Drag the expert advisor from the Navigator onto a chart, check the settings, allow Algo/Auto Trading and press OK.
- Test it first on a demo account or in the Strategy Tester.
Tags: RSI · ADX · Trend · Martingale · Trailing Stop & Break-even · Risk & Lot Size · News Trading · Moving Average · MT5 Expert Advisor · MQL5 Source Code