Mean-Reversion Teaching Strategy (Median/MAD)
Risk warning: This strategy only detects deviations from a recent statistical price regime. It does not estimate intrinsic value or guarantee that price will revert. Persistent trends, structural changes, limited liquidity, fees, and slippage can all cause losses. Parameters must be revalidated for each instrument and timeframe.
Strategy Rationale
The strategy separates mean reversion into two stages. When price first moves far away from its recent regime, the program only records the extreme. A trade becomes eligible only after the absolute deviation contracts for several completed bars and returns inside the confirmation zone.
The recent regime is the median of the previous N log closing prices. Dispersion is measured with the median absolute deviation (MAD):
text
dynamic center = median(log(close))
robust scale = 1.4826 × MAD
deviation z = (log(current close) - dynamic center) / robust scale
This is a statistical price reference. Material changes that cannot be quantified by the model must be assessed outside the strategy.
Observation and Entry
Reaching the extreme z threshold only arms the setup. An entry requires a configured number of consecutive contractions in |z|, a return inside the confirmation threshold, limited center drift, and enough distance to the frozen target to cover estimated round-trip costs.
A downside deviation followed by reentry opens a long position. An upside deviation followed by reentry opens a short position. All calculations use completed bars.
Position Management and Exit
The target center is frozen at entry so that a moving reference cannot follow a persistent trend and falsely report a completed reversion. The position exits when any of the following occurs:
- price reaches the frozen center or the remaining distance contracts to the configured ratio;
- deviation expands beyond the extreme recorded during observation;
- the live center drifts too far from the frozen target;
- the maximum holding time is reached;
- the fixed stop based on the fill price is triggered.
The strategy uses a fixed contract quantity and manages one instrument and one directional position. Confirmation count, observation timeout, center drift, holding timeout, cost assumptions, and cooldown are fixed teaching settings in the code so the parameter panel stays focused on the core mechanism.
text
idle → extreme deviation → observe reentry → confirmed entry
↓ ↓
reference invalid target / invalidation exit
Main Parameters
| Parameter | Default | Description |
|---|---|---|
| Lookback | 120 | Bars used for the log-price median and MAD |
| Extreme z threshold | 2.5 | Arms an observation setup |
| Reentry z threshold | 2.0 | Price must return inside this threshold |
| Order size | 1 contract | Fixed contract quantity |
| Leverage | 1 | Leverage used by the contract account |
| Fixed stop | 4% | Checked against the fill price; zero disables it |
Validation Status
- The JavaScript source passed
node --checksyntax validation. - Offline logic checks passed for odd and even medians, MAD deviation direction, robust-center behavior, the cost-distance filter, and the arm-confirm-enter-exit-cooldown state transitions.
- The importable FMZ XML was generated with six teaching parameters, and its embedded source was verified against the standalone JavaScript file.
- FMZ backtesting, paper trading, and live small-size validation have not been performed. The backtest dates and symbol stored in the XML are import settings, not reported results.
- 1