> For the complete documentation index, see [llms.txt](https://docs-v2.fermilabs.xyz/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://docs-v2.fermilabs.xyz/trading/ordering-and-inclusion.md).

# 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
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## 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, and the optional `goal` query parameter:

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

`ask` is the immediate question: it should be specific, self-contained, and written in natural language.
`goal` is optional and describes the broader end goal you are ultimately trying to accomplish on behalf of the user. GitBook uses it to tailor the answer towards what is most useful for that goal.

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.
