# 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.                                                   |
| **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. 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})
$$

***

### 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.

***

### 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.
