# Margin

Margin is the collateral deposited by a trader to cover their positions and potential losses. On Aftermath, margin is used to back your leverage. If the value of your account falls below the required margin level, your positions may be subject to Liquidations.

## Margin Ratio Formula

The Margin Ratio is the primary metric for account health. It is calculated as the ratio of your **Account Value** (Equity) to your **Position Exposure** (adjusted by `MaxNetBase`).

$$
{\small
\begin{align\*}
MarginRatio(p) &= \frac{
Collateral(p) \cdot CollateralPrice + UnrealizedFundings(p) + Pnl(p)\
}{
MaxNetBase(p) \cdot BasePrice
}
\end{align\*}
}%
$$

#### Understanding MaxNetBase

The exposure of a position is determined by `MaxNetBase`.

$$
\small MaxNetBase(p) = \max { | BaseAmount(p) + PendingBids(p) | , | BaseAmount(p) - PendingAsks(p) | }
$$

{% hint style="info" %}
𝑀𝑎𝑥𝑁𝑒𝑡𝐵𝑎𝑠𝑒(𝑝) is motivated as follows: Instead of taking |𝐵𝑎𝑠𝑒𝐴𝑚𝑜𝑢𝑛𝑡(𝑝) + 𝑃𝑒𝑛𝑑𝑖𝑛𝑔𝐵𝑖𝑑𝑠(𝑝) − 𝑃𝑒𝑛𝑑𝑖𝑛𝑔𝐴𝑠𝑘𝑠(𝑝)| , we take the max above, since otherwise a user could place equivalent bids and asks without raising its leverage, thus artificially deepening the order book.
{% endhint %}

## Initial and Maintenance Margin Ratio (IMR/MMR)

The **IMR** (Initial Margin Ratio) and **MMR** (Maintenance Margin Ratio) are fundamental risk parameters set at market creation. They represent the inverse of leverage:

* **IMR (Initial):** Determines the maximum leverage for **opening** a position.
  * *Example:* An IMR of **0.1** allows a maximum leverage of **10x** ($1 / 0.1$).
* **MMR (Maintenance):** Determines the liquidation threshold.
  * *Example:* An MMR of **0.05** means a position will be liquidated once its effective leverage reaches **20x** ($1 / 0.05$).

#### Governance

The owner of the `AdminCapability` may change these parameters to adapt to market conditions. However, for user safety:

1. The smart contract stores the new values on-chain first.
2. The update is **time-delayed**; it only takes effect after a sufficient waiting period (a few days), ensuring users have time to adjust their positions.

## Position Margin Requirements

The absolute margin required for a specific position is calculated as:

$$
{\small
\begin{align}
MarginRequirements(p) &= MaxNetBase(p) \cdot BasePrice \cdot IMR\
\end{align}
}%
$$

#### Solvency Check

To verify that a position is solvent (i.e., the margin ratio is healthy), the protocol checks that the Margin Ratio is greater than the IMR. This is mathematically equivalent to checking if your **Equity** covers the **Requirement**:

$$
{\small
\begin{align}
MarginRatio(p) &> IMR
\\
&\equiv
\\
\frac{
Coll(p) \cdot CollPrice + UF(p) + Pnl(p)\
}{
MaxNetBase(p) \cdot BasePrice
} &>
\frac{
MaxNetBase(p) \cdot BasePrice \cdot IMR\
}{
MaxNetBase(p) \cdot BasePrice
}
\\
&\equiv
\\
\underbrace{
Coll(p) \cdot CollPrice + UF(p) + Pnl(p)
}*{\text{margin}(p)}
&>
\underbrace{
MaxNetBase(p) \cdot BasePrice \cdot IMR
}*{\text{margin\_requirements}(p)}
\end{align}
}%
$$

{% hint style="info" %}
To get the ***Maintenance Margin Requirements*** of a position, simply replace 𝐼𝑀𝑅 with 𝑀𝑀𝑅 .
{% endhint %}
