> 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 by asking a question.

Perform an HTTP GET request on the following URL with the `ask` and `goal` query parameters:

```
GET https://docs.afx.xyz/trading-rules/order-matching.md?ask=<question>&goal=<user_goal>
```

`ask` is the immediate question: it should be specific, self-contained, and written in natural language.
`goal` is what the user is ultimately trying to achieve, the reason they need the answer. Sharing it helps GitBook give you a better, more relevant answer. A goal is most helpful when it describes the outcome the user wants rather than restating the question. For example, with `ask=how do I create an API token`, a goal like `automate deployments from our CI pipeline` lets GitBook tailor the answer to that use case.

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.
