# Margin (/docs/risex/trading/margin)

RISEx is a natively **cross-margined** venue that unifies margin on your spot and perpetual balances. The exchange is set up so that in the future, you will be able to leverage a host of differentiated assets to gain maximum capital efficiency. Currently, USDC is the primary collateral type, and a user's P\&L also contributes to their margin. RISEx supports two main margin types:

## Margin Types

| Type                                                | Description                                                                                                                                                                                                                                                                                                                                                                                       |
| --------------------------------------------------- | ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
| **Cross margin**                                    | Your total account contributes to margin. Position P\&L + all USDC in your account contribute to equity.                                                                                                                                                                                                                                                                                          |
| **Isolated margin**                                 | You can enable Isolated Margin mode to protect your total account against a singular position that you want to take. In isolated margin mode, only the margin you deposit and the unrealized PnL of the position contribute to the health of the position. Resting orders priced worse than the mark price also reserve margin inside the position ([see Open-Loss Reserve](#open-loss-reserve)). |
| **Permissionless portfolio margin** *(Coming Soon)* | Given RISEx is fully composable with all of RISE we are working on enabling portfolio margining where users can interact with the underlying Borrow-Lend markets on RISE to get leverage on select spot assets. This will also enable native yield on your holdings, so you can earn on idle spot positions.                                                                                      |

***

## Account Balances and Account Health

The RISEx risk engine recognizes the following when looking at an account.

### Account Equity

Total equity including all unrealized PnL (across both cross and isolated positions). This represents the current value of your account and is used to determine when you should be liquidated.

$$
\text{accountEquity} = \text{totalBalance} + \sum(\text{unsettledUSDC per position})
$$

***

### Cross Initial Margin (IM)

Initial margin is the margin required to open a position. It looks at your **free collateral** to determine if you have enough USDC to place into the position. The Initial Margin Rate (IMR) is simply the inverse of the leverage chosen for the position:

$$
\text{IMR} = \frac{1}{\text{leverage}}, \qquad \text{initialMargin} = \text{IMR} \times \text{notional}, \qquad \text{notional} = \text{size} \times \text{markPrice}
$$

Cross initial margin is the sum of all initial margins across your positions, plus the [open-loss reserve](#open-loss-reserve) of your resting orders. Cross-initial margin tells you how much capital you have locked up as margin on current positions and unfilled orders.

$$
\text{totalCrossIM} = \sum(\text{initialMargin per cross position}) + \sum(\text{initialMargin per unfilled cross order}) + \sum(\text{openLossReserve per market})
$$

***

### Cross Maintenance Margin (MM)

Maintenance margin is the required margin needed in your account to keep the position healthy. Each market has its own fixed Maintenance Margin Rate (MMR), set at (2/3) of the market's base Initial Margin Rate — $\text{IMR}_{\text{base}}$, the IMR at the market's max allowed leverage:

$$
\text{MMR} = \frac{2}{3} \times \text{IMR}_{\text{base}}, \qquad \text{IMR}_{\text{base}} = \frac{1}{\text{maxLeverage}}
$$

$$
\text{maintenanceMargin} = \text{MMR} \times \text{notional}
$$

Note $\text{MMR}$ is fixed per-market and does **not** depend on the leverage a trader actually chose. If a trader opens a position below the market's max leverage, their posted initial margin is larger than $\text{IMR}_{\text{base}} \times \text{notional}$, so maintenance margin ends up being *less* than (2/3) of their actual initial margin, not exactly 2/3 of it.

Cross maintenance margin is the sum of all maintenance margins across your position. You have to keep your account equity above the cross maintenance margin to avoid partial liquidations ([see more here](/docs/risex/trading/liquidations)).

$$
\text{totalCrossMM} = \sum(\text{maintenanceMargin per cross position}) + \sum(\text{maintenanceMargin per unfilled cross order})
$$

***

### Close-out Margin (CMR)

Close-out Margin Rate (CMR) is the required margin needed in your account to avoid full liquidations. The close-out margin is an important metric because it is the level at which your account transitions from partial to full liquidation, at which point you lose all the margin.

$$
\text{CMR} = \frac{2}{3} \times \text{MMR}
$$

$$
\text{totalCloseOutMargin} = \text{totalCrossMM} \times \frac{2}{3}
$$

***

### Netted Margin

The margin requirement in each market is based on that market's **netted exposure**, the worst case across both directions, rather than the simple sum of the open position and open orders:

$$
\begin{aligned}
\text{nettedExposure} &= \max\big(|\text{position} + \text{openBuys}|,\ |\text{position} - \text{openSells}|\big) \\
\text{initialMargin} &= \text{nettedExposure} \times \text{markPrice} \times \text{IMR}_{\text{eff}} \\
\text{maintenanceMargin} &= \text{nettedExposure} \times \text{markPrice} \times \text{MMR}
\end{aligned}
$$

What this means in practice:

* Opposite-side orders larger than the open position only add margin on the excess.
* Same-side orders add to the margin requirement in full.

Let's take a look at the following example of BTC at \$100,000 mark price, 25x max leverage (4% IMR):

| Position    | Open Orders              | Netted Exposure | Initial Margin |
| ----------- | ------------------------ | --------------- | -------------- |
| Long 10 BTC | None                     | 10 BTC          | \$40,000       |
| Long 10 BTC | Sell 10 BTC              | 10 BTC          | \$40,000       |
| Long 10 BTC | Buy 5 BTC                | 15 BTC          | \$60,000       |
| Long 10 BTC | Sell 25 BTC              | 15 BTC          | \$60,000       |
| No position | Buy 10 BTC + Sell 10 BTC | 10 BTC          | \$40,000       |

With no position, resting buys and sells do not cancel out, and the larger side sets the requirement. When placing a new order, only the *increase* in netted exposure is checked against the free collateral, so a closing order no larger than the open position can be placed even with no free collateral.

***

### Open-Loss Reserve

A resting limit order fills at its own price, whatever the mark price is at that moment. A buy above the mark price, or a sell below it, loses money the instant it fills. To make sure a fill cannot push your account straight toward liquidation, RISEx reserves this **open loss** as part of your initial margin while the order rests.

For each market, the gap is how far a resting order sits on the wrong side of the mark price. Buys (bid) and sells (ask) are tracked separately, and each side uses the largest gap among your resting orders in that market:

$$
\begin{aligned}
\text{gap}_{\text{bid}} &= \max(\text{buyPrice} - \text{markPrice},\ 0) \\
\text{gap}_{\text{ask}} &= \max(\text{markPrice} - \text{sellPrice},\ 0) \\
\text{openLossReserve} &= \text{gap}_{\text{bid}} \times \text{buySize} + \text{gap}_{\text{ask}} \times \text{sellSize}
\end{aligned}
$$

What this means in practice:

* Buys at or below the mark price and sells at or above it have no gap, so they reserve nothing.
* The largest gap on a side applies to **all** your resting size on that side, so one aggressive order raises the reserve on your other orders on the same side.
* Cancelling the aggressive order does not lower the gap while other orders on that side still rest. The gap resets once that side has no resting orders.
* The reserve is part of initial margin only. It reduces your free margin and withdrawable amount, but it does not change maintenance margin, so it never triggers a liquidation by itself.
* On an isolated position, the reserve is held inside the position while the order rests. After a fill it stays in the position's margin, unless the fill closes the position.

Let's take a look at the following example of BTC at \$100,000 mark price. The reserve does not depend on leverage:

| Resting Buy Orders                             | Bid Gap | Buy Size | Reserve |
| ---------------------------------------------- | ------- | -------- | ------- |
| Buy 2 BTC at \$99,000                          | \$0     | 2 BTC    | \$0     |
| Buy 1 BTC at \$100,500                         | \$500   | 1 BTC    | \$500   |
| Buy 1 BTC at \$100,500 + Buy 2 BTC at \$99,000 | \$500   | 3 BTC    | \$1,500 |

In the last row, the passive 2 BTC bid also reserves \$500 per BTC, because the gap is set by the most aggressive order on that side.

***

### Free Cross Margin

The total amount of free margin that can be used on positions. Once your free cross margin hits 0, you can no longer take positions, given you have no more collateral left to use.

$$
\text{freeCrossMargin} = \max(\text{crossMarginBalance} - \text{totalCrossIM},\ 0)
$$

***

### Withdrawable Amount

The withdrawable amount is the total amount a user can withdraw from the exchange. The withdrawable amount is the minimum of your cross balance and your free cross margin, ensuring you always have enough margin to cover your positions.

> **Note:** RISEx does not allow for withdrawal of uPnL.

$$
\text{crossBalance} = \text{totalBalance} - \text{isolatedMarginUsage}
$$

$$
\text{withdrawable} = \max(\min(\text{crossBalance},\ \text{freeCrossMargin}),\ 0)
$$

***

## Self-Trade Prevention

RISEx also implements self-trade prevention to block the matching of two opposite orders from the same trading account. If two orders from the same account are attempted to be matched, then the maker order will be canceled, and the taker order will hit the next Bid/Ask on the book.
