# Cancel/Replace Priority Rule

`D12-F04-A02` · 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/cancel-replace-priority-rule/
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 { cancelReplacePriority } from "fintech-algorithms/matching-engines-and-venue-logic/order-lifecycle-and-queue-state/cancel-replace-priority-rule";
```

## Signature

```ts
cancelReplacePriority(originalRaw, replacementRaw)
```

Decides whether an amendment keeps queue position. Reducing quantity usually keeps it; raising quantity or changing price loses it. This one rule determines whether an amend is nearly free or extremely expensive.

## Parameters

| Name | Type | Required | Notes |
| --- | --- | --- | --- |
| `originalRaw` | `Order` | yes | The resting order before amendment. |
| `replacementRaw` | `Order` | yes | The requested replacement. |

## Returns

`{ priority_retained, reason, new_sequence, … }`

Whether priority survives and the reason — the reason is what a participant needs to design around.

## Errors

- When the replacement changes side or instrument — throws

## Complexity

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

`originalRaw`:

```json
{
  "order_id": "O-1",
  "order_type": "displayed-limit",
  "price": 100,
  "remaining_quantity": 1000,
  "priority_sequence": 42
}
```

`replacementRaw`:

```json
{
  "order_id": "O-2",
  "price": 100,
  "remaining_quantity": 600,
  "replace_sequence": 80
}
```

### Call

```ts
cancelReplacePriority(originalRaw, replacementRaw)
```

### Returns

object with 14 fields: model, original_order_id, replacement_order_id, original_price, replacement_price, original_remaining_quantity, replacement_remaining_quantity, same_price, …

```json
{
  "model": "nasdaq-style-displayed-limit-priority-rule",
  "original_order_id": "O-1",
  "replacement_order_id": "O-2",
  "original_price": 100,
  "replacement_price": 100,
  "original_remaining_quantity": 1000,
  "replacement_remaining_quantity": 600,
  "same_price": true,
  "decrease_only": true,
  "priority_preserved": true,
  "original_priority_sequence": 42,
  "effective_priority_sequence": 42,
  "reason": "same-price quantity decrease preserves canonical displayed-order priority",
  "state": "priority-preserved"
}
```

## Other exports

`limitOrderLifecycle`, `partialFillResidual`, `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/cancel-replace-priority-rule/
- Implementation source: https://github.com/IslamBaraka90/Fintech-Algorithms-Library/blob/main/src/matching-engines-and-venue-logic/order-lifecycle-and-queue-state/cancel-replace-priority-rule/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
