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

# Order Matching

AFX uses a central limit order book (CLOB) matching engine with on-chain verifiable settlement.

## Matching Rules

Orders are matched using **price-time priority**:

1. **Price priority** — The best priced orders are matched first. Buy orders at higher prices and sell orders at lower prices have priority.
2. **Time priority** — Among orders at the same price, earlier orders are matched first.

When a buy price ≥ sell price, the orders are matched. The trade executes at the **maker's price** (the price of the resting order in the book).

## Last Traded Price

The Last Price is determined by the first trade in the order book — the intersection of the highest buy price and the lowest sell price.

## Self-Trade Prevention (STP)

AFX implements platform-level self-trade prevention. Self-trades are detected and blocked automatically.

**Detection criteria:**

* Both orders originate from the same user's contract address.
* A new taker order enters at a price that would match against the user's own resting maker order.

**Handling:** Cancel Newest — The incoming taker order is rejected (not matched) if it would result in a self-trade. The existing maker order in the book remains unchanged. This approach maintains order book stability and is compatible with on-chain verification.


---

# 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:

```
GET https://docs.afx.xyz/trading-rules/order-matching.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.
