Pro Feature. Requires a Pro or Ultra subscription. Get started at api.mathematicalcompany.com
Spread Convergence
Track the price spread between two correlated markets. Enter when the spread deviates beyond a z-score threshold, exit when it reverts to the mean.How It Works
- Compute
spread = price_a - price_beach cycle - Maintain a rolling window of spread values
- Compute z-score of current spread vs historical mean/std
- Enter when
|z| > entry_zscore, exit when|z| < exit_zscore
Pipeline: spread_convergence
| Parameter | Type | Default | Description |
|---|---|---|---|
market_a | str | required | First market ID |
market_b | str | required | Second market ID |
feed_a | str | required | Feed for market A |
feed_b | str | required | Feed for market B |
lookback | int | 100 | Rolling window size |
entry_zscore | float | 2.0 | Z-score to enter |
exit_zscore | float | 0.5 | Z-score to exit |
max_spread | float | 0.20 | Max absolute spread to consider valid |
size | float | 10.0 | Trade size per leg |
auto_execute | bool | False | Auto-execute signals |
cooldown | float | 30.0 | Seconds between trades |
SpreadSignal
Stored inctx.params["last_spread_signal"].
| Field | Type | Description |
|---|---|---|
market_a | str | First market |
market_b | str | Second market |
spread | float | Current spread value |
spread_mean | float | Rolling mean |
spread_std | float | Rolling standard deviation |
zscore | float | Current z-score |
signal | str | "long_a_short_b", "long_b_short_a", "exit", or "hold" |
Signal Logic
| Condition | Position | Signal | ||
|---|---|---|---|---|
z > entry_zscore | flat | long_b_short_a | ||
z < -entry_zscore | flat | long_a_short_b | ||
| ` | z | < exit_zscore` | positioned | exit |
| otherwise | any | hold |
Example
For more rigorous pairs trading with cointegration testing and half-life filtering, see Statistical Arb.