Pair trading strategy. Trades mean reversion using the spread/ratio Z-score of two correlated instruments. Requires at least two instruments.
npx clawhub@latest install pair-tradingPair Trading is a market-neutral strategy skill that monitors the price ratio of two correlated instruments and trades mean reversion when their spread diverges significantly. It computes a rolling Z-score of the price ratio and enters opposing long/short positions when the Z-score exceeds a configurable threshold, then exits when the ratio reverts toward its historical mean. Install this skill to systematically exploit temporary mispricings between two related assets without taking a directional market bet.
npx clawhub@latest install pair-tradingClick the Install button at the top of this page for one-click setup
Computes a rolling mean and standard deviation of the price ratio (leg A / leg B) over a configurable lookback window, then derives a Z-score to quantify how far the current ratio deviates from its historical norm. Entry and exit signals fire at user-defined Z-score thresholds (entry_z and exit_z).
When a signal is triggered, leg A and leg B always receive opposite directions — one goes long while the other goes short. Each leg is allocated exactly 50% of capital, ensuring a balanced, market-neutral hedge at all times.
Three core parameters — lookback (default 60), entry_z (default 2.0), and exit_z (default 0.5) — can be tuned to match the mean-reversion speed and volatility characteristics of any instrument pair.
Works with both Tushare (for A-share equities) and OKX (for cryptocurrency pairs such as BTC-USDT / ETH-USDT), making the strategy applicable across asset classes without code changes.
Before the lookback window is fully populated, Z-scores are NaN and signals are automatically set to 0, preventing spurious trades during the initialization period.
Trade two highly correlated A-share insurance stocks (e.g., 601318.SH and 601628.SH) by entering when their price ratio Z-score exceeds ±2.0 and exiting when it falls back within ±0.5. This captures temporary mispricings driven by sector-neutral noise rather than fundamental divergence.
Apply the same Z-score logic to BTC-USDT and ETH-USDT on OKX, exploiting the historically tight correlation between the two largest cryptocurrencies. When one rallies disproportionately relative to the other, the skill shorts the outperformer and longs the laggard.
Use the configurable lookback, entry_z, and exit_z parameters to backtest different mean-reversion assumptions over historical data, comparing how tighter or wider Z-score thresholds affect trade frequency, drawdown, and returns.
Embed this skill within a broader portfolio to add a market-neutral return stream, since the equal-weight long-short structure reduces net market exposure and provides returns that are relatively uncorrelated with broad market direction.
pandas, numpy (install via pip install pandas numpy)codes array; their price series must share aligned date indexesnpx clawhub@latest install pair-tradingLog in to write a review
No reviews yet. Be the first to share your experience!