skills/mean-reversion/SKILL.md
Mean-reversion strategy tools including Hurst exponent, half-life estimation, z-score signals, ADF testing, and Ornstein-Uhlenbeck modeling
npx skillsauth add agiprolabs/claude-trading-skills mean-reversionInstall this skill globally with one command. Works with Claude Code, Cursor, and Windsurf.
3 of 9 scanners reported clean
Some scanners were skipped, did not run, or reported a non-clean status. Review each row below.
Mean reversion is the statistical tendency for prices, spreads, or other financial variables to return toward a long-run average after deviating from it. A mean-reverting series overshoots its mean, then corrects back -- creating predictable oscillations that can be traded.
Before trading mean reversion, you must statistically confirm the series is mean-reverting. Three complementary tests:
Tests the null hypothesis that a series has a unit root (non-stationary).
from scipy import stats
import numpy as np
def adf_test(series: np.ndarray, max_lag: int = 0) -> dict:
"""Run ADF test. Reject null (p < 0.05) → stationary → mean-reverting."""
# See references/statistical_tests.md for full implementation
# Use statsmodels.tsa.stattools.adfuller for production
pass
Measures the long-range dependence of a time series.
| Hurst Value | Interpretation | Trading Implication | |-------------|---------------|---------------------| | H < 0.5 | Mean-reverting | Trade mean reversion | | H = 0.5 | Random walk | No edge | | H > 0.5 | Trending | Trade momentum |
def hurst_exponent(series: np.ndarray) -> float:
"""Compute Hurst exponent via R/S method. H < 0.5 → mean-reverting."""
# See references/statistical_tests.md for full R/S algorithm
pass
Compares variance of multi-period returns to single-period variance.
def variance_ratio(series: np.ndarray, q: int = 5) -> float:
"""Compute variance ratio at horizon q. VR < 1 → mean-reverting."""
returns = np.diff(np.log(series))
var_1 = np.var(returns)
returns_q = np.diff(np.log(series[::q]))
var_q = np.var(returns_q)
return var_q / (q * var_1)
See references/statistical_tests.md for complete implementations and interpretation guides.
The half-life tells you how many periods it takes for a deviation to decay to half its size. This is the single most important parameter for mean-reversion trading.
Fit the autoregressive model: delta_X_t = alpha + beta * X_{t-1} + epsilon
def half_life(series: np.ndarray) -> float:
"""Estimate mean-reversion half-life from AR(1) regression.
Returns:
Half-life in periods. Negative means non-mean-reverting.
"""
y = np.diff(series)
x = series[:-1]
x = np.column_stack([np.ones(len(x)), x])
beta = np.linalg.lstsq(x, y, rcond=None)[0][1]
if beta >= 0:
return -1.0 # Not mean-reverting
return -np.log(2) / np.log(1 + beta)
| Parameter | Rule of Thumb | |-----------|--------------| | Lookback window | 2x half-life | | Holding period | 1x half-life | | Maximum hold | 3x half-life (stop) | | Signal recalc | 0.5x half-life |
The z-score normalizes the deviation from the mean, providing standardized entry/exit signals.
z = (price - rolling_mean) / rolling_std
| Condition | Signal | Action | |-----------|--------|--------| | z < -2.0 | Buy | Enter long (price below mean) | | z > +2.0 | Sell | Enter short (price above mean) | | z crosses 0 | Exit | Close position (returned to mean) | | abs(z) > 3.0 | Stop | Close position (reversion failed) |
Set the rolling window to approximately 2x the half-life:
def z_score_signals(
prices: np.ndarray,
lookback: int,
entry_z: float = 2.0,
exit_z: float = 0.0,
stop_z: float = 3.0,
) -> np.ndarray:
"""Generate z-score-based mean-reversion signals.
Returns:
Array of signals: 1 (long), -1 (short), 0 (flat).
"""
rolling_mean = pd.Series(prices).rolling(lookback).mean().values
rolling_std = pd.Series(prices).rolling(lookback).std().values
z = (prices - rolling_mean) / rolling_std
# See scripts/mean_reversion_test.py for full signal generation
...
Scale position size with z-score magnitude for better risk-adjusted returns:
size = base_size * min(abs(z) / entry_threshold, max_scale)
See references/strategy_design.md for complete entry/exit framework and sizing.
The OU process is the continuous-time model of mean reversion:
dX = theta * (mu - X) * dt + sigma * dW
| Parameter | Meaning | Estimation | |-----------|---------|------------| | theta | Speed of mean reversion | From AR(1) beta: theta = -ln(1+beta)/dt | | mu | Long-run mean | From AR(1) intercept: mu = -alpha/beta | | sigma | Volatility of innovations | Residual std from AR(1) |
def estimate_ou_params(series: np.ndarray, dt: float = 1.0) -> dict:
"""Estimate OU process parameters from observed series.
Returns:
Dict with keys: theta, mu, sigma, half_life.
"""
y = np.diff(series)
x = series[:-1]
x_with_const = np.column_stack([np.ones(len(x)), x])
params = np.linalg.lstsq(x_with_const, y, rcond=None)[0]
alpha, beta = params[0], params[1]
theta = -np.log(1 + beta) / dt
mu = -alpha / beta if beta != 0 else np.mean(series)
residuals = y - (alpha + beta * x)
sigma = np.std(residuals) * np.sqrt(2 * theta / (1 - np.exp(-2 * theta * dt)))
return {
"theta": theta,
"mu": mu,
"sigma": sigma,
"half_life": np.log(2) / theta if theta > 0 else -1,
}
Apply z-score framework directly to a token's price series. Works best on:
Trade the spread between two cointegrated assets:
cointegration-analysis skill)S = Y - beta * XMulti-asset extension of pairs trading:
regime-detection skill to only trade mean reversion in ranging regimes.| Skill | Integration |
|-------|------------|
| cointegration-analysis | Find cointegrated pairs for pairs trading |
| pandas-ta | RSI, Bollinger Bands as mean-reversion indicators |
| regime-detection | Filter: only trade MR in ranging regimes |
| vectorbt | Backtest mean-reversion strategies |
| volatility-modeling | Estimate sigma for OU model |
| slippage-modeling | Factor execution costs into P&L estimates |
| position-sizing | Size positions using Kelly + z-score scaling |
references/statistical_tests.md -- ADF, Hurst exponent, variance ratio, and half-life estimation with full implementations and interpretationreferences/strategy_design.md -- Z-score framework, position sizing, pairs trading setup, risk management, and backtest considerationsscripts/mean_reversion_test.py -- Comprehensive mean-reversion analysis: ADF, Hurst, variance ratio, half-life, OU estimation, z-score signalsscripts/pairs_scanner.py -- Scan multiple assets for mean-reverting pairs: correlation, cointegration, spread analysis, ranking# Run mean-reversion analysis on synthetic data
python scripts/mean_reversion_test.py --demo
# Scan for mean-reverting pairs
python scripts/pairs_scanner.py --demo
# Analyze a specific token (requires BIRDEYE_API_KEY)
BIRDEYE_API_KEY=your_key TOKEN_MINT=So11...1 python scripts/mean_reversion_test.py
This skill provides analytical tools and information only. It does not constitute financial advice or trading recommendations.
data-ai
DeFi yield evaluation including fee APR, real vs nominal yield, net APY after costs, and yield sustainability analysis
tools
Real-time Solana transaction and account streaming via Yellowstone gRPC (Geyser plugin)
tools
Large wallet monitoring, accumulation and distribution detection, and smart money signal generation for Solana tokens
tools
Wash sale detection under 2025 US crypto rules with 61-day window monitoring, disallowed loss tracking, and safe re-entry countdown