> For the complete documentation index, see [llms.txt](https://docs.turbine.exchange/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://docs.turbine.exchange/reference/technical/clearing-algorithm-apex.md).

# Clearing Algorithm (APEX)

APEX is Turbine's batch clearing algorithm. Each batch, APEX finds a single exchange rate for every token that maximizes settled volume. APEX draws on all open orders, [market maker quotes](broken://spaces/usbDsiWDhwRR4sob2ROl/pages/LgxN2qWJul4UFuMsotOv), [Turbine LP pools](broken://spaces/gxFOZ85U3R3v2cPka9gM/pages/u7zWyMUKXDnn1eSGg3BW), and every DEX pool indexed by Tycho. Every order in the batch clears at that rate minus the platform fee.

APEX is Turbine's Rust implementation of [SPEEDEX](https://www.scs.stanford.edu/~geoff/papers/speedex.pdf), a batch clearing algorithm originally developed at Stanford.

### How APEX solves a batch

APEX solves each batch in two steps.

**Price search.** Starting from current market prices, APEX adjusts token prices iteratively until it finds a set where supply and demand balance across the full orderbook. At each step it checks how orders respond to a price change and keeps adjusting until it minimizes the imbalance.

**Volume maximization.** With prices fixed, APEX runs a linear program that maximizes total trade volume, subject to every order filling at or better than its limit price. Orders that match peer-to-peer settle without touching DEX liquidity. Remaining imbalances route to on-chain pools.

### Ring-CoWs: multi-party circular trades

APEX solves the entire batch as one problem across all token pairs. This lets it find ring-CoWs: circular multi-party trades where Alice sells ETH for USDC, Bob sells USDC for WBTC, and Carol sells WBTC for ETH, all netting out at zero price impact without any DEX interaction.

Solvers that work one pair at a time miss these. APEX finds them because it sees the whole batch.

### Uniform clearing prices

All orders for the same token pair in the same batch clear at one price. If APEX sets a clearing rate of 3800 USDC/ETH, every order in that batch gets exactly 3800, whether the limit was 3800 or 3750. There is no queue advantage: the first order to arrive gets the same price as the last.

### DEX liquidity

APEX routes through all DEX pools indexed by Tycho, including Uniswap v2, v3, and v4, Curve, Balancer, and others. When peer-to-peer matching is not enough, remaining volume settles at the best available on-chain rate.


---

# 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.turbine.exchange/reference/technical/clearing-algorithm-apex.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.
