# Partial-Fill and Residual-Quantity Processing

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

Full page: https://docs.thefintechbuilder.com/matching-engines-and-venue-logic/order-lifecycle-and-queue-state/partial-fill-and-residual-quantity-processing/
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 { partialFillResidual } from "fintech-algorithms/matching-engines-and-venue-logic/order-lifecycle-and-queue-state/partial-fill-and-residual-quantity-processing";
```

## Signature

```ts
partialFillResidual(orderQuantityRaw, fillsRaw)
```

Tracks cumulative fills against an order and computes the residual. Over-fill is impossible on a correct venue, so detecting it is detecting a defect rather than handling a case.

## Parameters

| Name | Type | Required | Notes |
| --- | --- | --- | --- |
| `orderQuantityRaw` | `number` | yes | Original order quantity. · min: 1, integer: true |
| `fillsRaw` | `Fill[]` | yes | Fills in sequence, each with a quantity. |

## Returns

`{ filled, residual, over_filled, fills, … }`

Cumulative filled and residual quantity, with an explicit over-fill flag.

## Errors

- When a fill quantity is not positive — throws

## Complexity

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

`orderQuantityRaw`:

```json
1200
```

`fillsRaw`:

```json
[
  {
    "execution_id": "E-1",
    "quantity": 300,
    "price": 100
  },
  {
    "execution_id": "E-2",
    "quantity": 450,
    "price": 100.01
  },
  {
    "execution_id": "E-3",
    "quantity": 250,
    "price": 100.02
  }
]
```

### Call

```ts
partialFillResidual(orderQuantityRaw, fillsRaw)
```

### Returns

object with 10 fields: model, order_quantity, cumulative_quantity, leaves_quantity, fill_notional, average_fill_price, execution_count, fill_states, …

```json
{
  "model": "partial-fill-residual-accounting",
  "order_quantity": 1200,
  "cumulative_quantity": 1000,
  "leaves_quantity": 200,
  "fill_notional": 100009.5,
  "average_fill_price": 100.0095,
  "execution_count": 3,
  "fill_states": [
    {
      "execution_id": "E-1",
      "last_quantity": 300,
      "last_price": 100,
      "cumulative_quantity": 300,
      "leaves_quantity": 900,
      "status": "partially-filled"
    },
    {
      "execution_id": "E-2",
      "last_quantity": 450,
      "last_price": 100.01,
      "cumulative_quantity": 750,
      "leaves_quantity": 450,
      "status": "partially-filled"
    },
    {
      "execution_id": "E-3",
      "last_quantity": 250,
      "last_price": 100.02,
      "cumulative_quantity": 1000,
      "leaves_quantity": 200,
      "status": "partially-filled"
    }
  ],
  "status": "partially-filled",
  "state": "partially-filled"
}
```

## Other exports

`limitOrderLifecycle`, `cancelReplacePriority`, `queuePositionAheadVolume`, `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/partial-fill-and-residual-quantity-processing/
- Implementation source: https://github.com/IslamBaraka90/Fintech-Algorithms-Library/blob/main/src/matching-engines-and-venue-logic/order-lifecycle-and-queue-state/partial-fill-and-residual-quantity-processing/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
