# Cumulative Bid/Ask Depth

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

Full page: https://docs.thefintechbuilder.com/market-microstructure/market-depth-analytics/cumulative-bid-ask-depth/
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 { cumulativeDepth } from "fintech-algorithms/market-microstructure/market-depth-analytics/cumulative-bid-ask-depth";
```

## Signature

```ts
cumulativeDepth(bidsRaw, asksRaw, tickRaw)
```

Running total of quantity available as you walk out from the touch on each side — the basis of every question about how much can be traded before price moves.

## Parameters

| Name | Type | Required | Notes |
| --- | --- | --- | --- |
| `bidsRaw` | `Level[]` | yes | Bid levels, best first. |
| `asksRaw` | `Level[]` | yes | Ask levels, best first. |
| `tickRaw` | `number` | yes | Tick size, used to express distance in ticks rather than currency. · min: 0 |

## Returns

`{ bids, asks, total_bid_depth, total_ask_depth, … }`

Cumulative depth per level on each side.

## Errors

- When tick size is not positive, or a level has negative size — throws

## Complexity

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

## 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

`bidsRaw`:

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

Showing 3 of 5 elements.

`asksRaw`:

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

Showing 3 of 5 elements.

`tickRaw`:

```json
0.01
```

### Call

```ts
cumulativeDepth(bidsRaw, asksRaw, tickRaw)
```

### Returns

object with 10 fields: model, best_bid, best_ask, tick_size, spread_ticks, bid_levels, ask_levels, total_bid_depth, …

```json
{
  "model": "visible-cumulative-depth",
  "best_bid": 99.99,
  "best_ask": 100.01,
  "tick_size": 0.01,
  "spread_ticks": 2,
  "bid_levels": [
    {
      "price": 99.99,
      "quantity": 500,
      "distance_ticks": 0,
      "cumulative_quantity": 500
    },
    {
      "price": 99.98,
      "quantity": 800,
      "distance_ticks": 1,
      "cumulative_quantity": 1300
    },
    {
      "price": 99.97,
      "quantity": 1200,
      "distance_ticks": 2,
      "cumulative_quantity": 2500
    }
  ],
  "ask_levels": [
    {
      "price": 100.01,
      "quantity": 300,
      "distance_ticks": 0,
      "cumulative_quantity": 300
    },
    {
      "price": 100.02,
      "quantity": 600,
      "distance_ticks": 1,
      "cumulative_quantity": 900
    },
    {
      "price": 100.03,
      "quantity": 1000,
      "distance_ticks": 2,
      "cumulative_quantity": 1900
    }
  ],
  "total_bid_depth": 3600,
  "total_ask_depth": 3300,
  "state": "bid-deeper"
}
```

## Other exports

`topNDepthImbalance`, `depthAtDistanceProfile`, `expectedFillPrice`, `sweepCostAndSlippage`, `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/cumulative-bid-ask-depth/
- Implementation source: https://github.com/IslamBaraka90/Fintech-Algorithms-Library/blob/main/src/market-microstructure/market-depth-analytics/cumulative-bid-ask-depth/impl.ts
- Package on npm: https://www.npmjs.com/package/fintech-algorithms
- Domain index for agents: https://docs.thefintechbuilder.com/market-microstructure/llms.txt
