# Price-Time Priority

`D12-F01-A01` · Matching Engines and Venue Logic → Continuous Matching · archetype `record-transform` · difficulty 3/5 · verification **verified**

Full page: https://docs.thefintechbuilder.com/matching-engines-and-venue-logic/continuous-matching/price-time-priority/
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 { priceTimePriority } from "fintech-algorithms/matching-engines-and-venue-logic/continuous-matching/price-time-priority";
```

## Signature

```ts
priceTimePriority(incomingRaw, restingRaw)
```

Matches an incoming order against resting orders by best price, then earliest arrival. The default rule on most equity venues, and the one that makes queue position valuable. Prices and quantities are integer **atoms** — the venue's minimum increment — not floating-point currency. Matching arithmetic that rounds is matching arithmetic that disagrees with the exchange.

## Parameters

| Name | Type | Required | Notes |
| --- | --- | --- | --- |
| `incomingRaw` | `Order` | yes | The aggressing order. |
| `restingRaw` | `Order[]` | yes | Resting orders with prices, quantities and arrival sequence. |

## Returns

`{ fills, residual, book_after, … }`

Fills in priority order with any residual quantity and the resulting book.

## Errors

- When an order has non-integer atoms or a non-positive quantity — throws

## Complexity

Time `O(resting log 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": "B-IN",
  "side": "buy",
  "order_type": "limit",
  "quantity": 650,
  "limit_price": 100.1
}
```

`restingRaw`:

```json
[
  {
    "order_id": "S-1",
    "side": "sell",
    "price": 100,
    "remaining_quantity": 200,
    "arrival_sequence": 10
  },
  {
    "order_id": "S-2",
    "side": "sell",
    "price": 100,
    "remaining_quantity": 300,
    "arrival_sequence": 20
  },
  {
    "order_id": "S-3",
    "side": "sell",
    "price": 100.05,
    "remaining_quantity": 400,
    "arrival_sequence": 30
  }
]
```

Showing 3 of 4 elements.

### Call

```ts
priceTimePriority(incomingRaw, restingRaw)
```

### Returns

object with 10 fields: model, incoming_order_id, requested_quantity, filled_quantity, residual_quantity, average_fill_price, fill_count, fills, …

```json
{
  "model": "single-venue-price-time-priority",
  "incoming_order_id": "B-IN",
  "requested_quantity": 650,
  "filled_quantity": 650,
  "residual_quantity": 0,
  "average_fill_price": 100.01153846153846,
  "fill_count": 3,
  "fills": [
    {
      "resting_order_id": "S-1",
      "price": 100,
      "quantity": 200,
      "arrival_sequence": 10
    },
    {
      "resting_order_id": "S-2",
      "price": 100,
      "quantity": 300,
      "arrival_sequence": 20
    },
    {
      "resting_order_id": "S-3",
      "price": 100.05,
      "quantity": 150,
      "arrival_sequence": 30
    }
  ],
  "final_resting_orders": [
    {
      "order_id": "S-3",
      "side": "sell",
      "price": 100.05,
      "remaining_quantity": 250,
      "arrival_sequence": 30
    },
    {
      "order_id": "S-4",
      "side": "sell",
      "price": 100.1,
      "remaining_quantity": 500,
      "arrival_sequence": 40
    }
  ],
  "state": "filled"
}
```

## Other exports

`proRataMatching`, `sizeTimePriority`, `hybridProRataTime`, `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/continuous-matching/price-time-priority/
- Implementation source: https://github.com/IslamBaraka90/Fintech-Algorithms-Library/blob/main/src/matching-engines-and-venue-logic/continuous-matching/price-time-priority/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
