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
Last updated