# Realized Spread

`D11-F02-A03` · Market Microstructure → Liquidity and Spreads · archetype `record-transform` · difficulty 4/5 · verification **verified**

Full page: https://docs.thefintechbuilder.com/market-microstructure/liquidity-and-spreads/realized-spread/
Agent skill: `npx skills add IslamBaraka90/Fintech-Algorithms-Library` — https://docs.thefintechbuilder.com/guides/agent-skill/

## Install and import

```bash
npm install fintech-algorithms
```

```ts
import { realizedSpread } from "fintech-algorithms/market-microstructure/liquidity-and-spreads/realized-spread";
```

## Signature

```ts
realizedSpread(bidAtTrade, askAtTrade, tradePrice, side, bidAfter, askAfter, horizonSeconds)
```

The effective spread measured against the midpoint *after* a horizon, which strips out the permanent price impact and leaves what the liquidity provider actually earned. The horizon choice is a modelling decision, not a detail.

## Parameters

| Name | Type | Required | Notes |
| --- | --- | --- | --- |
| `bidAtTrade` | `number` | yes | Best bid at execution. |
| `askAtTrade` | `number` | yes | Best ask at execution. |
| `tradePrice` | `number` | yes | Executed price. |
| `side` | `"buy" \| "sell"` | yes | Trade direction. |
| `bidAfter` | `number` | yes | Best bid after the horizon. |
| `askAfter` | `number` | yes | Best ask after the horizon. |
| `horizonSeconds` | `number` | yes | Seconds after the trade at which the later midpoint is taken. Five minutes is conventional; shorter horizons attribute more of the move to impact. · min: 0 |

## Returns

`{ realized_spread, price_impact, effective_spread, … }`

The realized spread with the price impact it decomposes against — the two sum to the effective spread.

## Errors

- When horizonSeconds is negative, or a quote is invalid — throws

## Complexity

Time `O(1)`, space `O(1)`.

## Worked example

Captured by running this function on the input its own test provides. Real output of real code — but not asserted against a published figure.

### Input

`bidAtTrade`:

```json
100
```

`askAtTrade`:

```json
100.08
```

`tradePrice`:

```json
100.07
```

`side`:

```json
"buy"
```

`bidAfter`:

```json
100.11
```

`askAfter`:

```json
100.19
```

`horizonSeconds`:

```json
300
```

### Call

```ts
realizedSpread(bidAtTrade, askAtTrade, tradePrice, side, bidAfter, askAfter, horizonSeconds)
```

### Returns

object with 18 fields: model, side, direction, trade_price, midpoint_at_trade, midpoint_after, bid_after, ask_after, …

```json
{
  "model": "realized-spread",
  "side": "buy",
  "direction": 1,
  "trade_price": 100.07,
  "midpoint_at_trade": 100.03999999999999,
  "midpoint_after": 100.15,
  "bid_after": 100.11,
  "ask_after": 100.19,
  "horizon_seconds": 300,
  "effective_spread": 0.060000000000002274,
  "realized_spread": -0.160000000000025,
  "realized_spread_relative": -0.0015993602558978912,
  "realized_spread_bps": -15.993602558978912,
  "price_impact": 0.22000000000002728
}
```

Showing 14 of 18 fields.

## Other exports

`quotedSpread`, `effectiveSpread`, `rollSpread`, `amihudIlliquidity`, `corwinSchultzSpread`, `calculate`. Every module additionally exports `run` as an alias of its primary
function, and a `meta` object carrying its catalog id, domain, family, shape and article URL.

## Verification and provenance

Tier: **verified** (via D).

The worked example below is the figure published in this algorithm's article, replayed and asserted by the test suite on every build. The arithmetic cannot drift without the build failing.

Both tiers guarantee the signature. Full explanation: https://docs.thefintechbuilder.com/guides/verification/

Generated from the docs.json payload shipped inside fintech-algorithms@0.13.1.
The signature and parameter list are checked against the compiled implementation at build time,
so a description that contradicts the code fails the build rather than reaching this file.

## Links

- Article (how it works, step by step): https://thefintechbuilder.com/market-microstructure/liquidity-and-spreads/realized-spread/
- Implementation source: https://github.com/IslamBaraka90/Fintech-Algorithms-Library/blob/main/src/market-microstructure/liquidity-and-spreads/realized-spread/impl.ts
- Package on npm: https://www.npmjs.com/package/fintech-algorithms
- Domain index for agents: https://docs.thefintechbuilder.com/market-microstructure/llms.txt
