# Queue Imbalance

`D11-F03-A02` · Market Microstructure → Order-Flow and Impact · archetype `record-transform` · difficulty 3/5 · verification **contract**

Full page: https://docs.thefintechbuilder.com/market-microstructure/order-flow-and-impact/queue-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 { queueImbalance } from "fintech-algorithms/market-microstructure/order-flow-and-impact/queue-imbalance";
```

## Signature

```ts
queueImbalance(inputRows, levels, decay)
```

The share of depth resting on the bid versus the ask. A simple, strongly predictive feature for the direction of the next price move at very short horizons.

## Parameters

| Name | Type | Required | Notes |
| --- | --- | --- | --- |
| `inputRows` | `Row[]` | yes | Book observations with per-level sizes. |
| `levels` | `number` | yes | How many levels deep to include. One level is the classic definition; more captures a broader picture and dilutes the signal. · min: 1, integer: true |
| `decay` | `number` | yes | Weight decay applied to deeper levels, so a size far from the touch counts for less. · min: 0 |

## Returns

`{ imbalance, rows, levels, … }`

Imbalance per observation, ranging from −1 (all ask) to +1 (all bid).

## Errors

- When levels exceeds the depth supplied — throws

## Complexity

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

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

`inputRows`:

```json
[
  {
    "id": "Q001",
    "event_time_ns": 0,
    "bid_sizes": [600, 900, 1100],
    "ask_sizes": [600, 900, 1100]
  },
  {
    "id": "Q002",
    "event_time_ns": 1000000,
    "bid_sizes": [680.3444, 862.918, 1124.7214],
    "ask_sizes": [519.6556, 937.082, 1075.2786]
  },
  {
    "id": "Q003",
    "event_time_ns": 2000000,
    "bid_sizes": [752.8242, 829.4658, 1147.0228],
    "ask_sizes": [447.1758, 970.5342, 1052.9772]
  }
]
```

Showing 3 of 60 elements.

`levels`:

```json
1
```

`decay`:

```json
1
```

### Call

```ts
queueImbalance(inputRows, levels, decay)
```

### Returns

object with 11 fields: model, state, levels, decay, observation_count, valid_count, unknown_count, mean_imbalance, …

```json
{
  "model": "signed-queue-imbalance",
  "state": "estimated",
  "levels": 1,
  "decay": 1,
  "observation_count": 60,
  "valid_count": 60,
  "unknown_count": 0,
  "mean_imbalance": 0,
  "last_imbalance": -0.1339073333,
  "max_absolute_imbalance": 0.4333333333,
  "trace": [
    {
      "id": "Q001",
      "index": 0,
      "bid_depth": 600,
      "ask_depth": 600,
      "total_depth": 1200,
      "imbalance": 0,
      "side": "balanced",
      "reason": "normalized-depth"
    },
    {
      "id": "Q002",
      "index": 1,
      "bid_depth": 680.3444,
      "ask_depth": 519.6556,
      "total_depth": 1200,
      "imbalance": 0.1339073333,
      "side": "bid-heavy",
      "reason": "normalized-depth"
    },
    {
      "id": "Q003",
      "index": 2,
      "bid_depth": 752.8242,
      "ask_depth": 447.1758,
      "total_depth": 1200,
      "imbalance": 0.254707,
      "side": "bid-heavy",
      "reason": "normalized-depth"
    }
  ]
}
```

## Other exports

`orderFlowImbalance`, `kyleLambda`, `hasbrouckPriceImpact`, `pin`, `vpin`, `runTopic`. 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: **contract**.

The module loads, the entry point is callable and its declared signature matches the compiled code. The example below is real captured output, but no independently published figure asserts the numbers.

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/order-flow-and-impact/queue-imbalance/
- Implementation source: https://github.com/IslamBaraka90/Fintech-Algorithms-Library/blob/main/src/market-microstructure/order-flow-and-impact/queue-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
