# Queue Position and Ahead-Volume Calculation

`D12-F04-A04` · Matching Engines and Venue Logic → Order Lifecycle and Queue State · archetype `record-transform` · difficulty 4/5 · verification **verified**

Full page: https://docs.thefintechbuilder.com/matching-engines-and-venue-logic/order-lifecycle-and-queue-state/queue-position-and-ahead-volume-calculation/
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 { queuePositionAheadVolume } from "fintech-algorithms/matching-engines-and-venue-logic/order-lifecycle-and-queue-state/queue-position-and-ahead-volume-calculation";
```

## Signature

```ts
queuePositionAheadVolume(ordersRaw, targetIdRaw)
```

How much volume sits ahead of a given order at its price. This is the number that decides fill probability under price-time priority — position in the queue, not distance from the touch.

## Parameters

| Name | Type | Required | Notes |
| --- | --- | --- | --- |
| `ordersRaw` | `Order[]` | yes | Resting orders at the price, in priority order. |
| `targetIdRaw` | `string` | yes | The order whose position is wanted. |

## Returns

`{ position, ahead_volume, behind_volume, total, … }`

Position with volume ahead and behind.

## Errors

- When the target order is not in the queue — throws

## Complexity

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

`ordersRaw`:

```json
[
  {
    "order_id": "Q-1",
    "side": "buy",
    "price": 100,
    "remaining_quantity": 300,
    "arrival_sequence": 1,
    "displayed": true
  },
  {
    "order_id": "H-1",
    "side": "buy",
    "price": 100,
    "remaining_quantity": 1000,
    "arrival_sequence": 2,
    "displayed": false
  },
  {
    "order_id": "Q-2",
    "side": "buy",
    "price": 100,
    "remaining_quantity": 450,
    "arrival_sequence": 3,
    "displayed": true
  }
]
```

Showing 3 of 6 elements.

`targetIdRaw`:

```json
"TARGET"
```

### Call

```ts
queuePositionAheadVolume(ordersRaw, targetIdRaw)
```

### Returns

object with 12 fields: model, target_order_id, side, price, ahead_order_count, ahead_volume, displayed_queue_rank, behind_order_count, …

```json
{
  "model": "displayed-price-time-fifo-ahead-volume",
  "target_order_id": "TARGET",
  "side": "buy",
  "price": 100,
  "ahead_order_count": 2,
  "ahead_volume": 750,
  "displayed_queue_rank": 3,
  "behind_order_count": 1,
  "behind_volume": 250,
  "hidden_same_price_quantity_excluded": 1000,
  "ahead_order_ids": ["Q-1", "Q-2"],
  "state": "queued-behind-displayed-volume"
}
```

## Other exports

`limitOrderLifecycle`, `cancelReplacePriority`, `partialFillResidual`, `icebergReplenishment`, `marketableOrderSweep`, `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/matching-engines-and-venue-logic/order-lifecycle-and-queue-state/queue-position-and-ahead-volume-calculation/
- Implementation source: https://github.com/IslamBaraka90/Fintech-Algorithms-Library/blob/main/src/matching-engines-and-venue-logic/order-lifecycle-and-queue-state/queue-position-and-ahead-volume-calculation/impl.ts
- Package on npm: https://www.npmjs.com/package/fintech-algorithms
- Domain index for agents: https://docs.thefintechbuilder.com/matching-engines-and-venue-logic/llms.txt
