# Marketable-Order Multi-Level Sweep

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

Full page: https://docs.thefintechbuilder.com/matching-engines-and-venue-logic/order-lifecycle-and-queue-state/marketable-order-multi-level-sweep/
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 { marketableOrderSweep } from "fintech-algorithms/matching-engines-and-venue-logic/order-lifecycle-and-queue-state/marketable-order-multi-level-sweep";
```

## Signature

```ts
marketableOrderSweep(incomingRaw, restingRaw)
```

Walks a marketable order across price levels until filled or exhausted — the matching-engine counterpart of the expected-fill-price calculation, producing actual fills rather than an estimate.

## Parameters

| Name | Type | Required | Notes |
| --- | --- | --- | --- |
| `incomingRaw` | `Order` | yes | The marketable order. |
| `restingRaw` | `Order[]` | yes | Resting orders across levels, in priority order. |

## Returns

`{ fills, levels_swept, average_price, residual, … }`

Fills level by level with the residual left unfilled.

## Errors

- When the incoming order has a non-positive quantity — throws

## Complexity

Time `O(resting)`, space `O(fills)`.

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

`incomingRaw`:

```json
{
  "order_id": "IN-1",
  "side": "buy",
  "order_type": "limit",
  "quantity": 900,
  "limit_price": 100.02,
  "time_in_force": "GTC",
  "arrival_sequence": 100
}
```

`restingRaw`:

```json
[
  {
    "order_id": "B-1",
    "side": "buy",
    "price": 99.99,
    "remaining_quantity": 500,
    "priority_sequence": 1
  },
  {
    "order_id": "S-1",
    "side": "sell",
    "price": 100.01,
    "remaining_quantity": 300,
    "priority_sequence": 2
  },
  {
    "order_id": "S-2",
    "side": "sell",
    "price": 100.01,
    "remaining_quantity": 400,
    "priority_sequence": 3
  }
]
```

Showing 3 of 5 elements.

### Call

```ts
marketableOrderSweep(incomingRaw, restingRaw)
```

### Returns

object with 13 fields: model, incoming_order_id, incoming_side, requested_quantity, filled_quantity, residual_quantity, fill_notional, average_fill_price, …

```json
{
  "model": "single-venue-price-time-marketable-sweep",
  "incoming_order_id": "IN-1",
  "incoming_side": "buy",
  "requested_quantity": 900,
  "filled_quantity": 900,
  "residual_quantity": 0,
  "fill_notional": 90011,
  "average_fill_price": 100.01222222222222,
  "fills": [
    {
      "resting_order_id": "S-1",
      "price": 100.01,
      "quantity": 300,
      "resting_remaining_quantity": 0
    },
    {
      "resting_order_id": "S-2",
      "price": 100.01,
      "quantity": 400,
      "resting_remaining_quantity": 0
    },
    {
      "resting_order_id": "S-3",
      "price": 100.02,
      "quantity": 200,
      "resting_remaining_quantity": 300
    }
  ],
  "residual_action": "none",
  "final_book": [
    {
      "order_id": "B-1",
      "side": "buy",
      "price": 99.99,
      "remaining_quantity": 500,
      "priority_sequence": 1
    },
    {
      "order_id": "S-3",
      "side": "sell",
      "price": 100.02,
      "remaining_quantity": 300,
      "priority_sequence": 4
    },
    {
      "order_id": "S-4",
      "side": "sell",
      "price": 100.03,
      "remaining_quantity": 600,
      "priority_sequence": 5
    }
  ],
  "status": "filled",
  "state": "filled"
}
```

## Other exports

`limitOrderLifecycle`, `cancelReplacePriority`, `partialFillResidual`, `queuePositionAheadVolume`, `icebergReplenishment`, `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/marketable-order-multi-level-sweep/
- Implementation source: https://github.com/IslamBaraka90/Fintech-Algorithms-Library/blob/main/src/matching-engines-and-venue-logic/order-lifecycle-and-queue-state/marketable-order-multi-level-sweep/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
