> For the complete documentation index, see [llms.txt](https://docs.drake.exchange/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://docs.drake.exchange/trading/hybrid-clob-+-amm/trade-execution-flow.md).

# Trade Execution Flow

When you place a market order, the following sequence occurs:

**Step 1: Order Validation**

The system performs critical pre-flight checks:

* Verify portfolio ownership and trading permissions
* Check instrument is tradeable and not in emergency mode
* Validate order size meets minimum requirements and doesn't exceed position limits
* Confirm sufficient margin available for the trade
* Verify order price (if specified) falls within allowed price bands

**Step 2: Price Fetching**

Current market price is fetched from the OracleAggregator:

* Multiple oracle sources queried (Pyth, Chainlink)
* Prices normalized and standardized
* Stale or invalid prices filtered out
* Simple average calculated for final price

**Step 3: Venue Routing Decision**

Hybrid Order Router analyzes market conditions and determines optimal execution split:

Market Analysis:

* **Vault Utilization**: How much vault capital is currently deployed
* **OI Imbalance**: Difference between total longs vs shorts
* **Vault Liquidity**: Available capacity for new positions
* **Order Book Depth**: Resting order volume at best prices

Venue Analysis:

* **Vault Price**: Oracle price + spread, estimated slippage
* **Order Book Price**: Best bid/ask, available volume, estimated slippage

**Step 4: Execution**

Order is split according to router decision (e.g., 70% Vault / 30% Order Book):

For Vault Portion:

* Trade executed at oracle price ± spread
* Vault assumes opposite position
* Trading fee charged to user
* Open interest updated

For Order Book Portion:

* Matched against best available resting orders
* Price-time priority determines fill sequence
* Maker/taker fees applied
* Orders removed or reduced from book

**Step 5: Position Update**

Your portfolio reflects the executed trade:

* Position size increased/decreased by filled amount
* Entry price calculated as weighted average of fills
* Unrealized PnL begins tracking from entry price
* Margin requirements updated
* Open interest recorded in OIAndPnLManager

**Step 6: Settlement**

Final accounting and event logging:

* Fees deducted from collateral
* Portfolio balances updated
* Trade recorded in order history
* Events emitted for transaction record


---

# 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.drake.exchange/trading/hybrid-clob-+-amm/trade-execution-flow.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.
