> ## Documentation Index
> Fetch the complete documentation index at: https://docs.perpetradex.xyz/llms.txt
> Use this file to discover all available pages before exploring further.

# PnL & Liquidation

> How unrealized PnL and liquidation price are calculated on Perpetra

## Unrealized PnL

```
Long:  PnL = SizeUsd x MarkPrice / EntryPrice - SizeUsd
Short: PnL = SizeUsd - SizeUsd x MarkPrice / EntryPrice
```

This is algebraically the same as `SizeUsd x (MarkPrice - EntryPrice) / EntryPrice`, just rearranged to avoid precision loss in Solidity's integer math. A positive result is profit, negative is loss.

<Note>
  PnL uses mark price, not the last traded price. See [Mark Price & Index
  Price](/trading/mark-price-and-index-price) for how mark price is derived.
</Note>

## Liquidation price (isolated)

```
Long:  Liquidation Price = EntryPrice x (SizeUsd - Collateral + MaintenanceMargin) / SizeUsd
Short: Liquidation Price = EntryPrice x (SizeUsd + Collateral - MaintenanceMargin) / SizeUsd
```

where `MaintenanceMargin = SizeUsd x MaintenanceMarginBps / 10,000` for that market.

<Note>
  If a long position is so well collateralized that the formula would produce a
  negative result, its liquidation price is effectively 0 (it can't be
  liquidated by price alone). The equivalent case for a short position returns
  an effectively unreachable maximum, for the same reason.
</Note>

### Worked example

A $65,000 long position, entry price $64,800, in a market with a `maintenanceMarginBps` of 50 and \$6,500 collateral:

```
Maintenance Margin = 65,000 x 50 / 10,000 = 325
Liquidation Price   = 64,800 x (65,000 - 6,500 + 325) / 65,000
                    = 64,800 x 58,825 / 65,000
                    ~= 58,613
```

## Liquidation price (cross)

Isolated positions each have their own liquidation price, computed on their own collateral alone. Cross positions don't work quite the same way, because liquidation is decided at the account level, not per position.

For a single cross position, Perpetra can still show you a liquidation price, but it's computed by holding every other cross position's PnL fixed at its current price and solving for the price at which this one position's move would tip your whole cross account into liquidation. If any of your other cross positions move, that number shifts too. See [Margin Modes](/trading/margin-modes) for the full account-level liquidation check.

## What triggers an actual liquidation

Meeting the liquidation price condition makes a position *eligible* for liquidation. It isn't liquidated automatically the instant that happens. A liquidation keeper watches all open positions and calls Perpetra's liquidation contract once a position (or, for cross positions, the account) crosses the threshold. See [Keeper System](/architecture/keeper-system) and [Liquidation Engine](/architecture/liquidation-engine) for that side of it.
