> For the complete documentation index, see [llms.txt](https://docs.reya.xyz/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://docs.reya.xyz/developers/risk/margin-system.md).

# Margin System

Reya margins your **whole account as a portfolio**, not position-by-position. Within a market, your long and short exposure nets off, and all positions draw on one shared margin balance — which is what makes cross-margining capital-efficient.

## The two requirements

Both derive from a single portfolio risk number:

* **Initial Margin (IM)** — required to **open or increase** a position.
* **Liquidation Margin Requirement (LMR)** — the base risk number; **fall below it and the account becomes liquidatable** (see [Liquidations & ADL](/developers/risk/liquidations-and-adl.md)).

## How it's computed

Each market carries its **own risk factor**, and cross-market correlations are **not modelled** — a position in one market neither offsets nor amplifies another market's requirement. So your account requirement is simply the **sum** of the per-market requirements. Within a single market, your **net** exposure is what counts — longs net against shorts. All quantities are in **rUSD** (the wrapped-USDC quote / settlement collateral). The per-market parameters that drive this are published on each market's definition (see below), and the [next section](#computing-your-requirement) shows how to put them together.

## Where to get the per-market parameters

Each market's risk parameters are published on its [`perpMarketDefinitions`](/developers/api-reference/rest-api-reference.md) entry (see [Markets & Assets](/developers/reference/markets-and-assets.md)):

* `maxLeverage` — the maximum leverage allowed for the market.
* `liquidationMarginParameter` — drives the market's **liquidation** margin requirement (LMR).
* `initialMarginParameter` — drives the market's **initial** margin requirement (IM).

Read these per market to size positions and set your own buffer above the liquidation requirement.

## What "margin balance" means

The IM and LMR checks compare against your **margin balance**, not your raw deposited collateral. It is the **value of your collateral after haircuts, plus unrealised PnL** on your open positions at the current mark price. Collateral balances already include realised PnL, funding and ADL cashflows. Because unrealised PnL is included, your margin balance **falls as your positions move against you**, even if you never deposit or withdraw.

Margin balance is not the same as **equity**, which is the account's value *before* collateral haircuts. Equity is for display only: margin is always measured against margin balance. For an account holding only rUSD the two are equal.

## Reading margin from the API

`GET /v2/wallet/{address}/accountSummaries` returns these figures for each of the wallet's perp accounts, and the [`accountSummaries` WebSocket channel](/developers/api-reference/websocket-api-reference.md#v2walletaddressaccountsummaries) streams them. Pass `accountId` to select one account. Amounts are in rUSD.

| Field                                                      | Meaning                                                                                                         |
| ---------------------------------------------------------- | --------------------------------------------------------------------------------------------------------------- |
| `collateralValue`                                          | Collateral after haircuts: a holding is discounted, a debt is increased                                         |
| `unrealizedPnl`                                            | Unrealised PnL of the open positions at mark                                                                    |
| `marginBalance`                                            | `collateralValue + unrealizedPnl`: the figure IM and LMR are checked against                                    |
| `equity`                                                   | The account's value before haircuts, for display                                                                |
| `initialMarginRequirement`, `liquidationMarginRequirement` | The account's IM and LMR, summed across markets as [below](#computing-your-requirement)                         |
| `freeCollateral`                                           | `marginBalance − initialMarginRequirement`. Negative means the account is below IM                              |
| `initialMarginRatio`, `liquidationMarginRatio`             | The requirement divided by `marginBalance`, where `1` means 100%. Null when `marginBalance` is zero or negative |

`GET /v2/wallet/{address}/positionRisk` gives the same valuation for each open position, including an estimated liquidation price; see [Liquidations & ADL](/developers/risk/liquidations-and-adl.md#monitoring-your-distance-to-liquidation).

The figures cover **settled positions only**. Fills awaiting settlement are not included, so `freeCollateral` is neither an order-size quote nor a withdrawal limit: the [pre-trade checks](#pre-trade-risk-checks) decide whether a request is accepted. If a price is unavailable, or a mark is older than 60 seconds, both endpoints return HTTP 503 `SERVICE_UNAVAILABLE_ERROR` rather than partial figures. Retry the same request after backing off.

`realBalance` on `AccountBalance` is a collateral balance before haircuts and without unrealised PnL. It is not a margin figure; use `marginBalance`.

## Computing your requirement

Work it out **per market, then sum across markets**. For each market, using the parameters above:

* **Notional** — your net position size, in rUSD: `notional = |net base position| × markPrice` (see [Mark Price](/developers/pricing-and-funding/mark-price.md)).
* **Initial margin** (required to open or increase): `IM = initialMarginParameter × notional`, equivalently `notional / maxLeverage`.
* **Liquidation requirement**: `LMR = liquidationMarginParameter × notional`.

Across your whole account, sum the per-market requirements:

* `IM = Σ (IM per market)`
* `LMR = Σ (LMR per market)`

You become **liquidatable** once your account margin balance falls below `LMR`, and you **can't open or increase** a position if doing so would drop your margin below `IM`.

### Worked example

An ETH-perp market with `liquidationMarginParameter = 0.04`, `initialMarginParameter = 0.05`, and `maxLeverage = 20`. A **10 ETH long** at a mark price of **3,000 rUSD**:

* `notional = 10 × 3,000 = 30,000 rUSD`
* `IM = 0.05 × 30,000 = 1,500 rUSD` (equivalently `30,000 / 20`) — you need at least **1,500 rUSD** of margin to open it.
* `LMR = 0.04 × 30,000 = 1,200 rUSD` — you become liquidatable if your account margin balance falls below **1,200 rUSD**.

## Pre-trade risk checks

**Pre-trade risk is live for spot and perp orders.** Reya checks margin, balances, open-interest limits and reduce-only constraints before matching. Passing these checks does not confirm settlement; use the executions channels for confirmed trades.

* **Incoming creates and crossing modifies:** a failed risk check returns a request error. A rejected modify leaves the existing order unchanged.
* **Pending settlement:** fills awaiting settlement count against available margin, open-interest capacity and spot balances. Later requests can be rejected until those fills are resolved.
* **Reducing risk:** an account between initial and liquidation margin may make a qualifying risk-reducing trade. Normal trading below the liquidation-margin / real-balance floor is rejected; see [Liquidations & ADL](/developers/risk/liquidations-and-adl.md).

### Handling risk outcomes

| Surface                | Outcome                                                              | Client action                                                                                                                                                                                                                        |
| ---------------------- | -------------------------------------------------------------------- | ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------ |
| REST / WS Order Entry  | `ACCOUNT_BELOW_INITIAL_MARGIN_ERROR`                                 | Add collateral, reduce size or submit an eligible risk-reducing order                                                                                                                                                                |
| REST / WS Order Entry  | `ACCOUNT_BELOW_LIQUIDATION_MARGIN_ERROR`                             | Restore the account's margin / real-balance floor; ordinary reducing orders do not bypass it                                                                                                                                         |
| REST / WS Order Entry  | `OPEN_INTEREST_CAP_ERROR` / `OPEN_INTEREST_BUDGET_ERROR`             | Account for the market cap and pending-fill budget; do not blindly resubmit                                                                                                                                                          |
| REST / WS Order Entry  | `REDUCE_ONLY_CONDITION_NOT_MET_ERROR` / `INSUFFICIENT_BALANCE_ERROR` | Correct the reducible size or available spot balance                                                                                                                                                                                 |
| REST / WS Order Entry  | `CROSSING_ORDERS_TEMPORARILY_UNAVAILABLE_ERROR`                      | Retry the crossing request after a short delay. Existing resting orders remain unchanged and can still be cancelled. The same code is returned during a [matching pause](/developers/order-entry/order-lifecycle.md#matching-pauses) |
| WS Info `orderChanges` | `RISK_CANCELLED`                                                     | Remove the cancelled maker from local order state; reassess capacity before replacing it                                                                                                                                             |
| WS Info `orderChanges` | `RISK_REJECTED`                                                      | A protective fire was refused before a child existed; both armed legs are cancelled and protection must be re-armed deliberately                                                                                                     |

For cancellation events, `cancelReasonMessage` is a **fixed explanation per reason**, not the individual failed risk check. Do not parse it to choose a recovery path. Interactive request errors may include more specific diagnostic messages; use the error code for classification.

Continue reconciling [executions](/developers/executions-and-settlement/executions-and-trade-history.md) and [trade busts](/developers/executions-and-settlement/trade-busts.md). Pre-trade checks reduce settlement failures; they do not make a matching-engine acknowledgement final.

## What this means for integrators

* **Cross-margin within an account, isolated between accounts.** Your account is margined as one portfolio — within a market, long and short exposure nets off; separate accounts never share margin (see [Accounts & Subaccounts](/developers/getting-started/accounts-and-subaccounts.md)).
* **Keep a buffer above the liquidation requirement (LMR).** Liquidation triggers on margin, and the [liquidation waterfall](/developers/risk/liquidations-and-adl.md) acts quickly once you cross it — keepers catch accounts shortly after they breach LMR.
