# Multi-Level Sweep Cost and Slippage

`D11-F05-A05` · Market Microstructure → Market-Depth Analytics · archetype `record-transform` · difficulty 3/5 · verification **verified**

Full page: https://docs.thefintechbuilder.com/market-microstructure/market-depth-analytics/multi-level-sweep-cost-and-slippage/
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 { sweepCostAndSlippage } from "fintech-algorithms/market-microstructure/market-depth-analytics/multi-level-sweep-cost-and-slippage";
```

## Signature

```ts
sweepCostAndSlippage(bids, asks, side, quantity, benchmarkRaw, benchmarkPriceRaw, limitPrice)
```

The cost of sweeping multiple levels, measured against a chosen benchmark. Slippage is only meaningful relative to a benchmark, so which one is a parameter rather than an assumption.

## Parameters

| Name | Type | Required | Notes |
| --- | --- | --- | --- |
| `bids` | `Level[]` | yes | Bid levels. |
| `asks` | `Level[]` | yes | Ask levels. |
| `side` | `"buy" \| "sell"` | yes | Order direction. |
| `quantity` | `number` | yes | Order quantity. · min: 0 |
| `benchmarkRaw` | `string` | yes | Which benchmark to measure against — touch, midpoint or a supplied price. |
| `benchmarkPriceRaw` | `number` | no | The benchmark price when one is supplied explicitly. |
| `limitPrice` | `number` | no | Optional limit price. |

## Returns

`{ sweep_cost, slippage_bps, average_price, benchmark_price, … }`

Cost and slippage in basis points with the benchmark actually used.

## Errors

- When the benchmark is unrecognised, or requires a price that was not supplied — throws

## Complexity

Time `O(levels)`, 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

`bids`:

```json
[
  {
    "price": 99.99,
    "quantity": 500
  },
  {
    "price": 99.98,
    "quantity": 800
  },
  {
    "price": 99.97,
    "quantity": 1200
  }
]
```

Showing 3 of 5 elements.

`asks`:

```json
[
  {
    "price": 100.01,
    "quantity": 300
  },
  {
    "price": 100.02,
    "quantity": 600
  },
  {
    "price": 100.03,
    "quantity": 1000
  }
]
```

Showing 3 of 5 elements.

`side`:

```json
"buy"
```

`quantity`:

```json
2600
```

`benchmarkRaw`:

```json
"midpoint"
```

`benchmarkPriceRaw`:

```json
null
```

`limitPrice`:

```json
null
```

### Call

```ts
sweepCostAndSlippage(bids, asks, side, quantity, benchmarkRaw, benchmarkPriceRaw, limitPrice)
```

### Returns

object with 20 fields: model, side, requested_quantity, filled_quantity, unfilled_quantity, full_fill, fills, fill_notional, …

```json
{
  "model": "visible-book-sweep-cost",
  "side": "buy",
  "requested_quantity": 2600,
  "filled_quantity": 2600,
  "unfilled_quantity": 0,
  "full_fill": true,
  "fills": [
    {
      "level_index": 0,
      "price": 100.01,
      "quantity": 300,
      "notional": 30003
    },
    {
      "level_index": 1,
      "price": 100.02,
      "quantity": 600,
      "notional": 60012
    },
    {
      "level_index": 2,
      "price": 100.03,
      "quantity": 1000,
      "notional": 100030
    }
  ],
  "fill_notional": 260073,
  "partial_vwap": 100.02807692307692,
  "worst_fill_price": 100.04,
  "best_bid": 99.99,
  "best_ask": 100.01,
  "midpoint": 100,
  "benchmark": "midpoint"
}
```

Showing 14 of 20 fields.

## Other exports

`cumulativeDepth`, `topNDepthImbalance`, `depthAtDistanceProfile`, `expectedFillPrice`, `liquidityWallConcentration`, `depthDepletionReplenishment`, `marketDepthHeatmap`, `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/market-depth-analytics/multi-level-sweep-cost-and-slippage/
- Implementation source: https://github.com/IslamBaraka90/Fintech-Algorithms-Library/blob/main/src/market-microstructure/market-depth-analytics/multi-level-sweep-cost-and-slippage/impl.ts
- Package on npm: https://www.npmjs.com/package/fintech-algorithms
- Domain index for agents: https://docs.thefintechbuilder.com/market-microstructure/llms.txt
