Half Life Timed Reversion System
Most "mean reversion" Expert Advisors trade a fixed-parameter z-score band and never ask whether the instrument is actually mean-reverting right now. This one estimates it directly. On every new bar it regresses the bar-to-bar price change against the price level over the lookback window - the standard discrete-time Ornstein-Uhlenbeck-to-AR(1) discretisation, dx[t] = a + b*x[t] - computed with plain double-precision sums, no external statistics library. The regression slope b maps to a mean-reversion speed theta = -ln(1+b), and from theta to a half-life in bars, ln(2)/theta. That half-life then drives three separate decisions: whether to trade at all (only inside a user-chosen tradeable range of half-lives), how far to place the stop (a multiple of the lookback window's standard deviation), and how long to hold (a small multiple of the half-life itself - if price has not reverted within roughly two to three half-lives, the position is closed regardless of profit or loss, because the statistical assumption behind the trade has expired). The naive version of this idea trades every window's z-score without checking whether the regression describes anything real. A trending or random-walking instrument, a numerically degenerate fit, or a fit with almost no explanatory power will all produce a b and an R-squared just like a genuinely mean-reverting one - the difference only shows up if you check. This EA checks: a non-negative slope, an undefined mean-reversion speed, an implausible half-life, and a weak R-squared each independently block the trade, with the reason printed to the Experts log and shown in the chart comment. Skipping quietly and trading anyway on a bad fit is exactly the failure mode this submission avoids. Notes and limitations RISK WARNING: this Expert Advisor opens real market positions with a real stop-loss on every trade. Mean reversion is a statistical tendency, not a guarantee - a genuine regime change (a trend replacing what had been a range) looks identical to ordinary noise in the fitted half-life until well after the fact, and the EA has no way to distinguish the two in advance. Test on a demo account before committing real funds, and never risk more than you can afford to lose. The regression refuses to trade, rather than trading anyway, in four cases: a non-negative slope (no mean reversion measured); an implied mean-reversion speed with no real solution (1+b <= 0); a half-life that is non-positive or longer than the entire lookback window (too slow to trust); and an R-squared below InpMinRSquared (fit too weak to trust even when the slope alone looks fine). Each is logged with its specific reason and shown in the chart comment. The EA holds at most one position at a time on its chart symbol, filtered by magic number - it will not open a second position while one is already open, and it never touches a position it did not open itself. The maximum-holding-time exit is tracked with a per-ticket terminal global variable (cleared once the position closes), because MQL4 order records carry no custom fields of their own. If the terminal's global variables are wiped externally between ticks, the EA falls back to leaving the time-based exit unenforced for that ticket until the position closes by its ordinary SL/TP. The OU equilibrium level used for the take-profit and the z-score is the regression-implied attractor (-a/b), not simply the arithmetic mean of the lookback window - the two are close whenever the fitted slope is small, but the regression-implied value is the one that is actually consistent with the fitted model. This is a single-instrument, single-timeframe system with no correlation or portfolio awareness. Running it on several charts at once trades each instrument independently with no shared risk budget.
Expert advisor · MT4 · v1.0 · Free
About Half Life Timed Reversion System
Half Life Timed Reversion System is an expert advisor (trading robot) for MetaTrader 4. It can size trades from your balance and risk percent. It has 14 settings, such as Lookback bars, Min r squared and Min half life bars. MQL source code included.
Files in the download: HalfLifeReversionSystem.ex4, HalfLifeReversionSystem.mq4, HalfLifeReversionSystem.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 |
|---|---|---|
| Lookback bars | 150 | Bars used to fit the AR(1) regression |
| Min r squared | 0.10 | Minimum regression R-squared required to trust the fit |
| Min half life bars | 3.0 | Minimum tradeable half-life, in bars |
| Max half life bars | 60.0 | Maximum tradeable half-life, in bars |
| Z score entry | 1.5 | Z-score distance from equilibrium required to enter |
| Stop z score distance | 1.0 | Extra stdev distance beyond entry used for the stop-loss |
| Max hold multiple | 2.5 | Maximum holding time, as a multiple of the measured half-life |
| 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 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: Risk & Lot Size · MT4 Expert Advisor · MQL4 Source Code