# Depth Depletion and Replenishment

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

Full page: https://docs.thefintechbuilder.com/market-microstructure/market-depth-analytics/depth-depletion-and-replenishment/
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 { depthDepletionReplenishment } from "fintech-algorithms/market-microstructure/market-depth-analytics/depth-depletion-and-replenishment";
```

## Signature

```ts
depthDepletionReplenishment(seriesRaw)
```

Tracks depth being consumed and refilled over a sequence of snapshots. The pairing matters: depletion without replenishment is a book emptying out, which is the condition preceding a dislocation.

## Parameters

| Name | Type | Required | Notes |
| --- | --- | --- | --- |
| `seriesRaw` | `Snapshot[]` | yes | Sequential book snapshots with per-side depth. |

## Returns

`{ events, depletion_total, replenishment_total, net, … }`

Depletion and replenishment events with their net effect.

## Errors

- When fewer than two snapshots are supplied — throws

## Complexity

Time `O(snapshots × levels)`, space `O(events)`.

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

`seriesRaw`:

```json
[
  {
    "timestamp_seconds": 0,
    "quantity": 500
  },
  {
    "timestamp_seconds": 1,
    "quantity": 380
  },
  {
    "timestamp_seconds": 2,
    "quantity": 440
  }
]
```

Showing 3 of 6 elements.

### Call

```ts
depthDepletionReplenishment(seriesRaw)
```

### Returns

object with 10 fields: model, snapshot_count, start_quantity, end_quantity, gross_depletion, gross_replenishment, net_depth_change, replenishment_to_depletion, …

```json
{
  "model": "snapshot-visible-depth-change-decomposition",
  "snapshot_count": 6,
  "start_quantity": 500,
  "end_quantity": 420,
  "gross_depletion": 300,
  "gross_replenishment": 220,
  "net_depth_change": -80,
  "replenishment_to_depletion": 0.7333333333333333,
  "changes": [
    {
      "timestamp_seconds": 1,
      "previous_quantity": 500,
      "quantity": 380,
      "delta_quantity": -120,
      "classification": "depletion"
    },
    {
      "timestamp_seconds": 2,
      "previous_quantity": 380,
      "quantity": 440,
      "delta_quantity": 60,
      "classification": "replenishment"
    },
    {
      "timestamp_seconds": 3,
      "previous_quantity": 440,
      "quantity": 300,
      "delta_quantity": -140,
      "classification": "depletion"
    }
  ],
  "state": "net-depleting"
}
```

## Other exports

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