# Margin

The margin engine ensures that users who place trades always have sufficient capital to open and maintain their intended positions.

* **Initial Margin**: The amount required to open a position; calculated as a percentage of notional value based on the asset's risk profile.
* **Maintenance Margin**: Minimum collateral required to keep a position open; if breached, liquidation is triggered

Initial margin requirements on Perpl are dynamic and may vary by market, depending on volatility and position size.

#### Margin Mode

The difference between a cross and an isolated margin is:

* **Isolated Margin**: Each position has its own margin
* **Cross Margin**: All collateral in the account is shared across positions
* **Hybrid Margin**: Manually manage margin between positions

{% hint style="info" %}
To begin with, Perpl will be restricted to isolated margin.
{% endhint %}

<figure><img src="https://809468657-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FXVKhDpsjDZo7VFt2Qfew%2Fuploads%2FT0Wu8pWV98wymIzdfmLk%2Fimage.png?alt=media&#x26;token=3394098b-112d-466d-9014-73f294aa976f" alt=""><figcaption></figcaption></figure>

#### Position Equations

Perpetual contracts can be implemented using the following representation of lot sizes, L:

* {L : L > 0, L ∈ Z}

**Position Notional Value,&#x20;*****N*****:**

<p align="center"><span class="math">N = P · L</span></p>

*P = The mark, entry, or realized price, depending on whether or not the value being calculated is unrealized, position, or realized notional value, respectively.*\
\&#xNAN;*L = The position lot size (the number of contracts of the position).*

**Position Margin Requirement,&#x20;*****MR*****:**

<p align="center"><span class="math">MR = N/MF</span></p>

*N = The position notional value.*\
\&#xNAN;*MF = The margin fraction (analogous to leverage).*

**Position Initial Margin Requirement,&#x20;*****IMR*****:**

<p align="center"><span class="math">IMR = N/IMF</span></p>

*N = The position notional value.*\
\&#xNAN;*IMF = Initial margin fraction (maximum leverage allowed to open a position).*

**Position Maintenance Margin Requirement,&#x20;*****MMR*****:**

<p align="center"><span class="math">MMR = N/MMF</span></p>

*N = The position notional value.*\
\&#xNAN;*MMF = Maintenance margin fraction (minimum collateralization permitted before a position can be*\
\&#xNAN;*liquidated).*

#### Collateral Management

**Increase Collateral**

Traders can add margin to an existing position at any time to reduce the risk of liquidation. This increases the position's `depositCNS` without affecting the entry price or lot size.

**Decrease Collateral (DCP)**

Traders can remove excess margin from a position, subject to the following constraints:

* **120-second expiry**: A DCP request must be executed within 120 seconds of submission. After this window, the request expires and must be resubmitted.
* **Margin tolerance check**: The remaining collateral after the decrease must still satisfy the initial margin requirement for the position.
* **OI threshold**: DCP is blocked when the perpetual's open interest exceeds the `dcpBorrowThreshHdths` threshold (default 85% of capacity), as the market is under stress.

{% hint style="info" %}
The 120-second expiry on DCP prevents stale withdrawal requests from being executed after market conditions have changed significantly.
{% endhint %}

#### Margining Criteria

The price used to determine the notional value in the invariants presented in this section is the most accurate price available. For example, the best price to use for the notional value of a new position is the price at which the position is entered (realized).

In some situations, for example, auto-deleveraging a position, a realization price is not available, and the failed position's bankruptcy price is used. In other situations, the mark or synthetic perpetual price may be used to calculate the position's fair market value (for the value of an existing position).

**Collateralization for establishing or changing a position:**

<p align="center"><span class="math">FMV >= IMR</span></p>

*FMV = Position fair market value.*\
\&#xNAN;*IMR = Position initial margin requirement.*

{% hint style="info" %}
**Reduce-only exemption**: Closing or decreasing a position does not require FMV >= IMR. This allows traders to close underwater positions that would otherwise be trapped.
{% endhint %}

**Collateralization for liquidation:**

<p align="center"><span class="math">0 &#x3C; FMV &#x3C;= MMR</span></p>

*FMV = Position fair market value.*\
\&#xNAN;*MMR = Position Maintenance margin requirement.*

**Collateralization for Auto Deleverage (ADL):**

<p align="center"><span class="math">FMV &#x3C;= 0</span></p>

*FMV = Position fair market value.*
