> ## Documentation Index
> Fetch the complete documentation index at: https://docs.0x0.exchange/llms.txt
> Use this file to discover all available pages before exploring further.

# Margin

Spectre operates exclusively in **Isolated Margin** mode. This means that margin is dedicated to each individual position, protecting your wider portfolio from a single bad trade.

## How it Works

In Isolated Margin mode, the maximum amount you can transfer out of a position is limited by the maintenance margin required to keep that specific position open.

### Formulas

Spectre uses the following logic to calculate margin requirements and transfer limits.

**Initial Margin Calculation:**

$$
\text{Margin} = \frac{\text{Quantity} \times \text{Price}}{\text{Leverage}}
$$

**Maximum Transfer-In:**
Transfering funds *into* a position increases its safety buffer.

$$
\text{Max In} = \min(\text{Wallet Balance} - \sum \text{Other Margins}, \text{Available Balance})
$$

**Maximum Transfer-Out:**
Transfering funds *out* reduces the safety buffer and increases liquidation risk.

$$
\text{Max Out} = \max(0, \min(\text{Position Balance} - \text{Maintenance Margin}, \text{PnL Adjusted Balance}))
$$

> **Note**: Transferring margin out reduces the buffer protecting your position. Removing too much margin can drastically increase your risk of liquidation.

## Risk Checks

The engine performs the following checks before accepting any new order or modification:

1. **New Order**: Checks if `Available Margin >= (Qty * Price) / Leverage`.
2. **Modify Order**: Checks if the *additional* margin required (if increasing size or price) is covered by the available balance.

If these checks fail, the order is rejected with an `Insufficient margin` error.
