> For the complete documentation index, see [llms.txt](https://docs.megatao.com/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://docs.megatao.com/trading/liquidations.md).

# Liquidations

A liquidation occurs when a position's margin ratio falls below the maintenance margin. The position is automatically closed and a liquidation fee is charged.

## When does liquidation happen?

Liquidation is triggered when:

```
Margin Ratio = (Margin + Unrealized P&L) / Notional Value < Maintenance Margin
```

The maintenance margin is configured per-market (currently 10% on all markets, configurable from 1% to 50%).

The system monitors all positions continuously. When a position becomes undercollateralized, a keeper bot executes the liquidation.

## Partial vs full liquidation

The protocol supports two liquidation modes:

| Margin Ratio                                                                  | Action                      |
| ----------------------------------------------------------------------------- | --------------------------- |
| Below maintenance margin (e.g., < 10%)                                        | Full liquidation            |
| Between maintenance margin and maintenance margin + 5% buffer (e.g., 10%-15%) | Partial liquidation allowed |
| Above maintenance margin + 5% buffer (e.g., > 15%)                            | Not liquidatable            |

Partial liquidation closes enough of the position to bring the margin ratio back above the maintenance threshold, preserving the rest.

## Liquidation price

### Long position

```
Liquidation Price = Entry Price x (1 + Maintenance Margin - 1 / Leverage)
```

Example with 3x leverage and 10% maintenance margin:

```
Liquidation Price = 0.019 x (1 + 0.10 - 1/3) = 0.019 x 0.767 = 0.01457
```

A \~23% price drop triggers liquidation.

### Short position

```
Liquidation Price = Entry Price x (1 - Maintenance Margin + 1 / Leverage)
```

Example with 3x leverage and 10% maintenance margin:

```
Liquidation Price = 0.019 x (1 - 0.10 + 1/3) = 0.019 x 1.233 = 0.02343
```

A \~23% price increase triggers liquidation.

## Liquidation process

1. Keeper detects margin ratio below maintenance margin
2. Position is closed at current market price
3. Liquidation fee is deducted from remaining margin
4. Keeper receives the liquidation fee as reward
5. Remaining margin (if any) is returned to the trader

If the position's losses exceed its margin (bad debt), the insurance fund covers the shortfall.

## Liquidation fee

```
Liquidation Fee = Remaining Margin x Liquidation Fee Rate
```

The liquidation fee rate is per-market configurable (up to 10%). Currently set to 5% on all markets.

This fee incentivizes keepers to execute liquidations promptly.

## Avoiding liquidation

* Close the position manually before the margin ratio reaches the maintenance level. This avoids the liquidation fee entirely.
* Use lower leverage. At 1x leverage with 10% maintenance margin, the price must move \~90% against you. At 3x, \~23%. At 5x, only \~10%.

The liquidation price is shown when you open a position and on your open positions panel.
