Leveraged trading allows you to trade with more funds than you own. In essence, you are borrowing funds from the exchange. When your position's value reaches a critical point, it will be proactively liquidated to insure against a loss to the exchange.
Perpetual Protocol uses index price to evaluate liquidation conditions.
Maintenance margin: 6.25%
Liquidation is triggered when your position margin ratio is below 6.25%. Margin ratio is calculated using 15-minute TWAP of the index price.
Positions with a value of $100 or lower will be subject to full liquidation, unlike larger positions where partial liquidation is used.
Each position has an associated liquidation price. Note that this price depends on how much collateral you deposited, and the PnL of each position. Depositing, withdrawing, and funding payments all affect your collateral, and therefore affect liquidation price.
The PnL of each position will affect the liquidation price of other positions. If the profit on one position increases, the liquidation price of all positions will decrease (longs) or increase (shorts).
Some or all of your margin will be taken as a liquidation penalty. Perpetual Protocol uses cross-collateral, so if you have multiple positions, liquidators can only attempt to liquidate one at a time, and will likely choose the biggest position so they can liquidate the maximum allowable amount.
The amount that is liquidated depends on a few factors.
- Amount of margin available in the liquidator's account. Since they are taking on the liquidated position, they require margin to back it.
- Competion with other liquidators. Liquidators may opt to only liquidate part of the position to increase their chances of success compared to trying to liquidate the entire position. If parts of the position are taken by other liquidators first and not enough remains, their transaction will revert. For example, if 1 ETH is available to liquidate, and the liquidator attempts to liquidate 100% of it, if another transaction liquidating 0.25 ETH is sent faster and succeeds, the 1 ETH transaction will fail. Therefore liquidators are encouraged to liquidate a smaller amount to increase the chance of success.
Calculating liquidation price
For accessing the values of various parameters, see Smart contract addresses
#long liqPrice = indexPrice - ((accountValue - totalPositionValue * mmRatio) / ((1 - mmRatio) * positionSizeOfTokenX)) #short liqPrice = indexPrice - ((accountValue - totalPositionValue * mmRatio) / ((1 + mmRatio) * positionSizeOfTokenX))
There is also a helper contract function that returns
Let's look at an example to illustrate how to calculate liquidation prices.
If the index price of ETH is $2,000 and you have $100 in collateral, then accountValue will be $100.
Let's say you open a position of 0.10 ETH, so that the totalPositionValue is equal to $200. The maintenance margin ratio is represented by mmRatio, which is equal to 6.25%. Finally, the positionSizeOfTokenX is the position size in terms of the base token, so for this example it will be +0.10.
Liquidation price = 2000 - ((100 - 200*0.0625) / ((1 - 0.0625)*0.10))
= 2000 - (100-12.5) / (0.9375*0.10)
= 2000 - (87.5)/(0.09375)
= 2000 - 933.3333
The 0.10 ETH long position with entry price of $2,000 and $100 in collateral will be liquidated once the index price reaches $1066.6666.
Now let's look at calculating the liquidation price for a short position with the same inputs as above. In this case positionSizeOfTokenX will be negative at -0.10.
Liquidation price = 2000 - ((100 - 200*0.0625) / ((1 + 0.0625)*-0.10))
= 2000 - (100 - 12.5) / (1.0625*-0.10)
= 2000 - (87.5) / (-0.10625)
= 2000 - (-823.5294)
The 0.10 ETH short position with entry price of $2,000 and $100 in collateral will be liquidated once the index price reaches $2823.5294.
Liquidations are performed by keeper bots which anyone can code and run.
These bots call the clearinghouse contract to trigger liquidations, and the clearinghouse determines whether a position meets the conditions for liquidations or not. Liquidators receive a bonus when successfully triggering a liquidation, paid from a portion of the collateral backing the liquidated position.