# Pairs spread (pairs_spread)

Desk: Equities. Page: https://hedgefund.md/models/pairs_spread

**What it asks.** Does this spread pull back to its mean, and what is left of a hypothetical walk through the later window after costs?

Fits a log hedge ratio on the first part of two price series, tests the spread for pull-back to its mean, and walks the later part with those fit numbers and entry and exit z-scores, after costs. Prices come from the caller. It is a hypothetical walk through one sample, not a test of cointegration in any strict sense, a trade idea or a signal to follow.

Results are hypothetical: arithmetic on the numbers sent, a made-up template or a stated default. Nobody achieved them, and they aren't a forecast, a recommendation, an offer or a record of real trading. Don't show any output to investors or clients, or use it to market an investment. Terms: https://hedgefund.md/terms

## Inputs

| Input | Type | Range | Required | What it is |
| --- | --- | --- | --- | --- |
| `series_a` | array | 40 to 2000 items | yes | Prices of A, oldest first, above zero. 40 to 2000 points. |
| `series_b` | array | 40 to 2000 items | yes | Prices of B, same dates and length as series_a. |
| `entry_z` | number | 0.1 to 10 | yes | The walk opens a hypothetical trade when the z-score is past this size. Must be above exit_z. |
| `exit_z` | number | 0 to 9 | yes | The walk closes a hypothetical trade once the z-score is back inside this size. 0 means at the mean. |
| `cost_bps_per_leg` | number | 0 to 1000 | yes | Cost per leg, in basis points. A round trip pays four legs. |
| `fit_share_pct` | number | 20 to 80 | no | Share of the points used to fit. The rest is walked. Default 50. |

## Template

Made-up numbers that run as they stand, so you can see the shape of a result. They aren't market levels, typical values or estimates, and the names in them don't exist. Replace every number before you read anything into a run.

```json
{
  "series_a": [
    108.12, 108.57, 103.13, 103.28, 102.31, 104.84, 107.3, 104.71, 102.25, 99.76,
    100.82, 100.29, 100.27, 100.31, 102.79, 101.69, 102.56, 102.15, 104.54, 108.17,
    101.79, 102.04, 101.32, 100.85, 99.1, 96.83, 96.75, 97.23, 97.84, 95.01,
    94.87, 94.28, 96.59, 97.83, 96.15, 98.17, 97.7, 95.29, 95.82, 97.05,
    94.79, 96.32, 99.1, 98.71, 98.34, 97.28, 99.53, 98.69, 99.56, 99.39,
    101.5, 100.89, 97.23, 99.35, 101.55, 99.1, 99.4, 99.33, 100.55, 101.12,
    104.48, 106.08, 104.26, 106.52, 108.34, 106.24, 105.95, 103.44, 104.96, 104.46,
    107.19, 106.83, 104.73, 103.89, 103.81, 101.68, 98.76, 100.12, 99.72, 99.31,
    100.34, 101.1, 99.65, 98.28, 98.92, 101.36, 103.15, 101.98, 101.38, 104.99,
    103.88, 104.66, 105.52, 105.99, 106.45, 107.69, 108.35, 108.47, 110.48, 110.34,
    108.45, 107.75, 106.32, 106.49, 104.92, 105.62, 102.94, 102.82, 105.32, 104.97,
    105.1, 107.19, 105.74, 106.02, 103.25, 100.81, 102.17, 100.61, 100.92, 100.48,
    101.25, 98.02, 96.89, 96.4, 95.25, 94.18, 91.6, 88.51, 86.67, 86.33,
    86.05, 86.66, 88.49, 86.96, 85.07, 83.48, 83.86, 81.13, 81.38, 82.81,
    82.68, 82.27, 82.72, 83.14, 84.93, 81.84, 81.47, 82.31, 80.94, 82.77,
    81.43, 84.07, 84.72, 84.85, 85.56, 85.16, 85.77, 85.72, 83.98, 83.23,
    84.4, 83.87, 81.29, 79.32, 79.95, 81.29, 81.97, 81.18, 81.22, 82.95,
    80.72, 82.22, 83.5, 82.05, 83.4, 79.65, 79.95, 78.02, 75.87, 76.43,
    75.68, 75.07, 74.33, 71.12, 72.01, 71.81, 72.53, 72.69, 72.44, 73.33,
    73.83, 74.64, 76.35, 76.9, 77.04, 75.98, 74.18, 73.75, 73.68, 73.89,
    74.15, 71.69, 71.78, 71.49, 69.96, 69.68, 68.7, 69.15, 67.31, 70.81,
    70.64, 70.09, 70.53, 70.31, 70.64, 70.55, 71.25, 72.45, 73.08, 73.54,
    74.42, 74.52, 73.21, 72.9, 72.16, 71.05, 72.46, 72.75, 71.08, 69.57,
    70.58, 68.45, 71.43, 71.92, 71.79, 71.11, 71.26, 70.03, 68.98, 68.85,
    68.32, 67.81, 68.03, 67.15, 66.12, 65.89, 67.07, 66.25, 65.5, 67.16,
    68.57, 68.93, 68.13, 68.69, 67.88, 69.03, 70.61, 71.4, 70.77, 69.29,
    70.9, 72.13, 74.27, 73.7, 74.35, 73.28, 75.06, 75.77, 73.85, 72.74,
    72.29, 72.79, 72.3, 73.63, 75.22, 75.3, 75.55, 74.1, 73.82, 74.8,
    74.92, 75.89, 75.9, 76.07, 77.1, 76.49, 73.32, 72.95, 73.4, 76.44,
    75.78, 75.48, 73.77, 74.6, 74.87, 73.37, 71.53, 70.76, 70.91, 72.12,
    71.81, 72.47, 70.81, 69.78, 69.7, 69.03, 68.65, 68.14, 70.17, 69.15,
    66.99, 68.4, 68, 68.91, 71.28, 70.05, 70.53, 73.98, 73.15, 73.56,
    73.25, 71.05, 72.55, 74.1, 73.66, 72.63, 72.48, 71.59, 70.58, 70.61,
    71.85, 72.14, 71.41, 73.37, 75.11, 72.43, 72.28, 71.8, 70.66, 69.93,
    70.07, 69.56, 70.48, 69.65, 69.98, 70.38, 71.84, 72.18, 73.81, 74.64,
    74.69, 73.64, 73.3, 72.48, 72.83, 73.7, 74.26, 75.16, 76.51, 76.56,
    78.76, 79.02, 76.71, 76.34, 75.73, 74.2, 73.22, 74.02, 71.63, 71.26,
    70.51, 70.74, 71.14, 72.48, 73.16, 71.83, 73.75, 74.17, 72.31, 71.53,
    70.86, 69.51, 69.51, 70.29, 70.48, 72.24, 71.45, 70.35, 70.79, 71.27,
    71.14, 72.51, 73.33, 73.48, 72.58, 73.31, 74.91, 77.81, 79.32, 77.92,
    79.36, 80.13, 80.86, 78.81, 80.84, 79.97, 80.07, 77.41, 77.89, 77.68,
    78.59, 77.79, 78.2, 76.06, 75.64, 74.62, 75.52, 76.54, 76.14, 76.9,
    77.87, 74.2, 75.83, 75.53, 76.46, 75.92, 75.37, 79.44, 80.92, 78.71,
    82.66, 81.17, 83.28, 80.79, 81.99, 84.3, 84.99, 86.34, 87.32, 87.88,
    86.42, 84.29, 84.95, 86.3, 86.79, 87.05, 86.43, 87.47, 89.55, 91.81,
    91.3, 92.07, 93.51, 95.49, 96.59, 95.44, 96.95, 98.63, 98.6, 101.11,
    100.11, 98.41, 99.38, 101.46, 101.24, 98.48, 97.44, 97.48, 96.36, 95.08,
    98.74, 99.78, 98.94, 101.19, 98.97, 98.83, 98.96, 96.84, 97.82, 98.12,
    96.49, 97.01, 93.21, 92.38, 94.76, 94.14, 95.32, 96.07, 96.83, 97.44,
    99.83, 98.6, 98.62, 99.63, 99.79, 96.51, 94.69, 95.14, 97.03, 97.8
  ],
  "series_b": [
    49.48, 49.95, 48.68, 48.5, 47.89, 48.74, 48.72, 48.11, 47.72, 47.66,
    47.54, 47.91, 47.56, 47.96, 48.9, 47.8, 48, 47.66, 48.13, 48.9,
    47.31, 47.11, 46.71, 47.1, 46.57, 46.21, 46.27, 46.92, 46.84, 46.3,
    46.18, 46.12, 46.42, 46.52, 45.5, 46.03, 45.57, 44.92, 45.16, 45.88,
    45.45, 45.76, 45.87, 45.87, 45.23, 45.04, 45.18, 44.15, 44.88, 44.96,
    45.34, 45.15, 44.38, 45.51, 46.42, 46.47, 46.63, 46.35, 46.56, 46.7,
    47.62, 48.34, 47.23, 48.19, 49.03, 49.04, 48.64, 47.73, 48.02, 48.34,
    48.73, 48.19, 48.18, 47.76, 47.49, 46.96, 46.32, 47.01, 47.34, 47.55,
    47.2, 47.21, 47.24, 46.6, 46.43, 47.22, 47.99, 48.39, 48.06, 48.97,
    48.89, 49.08, 49.08, 49.83, 48.86, 48.68, 49.11, 48.84, 48.47, 48.82,
    47.94, 47.29, 46.95, 47.17, 46.85, 46.77, 46.49, 46.22, 46.96, 47.67,
    47.21, 47.67, 47.09, 46.86, 46.32, 45.58, 46.06, 45.82, 45.34, 45.44,
    45.27, 45.23, 45.01, 45.2, 44.96, 44.63, 43.41, 42.05, 41.35, 40.59,
    40.4, 40.69, 41.18, 40.71, 40.23, 39.89, 40.17, 39.21, 38.92, 39.2,
    39, 38.82, 38.86, 39.24, 40.06, 39.56, 38.76, 39.24, 39.03, 39.51,
    38.99, 40.23, 40.52, 40.55, 40.48, 40.14, 40.44, 40.33, 39.89, 39.63,
    39.86, 39.64, 39.15, 37.96, 38.23, 38.6, 38.81, 38.65, 38.49, 39,
    37.95, 38.07, 38.41, 37.93, 37.76, 36.58, 36.79, 36.05, 36.07, 35.88,
    35.83, 35.46, 35.3, 34.57, 34.57, 34.11, 34.43, 34.53, 34.36, 34.98,
    35.13, 35.29, 35.61, 35.75, 35.59, 35.18, 34.42, 33.74, 33.49, 33.38,
    33.36, 32.68, 32.49, 32.41, 32.21, 31.79, 31.78, 32.25, 31.67, 32.53,
    32.43, 32.12, 31.81, 31.89, 31.81, 31.62, 31.9, 32.42, 32.79, 33.37,
    33.79, 34.24, 33.58, 33.83, 33.53, 33.66, 34, 34, 33.26, 33.06,
    32.92, 32.61, 33.52, 33.19, 33.12, 33.23, 33.21, 33.3, 32.89, 33.09,
    32.8, 32.45, 32.95, 32.54, 32.11, 32.1, 32.12, 32.14, 32.07, 32.33,
    32.48, 32.59, 32.46, 32.11, 31.71, 32.03, 32.58, 32.78, 32.85, 32.5,
    32.47, 32.71, 32.91, 33.45, 33.17, 33.18, 33.69, 34.18, 33.91, 33.42,
    33.73, 33.55, 33.63, 33.6, 34.17, 34.11, 34.45, 34.04, 33.77, 33.88,
    33.57, 33.57, 33.53, 34.16, 34.08, 33.88, 33.18, 33.3, 33.79, 34.15,
    34.07, 34.44, 34.24, 34.55, 34.68, 34.18, 33.87, 33.67, 33.35, 33.74,
    33.43, 33.47, 33.13, 33.25, 33.08, 32.74, 32.78, 32.75, 33.18, 32.65,
    32.03, 32.44, 32.28, 32.07, 32.5, 32.37, 32.62, 33.58, 33.96, 34.12,
    33.98, 33.53, 33.84, 33.64, 34.03, 34.11, 33.86, 33.73, 33.1, 33.11,
    33.19, 33.34, 33.53, 34.03, 34.71, 33.93, 33.98, 33.9, 33.5, 33.02,
    33.26, 33.15, 33.67, 33.63, 33.63, 34.1, 34.28, 34.21, 34.65, 35.18,
    35.61, 35.56, 35.3, 35.01, 34.56, 34.77, 34.79, 34.9, 35.42, 35.68,
    35.94, 36.42, 35.96, 35.43, 35.36, 34.93, 34.75, 35.11, 34.32, 34.46,
    34.34, 34.45, 34.56, 34.55, 34.6, 34.19, 34.65, 35.17, 34.01, 33.58,
    33.75, 33.91, 33.29, 33.49, 33.4, 33.8, 33.94, 33.55, 33.97, 34.34,
    33.83, 34.5, 34.93, 34.81, 34.93, 35.42, 35.78, 36.17, 36.66, 36.21,
    36.43, 36.84, 36.97, 36.4, 37.1, 36.89, 36.92, 36.4, 37.07, 36.82,
    36.76, 36.67, 36.63, 36.16, 36.33, 36.37, 36.49, 36.59, 36.94, 37.26,
    37.16, 36.37, 36.52, 36.15, 36.23, 36.17, 36.13, 37.07, 37.41, 36.81,
    37.82, 37.8, 38.52, 38.25, 38.63, 38.96, 39.22, 39.59, 39.98, 39.77,
    39.11, 38.35, 38.23, 38.39, 38.03, 38.21, 38.17, 39.02, 39.34, 40.07,
    40.28, 40.99, 41.21, 41.85, 42.31, 42.1, 42.33, 42.5, 42.46, 43.09,
    42.35, 42.04, 42.65, 42.89, 43, 41.63, 42.25, 42.16, 42.08, 41.97,
    42.66, 43.17, 43.17, 43.85, 43.03, 43, 43.06, 42.22, 42.78, 42.61,
    42.24, 42.2, 41.31, 40.9, 41.45, 41.62, 41.6, 41.2, 41.8, 42.07,
    42.36, 42.26, 42.77, 42.74, 42.15, 41.47, 40.92, 40.57, 40.35, 40.19
  ],
  "entry_z": 2,
  "exit_z": 0.5,
  "cost_bps_per_leg": 5,
  "fit_share_pct": 50
}
```

## What the template gives

On these numbers, the fit window gives a hedge ratio of 1.0325 and a half-life of 6.29 points, and a hypothetical walk through the later window makes 3 trades for a total of 1.74% after costs.

- Hedge ratio h: 1.0325
- Half-life: 6.2938 points
- t-stat of b: -3.6525
- z-score at the last point: 5.294
- Hypothetical trades in the later window: 3
- Hypothetical hit rate: 66.67%
- Hypothetical total after costs: 1.7387% log, per unit of A
- Longest hold: 56 points

Where it flips, with the other inputs held:

- `cost_bps_per_leg` at 19.489: At this cost per leg the later-window total is zero. Above it, the total is negative. The trades themselves don't depend on cost.

## Formula

On the fit window: ln A = c + h ln B + e. S_t = ln A_t - h ln B_t - c. z_t = (S_t - mean_fit) / stdev_fit. delta S_t = a + b S_t-1, half-life = -ln 2 / ln(1 + b) when -1 < b < 0. Trade return = direction * (S_exit - S_entry) - 4 * cost_bps_per_leg / 10000. Flip cost per leg = sum of gross trade returns * 10000 / (4 * trades).

## Assumptions

- The fit window is the first fit_share_pct of the points. The hedge ratio, spread mean, spread stdev and reversion test come from it alone, so the walk never sees its own later numbers.
- Stdev divides by n - 1. Half-life is in points of the series. A b under 1e-12 in size is rounding, so it reads as zero.
- The t-stat of b is read against about -3.34, a rule of thumb for two series at the 5% level. Ordinary t tables don't apply, because h was fitted to the same numbers.
- Above +entry_z the walk is short the spread, below -entry_z long. A short spread leaves once z is at or below exit_z and a long spread once z is at or above -exit_z, so a jump straight across the band also leaves. That is how this walk runs, not a side to take.
- A leave and a new entry can happen on the same point if z jumps past the other side. Trades fill at the close that triggers them.
- A trade still open at the end is marked at the last point, pays its four legs and counts in the totals, so a pair that stops converging still shows up.
- Return is in log terms, per unit of A, and the B leg is charged one full leg of cost whatever h is.
- The hedge ratio comes from log prices, so it is a ratio of values: 1 of value in A against h of value in B, not h shares per share.
- The sleeve counts 100 of A against h times 100 of B.

## Not modeled

- Borrow and financing on the short leg, and dividends on either leg.
- The delay between seeing a close and trading it, and slippage beyond cost_bps_per_leg.
- Share counts: the log spread treats the hedge as reset every point, while a real position with fixed shares drifts.
- Stop-losses, position sizing and limits on a trade's length.
- Exact small-sample critical values for the reversion test.
- Taxes and fees.

## Call it

Every result has the same fields: summary, readings, tables, charts, flags, flips, formula, assumptions, not_modeled, hypothetical and inputs_hash. Read flags, flips and not_modeled with the summary, and pass them along together.

### HTTP

No key. The tools keep nothing you send. Send positions by POST, because a URL can land in a server log.

```bash
curl -s https://hedgefund.md/api/v1/pairs_spread \
  -H 'content-type: application/json' \
  -d '{"series_a":[108.12,108.57,103.13,103.28,102.31,104.84,107.3,104.71,102.25,99.76,100.82,100.29,100.27,100.31,102.79,101.69,102.56,102.15,104.54,108.17,101.79,102.04,101.32,100.85,99.1,96.83,96.75,97.23,97.84,95.01,94.87,94.28,96.59,97.83,96.15,98.17,97.7,95.29,95.82,97.05,94.79,96.32,99.1,98.71,98.34,97.28,99.53,98.69,99.56,99.39,101.5,100.89,97.23,99.35,101.55,99.1,99.4,99.33,100.55,101.12,104.48,106.08,104.26,106.52,108.34,106.24,105.95,103.44,104.96,104.46,107.19,106.83,104.73,103.89,103.81,101.68,98.76,100.12,99.72,99.31,100.34,101.1,99.65,98.28,98.92,101.36,103.15,101.98,101.38,104.99,103.88,104.66,105.52,105.99,106.45,107.69,108.35,108.47,110.48,110.34,108.45,107.75,106.32,106.49,104.92,105.62,102.94,102.82,105.32,104.97,105.1,107.19,105.74,106.02,103.25,100.81,102.17,100.61,100.92,100.48,101.25,98.02,96.89,96.4,95.25,94.18,91.6,88.51,86.67,86.33,86.05,86.66,88.49,86.96,85.07,83.48,83.86,81.13,81.38,82.81,82.68,82.27,82.72,83.14,84.93,81.84,81.47,82.31,80.94,82.77,81.43,84.07,84.72,84.85,85.56,85.16,85.77,85.72,83.98,83.23,84.4,83.87,81.29,79.32,79.95,81.29,81.97,81.18,81.22,82.95,80.72,82.22,83.5,82.05,83.4,79.65,79.95,78.02,75.87,76.43,75.68,75.07,74.33,71.12,72.01,71.81,72.53,72.69,72.44,73.33,73.83,74.64,76.35,76.9,77.04,75.98,74.18,73.75,73.68,73.89,74.15,71.69,71.78,71.49,69.96,69.68,68.7,69.15,67.31,70.81,70.64,70.09,70.53,70.31,70.64,70.55,71.25,72.45,73.08,73.54,74.42,74.52,73.21,72.9,72.16,71.05,72.46,72.75,71.08,69.57,70.58,68.45,71.43,71.92,71.79,71.11,71.26,70.03,68.98,68.85,68.32,67.81,68.03,67.15,66.12,65.89,67.07,66.25,65.5,67.16,68.57,68.93,68.13,68.69,67.88,69.03,70.61,71.4,70.77,69.29,70.9,72.13,74.27,73.7,74.35,73.28,75.06,75.77,73.85,72.74,72.29,72.79,72.3,73.63,75.22,75.3,75.55,74.1,73.82,74.8,74.92,75.89,75.9,76.07,77.1,76.49,73.32,72.95,73.4,76.44,75.78,75.48,73.77,74.6,74.87,73.37,71.53,70.76,70.91,72.12,71.81,72.47,70.81,69.78,69.7,69.03,68.65,68.14,70.17,69.15,66.99,68.4,68,68.91,71.28,70.05,70.53,73.98,73.15,73.56,73.25,71.05,72.55,74.1,73.66,72.63,72.48,71.59,70.58,70.61,71.85,72.14,71.41,73.37,75.11,72.43,72.28,71.8,70.66,69.93,70.07,69.56,70.48,69.65,69.98,70.38,71.84,72.18,73.81,74.64,74.69,73.64,73.3,72.48,72.83,73.7,74.26,75.16,76.51,76.56,78.76,79.02,76.71,76.34,75.73,74.2,73.22,74.02,71.63,71.26,70.51,70.74,71.14,72.48,73.16,71.83,73.75,74.17,72.31,71.53,70.86,69.51,69.51,70.29,70.48,72.24,71.45,70.35,70.79,71.27,71.14,72.51,73.33,73.48,72.58,73.31,74.91,77.81,79.32,77.92,79.36,80.13,80.86,78.81,80.84,79.97,80.07,77.41,77.89,77.68,78.59,77.79,78.2,76.06,75.64,74.62,75.52,76.54,76.14,76.9,77.87,74.2,75.83,75.53,76.46,75.92,75.37,79.44,80.92,78.71,82.66,81.17,83.28,80.79,81.99,84.3,84.99,86.34,87.32,87.88,86.42,84.29,84.95,86.3,86.79,87.05,86.43,87.47,89.55,91.81,91.3,92.07,93.51,95.49,96.59,95.44,96.95,98.63,98.6,101.11,100.11,98.41,99.38,101.46,101.24,98.48,97.44,97.48,96.36,95.08,98.74,99.78,98.94,101.19,98.97,98.83,98.96,96.84,97.82,98.12,96.49,97.01,93.21,92.38,94.76,94.14,95.32,96.07,96.83,97.44,99.83,98.6,98.62,99.63,99.79,96.51,94.69,95.14,97.03,97.8],"series_b":[49.48,49.95,48.68,48.5,47.89,48.74,48.72,48.11,47.72,47.66,47.54,47.91,47.56,47.96,48.9,47.8,48,47.66,48.13,48.9,47.31,47.11,46.71,47.1,46.57,46.21,46.27,46.92,46.84,46.3,46.18,46.12,46.42,46.52,45.5,46.03,45.57,44.92,45.16,45.88,45.45,45.76,45.87,45.87,45.23,45.04,45.18,44.15,44.88,44.96,45.34,45.15,44.38,45.51,46.42,46.47,46.63,46.35,46.56,46.7,47.62,48.34,47.23,48.19,49.03,49.04,48.64,47.73,48.02,48.34,48.73,48.19,48.18,47.76,47.49,46.96,46.32,47.01,47.34,47.55,47.2,47.21,47.24,46.6,46.43,47.22,47.99,48.39,48.06,48.97,48.89,49.08,49.08,49.83,48.86,48.68,49.11,48.84,48.47,48.82,47.94,47.29,46.95,47.17,46.85,46.77,46.49,46.22,46.96,47.67,47.21,47.67,47.09,46.86,46.32,45.58,46.06,45.82,45.34,45.44,45.27,45.23,45.01,45.2,44.96,44.63,43.41,42.05,41.35,40.59,40.4,40.69,41.18,40.71,40.23,39.89,40.17,39.21,38.92,39.2,39,38.82,38.86,39.24,40.06,39.56,38.76,39.24,39.03,39.51,38.99,40.23,40.52,40.55,40.48,40.14,40.44,40.33,39.89,39.63,39.86,39.64,39.15,37.96,38.23,38.6,38.81,38.65,38.49,39,37.95,38.07,38.41,37.93,37.76,36.58,36.79,36.05,36.07,35.88,35.83,35.46,35.3,34.57,34.57,34.11,34.43,34.53,34.36,34.98,35.13,35.29,35.61,35.75,35.59,35.18,34.42,33.74,33.49,33.38,33.36,32.68,32.49,32.41,32.21,31.79,31.78,32.25,31.67,32.53,32.43,32.12,31.81,31.89,31.81,31.62,31.9,32.42,32.79,33.37,33.79,34.24,33.58,33.83,33.53,33.66,34,34,33.26,33.06,32.92,32.61,33.52,33.19,33.12,33.23,33.21,33.3,32.89,33.09,32.8,32.45,32.95,32.54,32.11,32.1,32.12,32.14,32.07,32.33,32.48,32.59,32.46,32.11,31.71,32.03,32.58,32.78,32.85,32.5,32.47,32.71,32.91,33.45,33.17,33.18,33.69,34.18,33.91,33.42,33.73,33.55,33.63,33.6,34.17,34.11,34.45,34.04,33.77,33.88,33.57,33.57,33.53,34.16,34.08,33.88,33.18,33.3,33.79,34.15,34.07,34.44,34.24,34.55,34.68,34.18,33.87,33.67,33.35,33.74,33.43,33.47,33.13,33.25,33.08,32.74,32.78,32.75,33.18,32.65,32.03,32.44,32.28,32.07,32.5,32.37,32.62,33.58,33.96,34.12,33.98,33.53,33.84,33.64,34.03,34.11,33.86,33.73,33.1,33.11,33.19,33.34,33.53,34.03,34.71,33.93,33.98,33.9,33.5,33.02,33.26,33.15,33.67,33.63,33.63,34.1,34.28,34.21,34.65,35.18,35.61,35.56,35.3,35.01,34.56,34.77,34.79,34.9,35.42,35.68,35.94,36.42,35.96,35.43,35.36,34.93,34.75,35.11,34.32,34.46,34.34,34.45,34.56,34.55,34.6,34.19,34.65,35.17,34.01,33.58,33.75,33.91,33.29,33.49,33.4,33.8,33.94,33.55,33.97,34.34,33.83,34.5,34.93,34.81,34.93,35.42,35.78,36.17,36.66,36.21,36.43,36.84,36.97,36.4,37.1,36.89,36.92,36.4,37.07,36.82,36.76,36.67,36.63,36.16,36.33,36.37,36.49,36.59,36.94,37.26,37.16,36.37,36.52,36.15,36.23,36.17,36.13,37.07,37.41,36.81,37.82,37.8,38.52,38.25,38.63,38.96,39.22,39.59,39.98,39.77,39.11,38.35,38.23,38.39,38.03,38.21,38.17,39.02,39.34,40.07,40.28,40.99,41.21,41.85,42.31,42.1,42.33,42.5,42.46,43.09,42.35,42.04,42.65,42.89,43,41.63,42.25,42.16,42.08,41.97,42.66,43.17,43.17,43.85,43.03,43,43.06,42.22,42.78,42.61,42.24,42.2,41.31,40.9,41.45,41.62,41.6,41.2,41.8,42.07,42.36,42.26,42.77,42.74,42.15,41.47,40.92,40.57,40.35,40.19],"entry_z":2,"exit_z":0.5,"cost_bps_per_leg":5,"fit_share_pct":50}' \
  | jq '{summary, flags, flips, not_modeled, inputs_hash}'
```

### MCP

The tool is `pairs_spread` on the server at https://hedgefund.md/mcp.

```bash
curl -s https://hedgefund.md/mcp \
  -H 'content-type: application/json' -H 'accept: application/json' \
  -d '{"jsonrpc":"2.0","id":1,"method":"tools/call","params":{"name":"pairs_spread","arguments":{"series_a":[108.12,108.57,103.13,103.28,102.31,104.84,107.3,104.71,102.25,99.76,100.82,100.29,100.27,100.31,102.79,101.69,102.56,102.15,104.54,108.17,101.79,102.04,101.32,100.85,99.1,96.83,96.75,97.23,97.84,95.01,94.87,94.28,96.59,97.83,96.15,98.17,97.7,95.29,95.82,97.05,94.79,96.32,99.1,98.71,98.34,97.28,99.53,98.69,99.56,99.39,101.5,100.89,97.23,99.35,101.55,99.1,99.4,99.33,100.55,101.12,104.48,106.08,104.26,106.52,108.34,106.24,105.95,103.44,104.96,104.46,107.19,106.83,104.73,103.89,103.81,101.68,98.76,100.12,99.72,99.31,100.34,101.1,99.65,98.28,98.92,101.36,103.15,101.98,101.38,104.99,103.88,104.66,105.52,105.99,106.45,107.69,108.35,108.47,110.48,110.34,108.45,107.75,106.32,106.49,104.92,105.62,102.94,102.82,105.32,104.97,105.1,107.19,105.74,106.02,103.25,100.81,102.17,100.61,100.92,100.48,101.25,98.02,96.89,96.4,95.25,94.18,91.6,88.51,86.67,86.33,86.05,86.66,88.49,86.96,85.07,83.48,83.86,81.13,81.38,82.81,82.68,82.27,82.72,83.14,84.93,81.84,81.47,82.31,80.94,82.77,81.43,84.07,84.72,84.85,85.56,85.16,85.77,85.72,83.98,83.23,84.4,83.87,81.29,79.32,79.95,81.29,81.97,81.18,81.22,82.95,80.72,82.22,83.5,82.05,83.4,79.65,79.95,78.02,75.87,76.43,75.68,75.07,74.33,71.12,72.01,71.81,72.53,72.69,72.44,73.33,73.83,74.64,76.35,76.9,77.04,75.98,74.18,73.75,73.68,73.89,74.15,71.69,71.78,71.49,69.96,69.68,68.7,69.15,67.31,70.81,70.64,70.09,70.53,70.31,70.64,70.55,71.25,72.45,73.08,73.54,74.42,74.52,73.21,72.9,72.16,71.05,72.46,72.75,71.08,69.57,70.58,68.45,71.43,71.92,71.79,71.11,71.26,70.03,68.98,68.85,68.32,67.81,68.03,67.15,66.12,65.89,67.07,66.25,65.5,67.16,68.57,68.93,68.13,68.69,67.88,69.03,70.61,71.4,70.77,69.29,70.9,72.13,74.27,73.7,74.35,73.28,75.06,75.77,73.85,72.74,72.29,72.79,72.3,73.63,75.22,75.3,75.55,74.1,73.82,74.8,74.92,75.89,75.9,76.07,77.1,76.49,73.32,72.95,73.4,76.44,75.78,75.48,73.77,74.6,74.87,73.37,71.53,70.76,70.91,72.12,71.81,72.47,70.81,69.78,69.7,69.03,68.65,68.14,70.17,69.15,66.99,68.4,68,68.91,71.28,70.05,70.53,73.98,73.15,73.56,73.25,71.05,72.55,74.1,73.66,72.63,72.48,71.59,70.58,70.61,71.85,72.14,71.41,73.37,75.11,72.43,72.28,71.8,70.66,69.93,70.07,69.56,70.48,69.65,69.98,70.38,71.84,72.18,73.81,74.64,74.69,73.64,73.3,72.48,72.83,73.7,74.26,75.16,76.51,76.56,78.76,79.02,76.71,76.34,75.73,74.2,73.22,74.02,71.63,71.26,70.51,70.74,71.14,72.48,73.16,71.83,73.75,74.17,72.31,71.53,70.86,69.51,69.51,70.29,70.48,72.24,71.45,70.35,70.79,71.27,71.14,72.51,73.33,73.48,72.58,73.31,74.91,77.81,79.32,77.92,79.36,80.13,80.86,78.81,80.84,79.97,80.07,77.41,77.89,77.68,78.59,77.79,78.2,76.06,75.64,74.62,75.52,76.54,76.14,76.9,77.87,74.2,75.83,75.53,76.46,75.92,75.37,79.44,80.92,78.71,82.66,81.17,83.28,80.79,81.99,84.3,84.99,86.34,87.32,87.88,86.42,84.29,84.95,86.3,86.79,87.05,86.43,87.47,89.55,91.81,91.3,92.07,93.51,95.49,96.59,95.44,96.95,98.63,98.6,101.11,100.11,98.41,99.38,101.46,101.24,98.48,97.44,97.48,96.36,95.08,98.74,99.78,98.94,101.19,98.97,98.83,98.96,96.84,97.82,98.12,96.49,97.01,93.21,92.38,94.76,94.14,95.32,96.07,96.83,97.44,99.83,98.6,98.62,99.63,99.79,96.51,94.69,95.14,97.03,97.8],"series_b":[49.48,49.95,48.68,48.5,47.89,48.74,48.72,48.11,47.72,47.66,47.54,47.91,47.56,47.96,48.9,47.8,48,47.66,48.13,48.9,47.31,47.11,46.71,47.1,46.57,46.21,46.27,46.92,46.84,46.3,46.18,46.12,46.42,46.52,45.5,46.03,45.57,44.92,45.16,45.88,45.45,45.76,45.87,45.87,45.23,45.04,45.18,44.15,44.88,44.96,45.34,45.15,44.38,45.51,46.42,46.47,46.63,46.35,46.56,46.7,47.62,48.34,47.23,48.19,49.03,49.04,48.64,47.73,48.02,48.34,48.73,48.19,48.18,47.76,47.49,46.96,46.32,47.01,47.34,47.55,47.2,47.21,47.24,46.6,46.43,47.22,47.99,48.39,48.06,48.97,48.89,49.08,49.08,49.83,48.86,48.68,49.11,48.84,48.47,48.82,47.94,47.29,46.95,47.17,46.85,46.77,46.49,46.22,46.96,47.67,47.21,47.67,47.09,46.86,46.32,45.58,46.06,45.82,45.34,45.44,45.27,45.23,45.01,45.2,44.96,44.63,43.41,42.05,41.35,40.59,40.4,40.69,41.18,40.71,40.23,39.89,40.17,39.21,38.92,39.2,39,38.82,38.86,39.24,40.06,39.56,38.76,39.24,39.03,39.51,38.99,40.23,40.52,40.55,40.48,40.14,40.44,40.33,39.89,39.63,39.86,39.64,39.15,37.96,38.23,38.6,38.81,38.65,38.49,39,37.95,38.07,38.41,37.93,37.76,36.58,36.79,36.05,36.07,35.88,35.83,35.46,35.3,34.57,34.57,34.11,34.43,34.53,34.36,34.98,35.13,35.29,35.61,35.75,35.59,35.18,34.42,33.74,33.49,33.38,33.36,32.68,32.49,32.41,32.21,31.79,31.78,32.25,31.67,32.53,32.43,32.12,31.81,31.89,31.81,31.62,31.9,32.42,32.79,33.37,33.79,34.24,33.58,33.83,33.53,33.66,34,34,33.26,33.06,32.92,32.61,33.52,33.19,33.12,33.23,33.21,33.3,32.89,33.09,32.8,32.45,32.95,32.54,32.11,32.1,32.12,32.14,32.07,32.33,32.48,32.59,32.46,32.11,31.71,32.03,32.58,32.78,32.85,32.5,32.47,32.71,32.91,33.45,33.17,33.18,33.69,34.18,33.91,33.42,33.73,33.55,33.63,33.6,34.17,34.11,34.45,34.04,33.77,33.88,33.57,33.57,33.53,34.16,34.08,33.88,33.18,33.3,33.79,34.15,34.07,34.44,34.24,34.55,34.68,34.18,33.87,33.67,33.35,33.74,33.43,33.47,33.13,33.25,33.08,32.74,32.78,32.75,33.18,32.65,32.03,32.44,32.28,32.07,32.5,32.37,32.62,33.58,33.96,34.12,33.98,33.53,33.84,33.64,34.03,34.11,33.86,33.73,33.1,33.11,33.19,33.34,33.53,34.03,34.71,33.93,33.98,33.9,33.5,33.02,33.26,33.15,33.67,33.63,33.63,34.1,34.28,34.21,34.65,35.18,35.61,35.56,35.3,35.01,34.56,34.77,34.79,34.9,35.42,35.68,35.94,36.42,35.96,35.43,35.36,34.93,34.75,35.11,34.32,34.46,34.34,34.45,34.56,34.55,34.6,34.19,34.65,35.17,34.01,33.58,33.75,33.91,33.29,33.49,33.4,33.8,33.94,33.55,33.97,34.34,33.83,34.5,34.93,34.81,34.93,35.42,35.78,36.17,36.66,36.21,36.43,36.84,36.97,36.4,37.1,36.89,36.92,36.4,37.07,36.82,36.76,36.67,36.63,36.16,36.33,36.37,36.49,36.59,36.94,37.26,37.16,36.37,36.52,36.15,36.23,36.17,36.13,37.07,37.41,36.81,37.82,37.8,38.52,38.25,38.63,38.96,39.22,39.59,39.98,39.77,39.11,38.35,38.23,38.39,38.03,38.21,38.17,39.02,39.34,40.07,40.28,40.99,41.21,41.85,42.31,42.1,42.33,42.5,42.46,43.09,42.35,42.04,42.65,42.89,43,41.63,42.25,42.16,42.08,41.97,42.66,43.17,43.17,43.85,43.03,43,43.06,42.22,42.78,42.61,42.24,42.2,41.31,40.9,41.45,41.62,41.6,41.2,41.8,42.07,42.36,42.26,42.77,42.74,42.15,41.47,40.92,40.57,40.35,40.19],"entry_z":2,"exit_z":0.5,"cost_bps_per_leg":5,"fit_share_pct":50}}}' \
  | jq '.result.structuredContent | {summary, flags, flips, not_modeled, inputs_hash}'
```

### The library

The same code on your own machine, with no dependencies, and nothing is sent anywhere.

```bash
curl -fsSL https://hedgefund.md/hedgefund.mjs -o hedgefund.mjs   # read it before you run it
node hedgefund.mjs template pairs_spread > mine.json           # replace every number in it
node hedgefund.mjs run pairs_spread mine.json | jq '{summary, flags, flips, not_modeled, inputs_hash}'
```

In a script:

```js
import { run, template } from "./hedgefund.mjs";

const inputs = template("pairs_spread"); // replace every number in it
const { summary, flags, flips, not_modeled } = run("pairs_spread", inputs);
```
