Cancel/Replace Priority Rule
Install and import#
npm install fintech-algorithmsimport { cancelReplacePriority } from "fintech-algorithms/matching-engines-and-venue-logic/order-lifecycle-and-queue-state/cancel-replace-priority-rule";Signature#
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 | Notes |
|---|---|---|
originalRaw | Order | The resting order before amendment. |
replacementRaw | Order | 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#
executed 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#
{
"order_id": "O-1",
"order_type": "displayed-limit",
"price": 100,
"remaining_quantity": 1000,
"priority_sequence": 42
}{
"order_id": "O-2",
"price": 100,
"remaining_quantity": 600,
"replace_sequence": 80
}Call#
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, …
{
"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#
This module also 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.
Diagrams#
Calculation flow#
Cancel/Replace Priority Rule calculation flow
flowchart LR
S1["Validate original displayedlimit scope"]
S2["Compare original and replacement price"]
S3["Compare replacement quantity with current leaves"]
S4["Preserve only when both canonical conditions pass"]
S5["Return effective sequence and reason"]
S1 --> S2
S2 --> S3
S3 --> S4
S4 --> S5
S5 --> D{"venueordertype eligibility for the preserve exception"}
D --> O["priority_preserved + diagnostics"]
O --> A["Audit: A newpriority result uses the later replace sequence"]
How it works#
This page states the contract — how to call it correctly. The article explains the concept: why it works, and where it breaks.
References#
- Order State Changes — FIX Trading Community
- Nasdaq TotalView-ITCH 5.0 Specification — Nasdaq
- Nasdaq Equity 4 — Equity Rules — The Nasdaq Stock Market LLC
- Evidence boundary