# Liquidations (/docs/risex/trading/liquidations)

RISEx constantly computes a health factor for all accounts on the exchange to ensure accounts are not at risk for liquidation. To do this, the risk engine is calculating the Cross Health Factor defined below:

$$
\text{crossHealthFactor} = \frac{\text{crossMarginBalance}}{\text{totalCrossMM}}
$$

As the Cross Health Factor approaches 1, the risk of liquidation increases. Traders should constantly be monitoring their Health Factor and top-up margin as their Cross Health Factor approaches 1.

***

## Liquidation Waterfall

RISEx follows a four-step liquidation waterfall to protect both the trader and the exchange's solvency. Below is the liquidation flow:

### Stage 1: Pre-liquidation mode

**$\text{crossInitialMarginRequirement} > \text{accountEquity} \geq \text{crossMaintenanceMarginRequirement}$**

In this stage, the user can only perform actions that do not increase the maintenance margin or decrease the account equity. The user should reduce leverage or increase collateral to improve equity. Reduce-only orders are the only orders a user can place to reduce leverage. When an account enters this stage, RISEx automatically cancels any existing non-reduce-only open orders to reduce the account's margin requirement.

### Stage 2: Partial liquidation

**$\text{accountEquity} \leq \text{maintenanceMargin}$**

Once the account equity of a trader's account drops below their cross maintenance margin, the account enters into the partial liquidation mode. During partial liquidation, the following takes place in order:

* Cancel open orders, stopping as soon as the account is healthy again
* Start liquidating the users' positions directly onto the book
  * These liquidations are done via IoC orders at the zero price
* Close only the position size required to restore the account above its maintenance-margin requirement plus a buffer
  * Under normal market conditions, the buffer is 500 bps above the maintenance-margin requirement ($\text{accountEquity} \geq 1.05 \times \text{maintenanceMargin}$)
  * If the account is liquidated repeatedly within a short period, or market volatility is unusually high, the buffer increases progressively up to 5,000 bps, which restores the account to its initial-margin requirement

When the trade gets executed at the zero price, the total account value to maintenance margin ratio stays the same. Therefore, any trade that is executed in this phase only increases the account health.

If the trader has multiple positions, these positions are liquidated one-by-one. After each IoC order, if the trader's account is back above the maintenance margin plus the buffer, the liquidation process stops immediately.

It's also important to note here that each IoC order is sized to the amount needed to restore the account's health, so a position is fully closed only when that is required. It depends on the book's liquidity whether it can absorb that size at the zero price limit. If the order is partially filled, and the trader's account is healthy again, the liquidation process also stops.

RISEx takes up to 1% fee if the liquidations are executed at a price better than the zero price.

### Stage 3: Full liquidation

**$\text{accountEquity} \leq \text{closeOutMaintenanceRequirement}$**

Once the account equity drops below the CMR (2/3 × Maintenance Margin), the account enters full liquidation mode, in which XLP takes over the positions. Takeovers are done at the zero price to ensure that the positions account equity to maintenance margin ratio stays the same or improves:

$$
M_i = \frac{1}{\text{maintenanceMarginFactor}_i}
$$

$$
\text{MMR} = \sum_j |\text{size}_j| \times \text{markPrice}_j \times M_j \quad \text{(positions only, excluding orders)}
$$

$$
\text{adjustment} = \frac{|\text{crossMarginBalance}|}{\text{maintenanceMarginFactor}_i \times \text{MMR}}
$$

$$
\text{crossZeroPrice} = \begin{cases} \text{markPrice} \times (1 - \text{adjustment}) & \text{if long} \\ \text{markPrice} \times (1 + \text{adjustment}) & \text{if short} \end{cases}
$$

**If the account is bankrupt** ($\text{crossMarginBalance} < 0$):

$$
\text{crossZeroPrice} = 2 \times \text{markPrice} - \text{crossZeroPrice}
$$

(flips the adjustment so RLP receives a favorable price)

The XLP determines how to offload positions based on the threading strategies it is running. The XLP insurance fund follows a specific set of risk rules to ensure that the insurance fund does not take on too much risk within a certain time period and to ensure 1 pair can't compromise the total pool:

| Tier          | Max Position | Max Loss         |
| ------------- | ------------ | ---------------- |
| **BTC**       | 4.0x         | 0.30x (i.e. 30%) |
| **ETH**       | 3.0x         | 0.25x            |
| **Major**     | 1.0x         | 0.20x            |
| **Mid-Cap**   | 0.3x         | 0.15x            |
| **Small-Cap** | 0.1x         | 0.05x            |

$$
\text{lossRatio} = \frac{\text{trailing24hrLoss}}{\text{currentEquity}}
$$

$$
\text{maxLossSaturation} = \min\left(\frac{\text{lossRatio}}{\text{maxLoss}},\ 1.0\right)
$$

$$
\text{effectiveMaxPosition} = \text{maxPosition} \times (1.0 - \text{maxLossSaturation})
$$

* $\text{trailing24hrLoss}$: realized losses absorbed by the LV on this market over the last 24 hours, computed as an EMA.
* $\text{currentEquity}$: current LV TVL, total value locked in the vault.
* $\text{maxLoss}$: per-market daily loss cap as a **fraction of equity** (e.g., 0.3 = 30% of LV TVL).
* $\text{maxPosition}$: maximum gross notional the LV may hold on a market at any moment, as a **multiple of equity**.
* $\text{effectiveMaxPosition}$: the live, continuously-updated position ceiling for the market.

The effective max position formula creates a smooth, continuous throttle to bring down the total amount the liquidation vault can take based on the trailing 24h loss.

The table below shows $\text{effectiveMaxPosition}/\text{maxPosition}$ at discrete $\text{lossRatio}$ values for each tier.

| $\text{lossRatio}$ | 0%   | 5%  | 10% | 15% | 20% | 25% | 30% |
| ------------------ | ---- | --- | --- | --- | --- | --- | --- |
| BTC (4x)           | 100% | 83% | 67% | 50% | 33% | 17% | 0%  |
| ETH (3x)           | 100% | 80% | 60% | 40% | 20% | 0%  | 0%  |
| Major (1x)         | 100% | 75% | 50% | 25% | 0%  | 0%  | 0%  |
| Mid-Cap (0.3x)     | 100% | 67% | 33% | 0%  | 0%  | 0%  | 0%  |
| Small-Cap (0.1x)   | 100% | 0%  | 0%  | 0%  | 0%  | 0%  | 0%  |

### Stage 4: ADL

**$\text{xlpFreeCollateral} < \text{notionalLiquidated}$**

If the insurance fund cannot take on the position, the ADL process is initiated for the bankrupt position. ADL finds the most leveraged and profitable trade on the other side to make the bankrupt position whole.

$$
\text{pnlPercent} = \frac{\text{markPrice}}{\text{entryPrice}}
$$

$$
\text{effectiveLeverage} = \frac{\text{notionalPosition}}{\text{accountValue}}
$$

$$
\text{adlScore} = \text{pnlPercent} \times \text{effectiveLeverage}
$$

$$
\text{adlRanking} = \frac{\text{rank}(\text{user.adlScore})}{\text{totalUserCount}}
$$
