# Liquidation

Mosaiq liquidates at the account level. An account is liquidatable when it
has positive maintenance margin and

```text
accountValue < maintenanceMarginUsed
```

`accountValue` is settled cash plus unrealized PnL. `maintenanceMarginUsed`
sums every position leg's notional times its maintenance rate, marked at the
oracle or index price of the venue that holds the leg. Open orders reserve
initial margin but are not part of maintenance margin.

## Process

Once an account is liquidatable, Mosaiq closes every position of the account
to zero. For each liquidatable account, Mosaiq:

1. Cancels the account's open orders. A market (one asset on one venue) where
   an order of the account was open waits until the venue reports what that
   order executed and Mosaiq books those fills. The other markets go on at
   once.
2. Sends a reduce-only market order on each position, on the venue that holds
   it, for at most 100,000 USD of notional at the oracle price.
3. Bounds each order at the venue price moved by the maintenance margin rate of
   its market, for example 1% on a market with a 1% maintenance rate.
4. Sends the next order on a position when the previous one ends, or 5 seconds
   after the previous one was sent, until the position is flat.
5. Takes over a position at the oracle price, with the Mosaiq House as the
   other side, when what remains is below the venue's minimum order size, for
   example 10 USD on Hyperliquid. No venue sees a takeover.

A position larger than 100,000 USD closes in several orders. The liquidation
goes on until every position that it started is flat, also when the account
value comes back above maintenance margin, for example after a deposit. Once
the account value is back above maintenance margin, a position that the
liquidation has not started stays open.

While a liquidation is under way, the account can cancel orders, and it can
neither place nor modify one. Mosaiq rejects every placement, reduce-only ones
included, and every modify with `trading_restricted` and the detail
`liquidation_in_progress`, on every market. The reason is that Mosaiq trades
for all its accounts from shared venue accounts. A venue cannot limit a close
of the account to the account's position, so the close and the liquidation's
order could both fill. Once the liquidation ends, the account trades as
usual.

## See a liquidation in the API

The [`userEvents`](/api/websocket#userevents) topic sends `liquidationStarted`
when a liquidation of the account starts and `liquidationFinished` when it
ends. While it is under way,
[`userData.liquidation`](/api/clearinghouse-state#positions-and-margin) shows
the liquidation and the positions that it is closing.

Each fill of a liquidation order carries a `liquidation` block on the
[`userFills`](/api/websocket#userfills) topic and in
[`GET /v1/userFills`](/api/user-fills). The block names the liquidated account
and the oracle price at which Mosaiq sized the order. A House takeover shows as
a fill whose `liquidation.method` is `backstop`. The fill of the other side
of the trade carries no block, also when that side is another Mosaiq account.

Each order that the liquidation cancelled ends `cancelled` with `cancelCause`
`liquidation` on the [`orderUpdates`](/api/websocket#orderupdates) topic.

## Costs

A fill of a liquidation order and a House takeover are both taker executions.
Each pays the venue fee, the Mosaiq taker fee, and a liquidation penalty:

```text
penalty = 0.5 × maintenance margin rate × notional of the fill
```

Mosaiq fixes the rate when it sizes the reduce, and books the penalty when the
fill settles. `liquidation.penaltyUsdc` on the fill shows it.

## Failure handling

Repeated liquidation failures on one market can cause Mosaiq to halt trading on
that market (one asset on one venue, for example BTC on Lighter) and cancel the
live user orders of that market. Each order that the halt cancelled ends
`cancelled` with `cancelCause` `market_halt`. During the halt, on that market, modifies are
rejected and only immediate reduce-only placements are admitted, except for an
account in liquidation, which can place none. `withdraw` and
`transfer` fail with `trading_restricted` and the detail `operations_halted` for an account with a position on a
halted market. Other markets stay open. You can still cancel orders.

There is no insurance fund. If an account's value is still negative after its
positions are flat, the deficit stays on that account. Mosaiq does not charge
it to other users.

Venue collateral is managed separately by [Rebalancing](/concepts/rebalancing).
