๐ฃ๏ธMargin ratio & Liquidation
Last updated
Last updated
The margin ratio is used to assess the risk of a protocol user's position.
The margin ratio shows whether the user's positions are safe, whether the user can place new orders, and whether it is possible to partially liquidate or completely liquidate the user's position. At the first stage of implementation, all user collateral will be used to maintain positions in different markets, the so-called cross-margin. To simulate an isolated margin (when the user chooses which part of the margin will be used for a particular position), you can create an additional account.
Spin uses a rather conservative approach to calculating the margin ratio, which reduces the chance of liquidating users' positions and does not allow users to place orders, after which the position will have to be liquidated immediately or almost immediately.
The margin ratio depends on open positions and their unrealized PnL, on open orders that increase the position, and on funding.
The numerator is the user's equity: the user's real balance if he closes all positions at the moment. The denominator is the collateral required to support open positions and open orders.
If โCo = 0, that is, the user has no open orders, then the margin ratio is calculated simply by the formula, without iterations over open orders.
The following rules are used depending on the value of the margin ratio:
if, after the user has made a trade, his Mr, then there is no risk of opening new positions,
if 0.7 <= Mr < 1, then the user can't open new positions, but he is not subject to liquidation either,
if 0.3 <= Mr < 0.7, a partial liquidation of user positions is possible,
a trader cannot withdraw funds from his account if Mr< 1 or if, as a result of his withdrawal, falls below 1,
if Mr < 0.3, the complete liquidation of the user's positions is possible.
Or the same rules briefly:
If Mr < 0.7, the user's position can be liquidated.
The Mr of the liquidator after liquidation must be > 1.
The liquidator is not obliged to โbuy backโ the entire position of the liquidated person available for closing, the minimum liquidation is a value equal step_size parameter.
The user's position can be liquidated until it becomes equal to 0.7, taking into account the penalty charged for liquidation.
Liquidation calculator, which calculates how much of a selected asset can be liquidated:
If the position is long, then the equation is multiplied by +1, if short, then -1. The result is rounded up to step_size parameter.
The liquidation mechanism on Spin is implemented using a separate liquidation bot, the source code of which will be open in the future, and anyone can run it and receive rewards of 1.5% for helping to liquidate users on the Spin site.
The liquidation bot, when launched, initializes its state, storing in memory all accounts that have positions. Once in a given time interval, the bot receives from the smart contract information on all positions that have been changed since the last iteration and merges them with the accounts received during initialization or with the accounts received at the previous iteration. After that, the bot runs through all these accounts and the off-chain calculates who needs to be liquidated and who does not. All accounts to be liquidated will be liquidated using the liquidate() on-chain method, inside the call of which it will once again check whether the user is really under liquidation.