> For the complete documentation index, see [llms.txt](https://docs-v2.fermilabs.xyz/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://docs-v2.fermilabs.xyz/trading/margin-requirements.md).

# Margin Requirements

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:

```rust
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:

```rust
equity = collateral + sum(unrealized_pnl)
liquidatable = equity < maintenance_margin + reserved_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:

```rust
for position in account.positions:
    if position.base_position != 0:
        order = Order {
            order_id: LIQUIDATION_ORDER_START + counter,  // Bit 63 set
            side: opposite(position.direction),
            price: mark_price,
            quantity: abs(base_position),
        }
```

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:

```
pnl_per_unit = exit_price - entry_price  // for longs
pnl_per_unit = entry_price - exit_price  // for shorts
realized = pnl_per_unit * quantity_closed
```

#### Reserved Margin

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

```rust
// On order placement
reserve_margin_for_order(account, market, price, quantity)

// On fill
release_margin_reservation(account, required_initial_margin(notional))
```

This prevents over-commitment of margin to multiple orders.


---

# 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-v2.fermilabs.xyz/trading/margin-requirements.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.
