Autocorrelation Mode Switch System
Most single-rule systems assume the market is either trending or mean reverting for the whole time they are attached, when in reality that character changes. Lag-1 autocorrelation of bar-to-bar returns is a standard, genuinely computable statistic for which state the market is currently in: a positive reading means a bar's return tends to be followed by a same-signed return (momentum), a negative reading means it tends to be followed by an opposite-signed return (reversion). This EA recomputes that autocorrelation over a rolling InpAcfWindow-bar window on every closed bar - not once at startup - and trades whichever rule the current sign implies, provided |autocorrelation| clears InpMinAbsAcf. Below that threshold the reading is treated as noise and the EA stays flat rather than guessing. Because the regime can flip while a position is open, an open position is always closed the instant the active mode changes, before any new signal is considered. A momentum-mode position being managed by reversion-mode logic (or vice versa) would not make sense - the reason the trade was opened no longer holds once the underlying statistic flips sign. Both versions compute the autocorrelation the same way: a single bulk read of the last InpAcfWindow+3 closes, then one pass of mean/variance arithmetic over that array. Nothing in this indicator is computed per lag from a live indicator call, so there is no repeated handle lookup to slow it down on either platform. Notes and limitations - Autocorrelation is a statistical tendency measured over a finite window, not a guarantee about the next bar. A short window reacts fast but is noisier; a long window is smoother but slower to reflect a genuine regime change. - One position at a time, tracked by InpMagicNumber, and it is always flattened on a mode flip regardless of open profit or loss. - Evaluated once per closed bar, not on every tick. - No news, spread or session filter - add one if the raw signal trades through conditions you would rather avoid. - On MQL5 netting accounts, the EA has no guard against a foreign position already open on the same symbol; see the differences section above before running it alongside other EAs or manual trading on the same symbol.
Expert advisor · MT4 · v1.0 · Free
About Autocorrelation Mode Switch System
Autocorrelation Mode Switch System is an expert advisor (trading robot) for MetaTrader 4. It has 9 settings, such as Acf window, Min abs acf and Stop loss pips. MQL source code included.
Files in the download: AutocorrelationModeSwitch.ex4, AutocorrelationModeSwitch.mq4, AutocorrelationModeSwitch.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 |
|---|---|---|
| Acf window | 60 | Bars used to measure rolling lag-1 autocorrelation |
| Min abs acf | 0.10 | Minimum |autocorrelation| required to trade at all (else flat) |
| Stop loss pips | 40 | Stop loss distance, in pips (0 = none) |
| Take profit pips | 0 | Take profit distance, in pips (0 = none) |
| Fixed lots | 0.10 | Fixed lot size |
| Magic number | 20260360 | Magic number for orders opened by this EA |
| 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 4 open File → Open Data Folder, then go to MQL4 → Experts.
- 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 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: Momentum · Trend · MT4 Expert Advisor · MQL4 Source Code