# Ordering & Inclusion

Order sequencing is deterministic, derived from the Continuum sequencing layer.

#### Continuum Sequence

Every order receives a globally unique sequence number:

```
continuum_sequence = (tick_number << 32) | sequence_in_tick
```

* `tick_number` — Block height from Continuum (VDF-proven)
* `sequence_in_tick` — Position within that tick (0-indexed)

This replaces timestamp-based ordering to ensure all nodes process orders identically.

#### Order Structure

```rust
TickOrder {
    order_id: OrderId,
    market_id: MarketId,
    account_id: AccountId,
    side: Side,                    // Buy | Sell
    price: i64,
    quantity: u64,

    // Sequencing
    tick_number: u64,
    sequence_in_tick: u64,
    continuum_sequence: u64,       // PRIMARY KEY

    // Perpetuals
    leverage: Option<u64>,
    position_effect: Option<PositionEffect>,  // Open | Close
    reduce_only: bool,
    margin_mode: Option<MarginMode>,          // Cross | Isolated
    liquidation: bool,             // System-generated liquidation
}
```

#### Inclusion Rules

1. **Zero-quantity rejection** — Orders with `quantity == 0` are rejected at insertion
2. **Margin validation** — Orders must pass `reserve_margin_for_order()` before inclusion
3. **Signature verification** — All orders are Ed25519-signed; invalid signatures rejected

#### Priority Queue Processing

Orders are processed via a binary min-heap keyed by `continuum_sequence`:

```
price_updates → triggers → regular_orders → liquidations
```

Within each category, earlier sequences execute first (FIFO from Continuum).

#### Cancellation Handling

Cancellations use the same `continuum_sequence` ordering:

```rust
TickCancellation {
    order_id: OrderId,
    continuum_sequence: u64,
}
```

Cancels are processed in sequence order alongside new orders, ensuring deterministic interleaving.

***


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://docs-v2.fermilabs.xyz/trading/ordering-and-inclusion.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
