For the complete documentation index, see llms.txt. This page is also available as Markdown.

Margin Requirements

Clarifies how the trading system handles margin

Risk management is handled through liquidations. The current implementation uses direct liquidation orders rather than a separate insurance fund or ADL mechanism.

Margin Requirements

Two margin thresholds exist per market:

Margin Type
Purpose
Typical Ratio

Initial

Required to open positions

10% (1000 bps)

Maintenance

Minimum to avoid liquidation

5% (500 bps)

Margin requirements are calculated as:

required = notional * margin_ratio / 10,000
notional = abs(base_position) * mark_price

Tiered Leverage

Large positions face higher margin requirements:

LeverageTier {
    notional: u64,              // Position size threshold
    initial_margin_ratio: u64,
    maintenance_margin_ratio: u64,
}

When position notional exceeds a tier threshold, the higher ratios apply.

Liquidation Detection

Accounts are checked against maintenance margin:

Where:

  • unrealized_pnl = base_position * mark_price - quote_spent

  • reserved_margin = margin locked for open orders

Liquidation Order Generation

For each under-margined account:

Liquidation orders:

  • Have order IDs starting at 1 << 63 (bit 63 set) for identification

  • Are priced at current mark price

  • Close the entire position

  • Process through the normal matching engine

Position Lifecycle

Trades affect positions in three ways:

Effect
Condition
Behavior

Opening

No position or same direction

Increases base_position, adds to quote_spent

Reducing

Opposite direction, partial close

Decreases position, realizes PnL

Flipping

Opposite direction, exceeds position

Closes position, opens opposite side

Realized PnL calculation on close:

Reserved Margin

Margin is reserved when orders are placed and released when filled:

This prevents over-commitment of margin to multiple orders.

Last updated