# Top-N Depth Imbalance

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

Full page: https://docs.thefintechbuilder.com/market-microstructure/market-depth-analytics/top-n-depth-imbalance/
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 { topNDepthImbalance } from "fintech-algorithms/market-microstructure/market-depth-analytics/top-n-depth-imbalance";
```

## Signature

```ts
topNDepthImbalance(bidsRaw, asksRaw, nRaw, thresholdRaw)
```

Depth imbalance restricted to the top N levels — the same idea as queue imbalance, bounded to the part of the book that is realistically accessible.

## Parameters

| Name | Type | Required | Notes |
| --- | --- | --- | --- |
| `bidsRaw` | `Level[]` | yes | Bid levels, best first. |
| `asksRaw` | `Level[]` | yes | Ask levels, best first. |
| `nRaw` | `number` | yes | Levels to include. · min: 1, integer: true |
| `thresholdRaw` | `number` | yes | Magnitude above which the imbalance is flagged as significant. · min: 0 |

## Returns

`{ imbalance, bid_depth, ask_depth, significant, … }`

The imbalance with the depths behind it and whether it cleared the threshold.

## Errors

- When nRaw exceeds the levels supplied — throws

## Complexity

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

`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.

`nRaw`:

```json
3
```

`thresholdRaw`:

```json
0.1
```

### Call

```ts
topNDepthImbalance(bidsRaw, asksRaw, nRaw, thresholdRaw)
```

### Returns

object with 9 fields: model, n, bid_depth, ask_depth, total_depth, imbalance, threshold, state, …

```json
{
  "model": "top-n-visible-depth-imbalance",
  "n": 3,
  "bid_depth": 2500,
  "ask_depth": 1900,
  "total_depth": 4400,
  "imbalance": 0.13636363636363635,
  "threshold": 0.1,
  "state": "bid-heavy",
  "complete_window": true
}
```

## Other exports

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