Type/to search

Mean-Reversion Teaching Strategy (Median/MAD)

Common strategy
Created: 2026-09-16 11:33:42
Last modified: in 8 hours
2
Follow
525
Followers

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

ParameterDefaultDescription
Lookback120Bars used for the log-price median and MAD
Extreme z threshold2.5Arms an observation setup
Reentry z threshold2.0Price must return inside this threshold
Order size1 contractFixed contract quantity
Leverage1Leverage used by the contract account
Fixed stop4%Checked against the fill price; zero disables it

Validation Status

  • The JavaScript source passed node --check syntax 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.
Source
JavaScript
/*
 * 合约均值回归教学策略(中位数 / MAD,FMZ JavaScript)
 *
 * 核心:
 * 1. 用前 N 根已结束 K 线的对数收盘价中位数表示近期价格常态;
 * 2. 用 MAD(中位数绝对偏差)衡量正常波动,并把当前偏离标准化为 z 值;
 * 3. 极端偏离只进入观察状态;偏离连续收窄并回到确认线内才开仓;
 * 4. 开仓时冻结目标中枢。价格回靠目标则退出,中枢漂移、偏离恢复或超时则认错。
 *
 * 这是一套价格统计回归策略。它不计算交易标的的内在价值,也不能证明价格必然回归。
 * 教学版管理单交易所、单品种、单方向合约持仓;下单数量单位为张。
 */
Strategy parameters
Strategy parameters
参照周期 (Optional)
极端偏离线 (Optional)
回靠确认线 (Optional)
每次下单张数 (Optional)
杠杆倍数 (Optional)
固定止损(%) (Optional)
Comment
All comments (0)
No data
No data
  • 1
Forums
PINE Language
Get the app
iPhone Download
© 2015 - ∞ INVENTOR PTE LTD (SG)