💰Margin Trading
This page describes the margin trading use cases of the TurboSwap protocol, such as deposit, withdraw, trade, and liquidation
Deposit and Withdraw
When a user deposits an amountxiof thei-thasset, this amount is added to the corresponding reserves. Also, a fixed shareϕdof the deposit amount is added to the capital as a fee, and the remaining amount is added to the user's position:
When a user withdraws an amountxof thei-thasset, this amount is subtracted by the user's position. Also, a fixed shareϕwof the withdrawn amount is added to the capital as a fee, and the remaining amount is subtracted from the corresponding reserves and is paid to the user:
Note, that a withdrawal operation may turn a positive (long) position into a negative (short) one. This way withdrawal could be used to borrow an asset from the protocol. However, the protocol will allow such withdrawal only in case the loan is sufficiently collateralized.
Trade
When a user trades an amountxi1ofi1-thasset for an amountxi2ofi2-thasset, this is basically equivalent to an atomic combination of deposit and withdraw, though fees could be different:
Hereϕsis the sell fee share, andϕbis the buy fee share.
Margin Call and Default
For every asset, a margin quotient is set. The margin quotient for thei-thasset is denoted asmi. For a particular (j-th) account, the marginal value (μj) is the sum of the marginal values of all positions denominated in the base currency. For a positive (long) position, the marginal value is the position value divided by1+mi. For a negative (short) position, the marginal value is the position value multiplied by1+mi. Thus, the marginal value of an account could be calculated as:
Whenμj<0, thej-thaccount is in margin call. No operation is allowed to bring an account into margin call. If an account is already in margin call, no withdrawals are allowed and only those trades are allowed that:
don't turn any positive (long) position into negative (short), and
reduce the absolute value of some negative (short) position
For a particular (j-th) account, the net value (νj) is the sum of the values of all positions denominated in the base currency:
Whenνj<0, the j-thaccount is in default. Note, that default state of an account implies margin call state.
Liquidation
When an account is in margin call, the protocol allows forcefully to liquidate negative (short) positions of this account. A liquidation could be triggered by anybody and is rewarded by the protocol. A liquidation could be performed in three ways: on exchange, via peer-to-peer trade, or via cross liquidation trade.
Liquidation On Exchange
In order to perform a liquidation on exchange, a liquidator performs a trade on behalf on an account whose negative (short) position is being liquidated. The trade should exchange some asset whose position in the account is positive (long) for an asset whose position is negative (short), but should not turn the positive position to become negative or negative positions to become positive. The liquidator receives a part of the trade fees as a reward. The rewards are added to the liquidator's account.
More formally, letj1be the ID of the liquidator's account, andj2be the ID of the account which is in margin call. Leti1be the ID of an asset whose position in thej2-thaccount is positive (long), sovi1,j2>0, andi2be the ID of an asset whose position in thej2-thaccount is negative (short), so vi2,j2<0. The liquidator trades the amountxi1of thei1-thasset for the amountxi2of thei2-thasset on behalf of thej2-thaccount:
A part of the trade fees is added to the liquidator's positions in corresponding assets:
Hereλis the share of trade fees being paid to a liquidator as a reward.
The protocol ensures, that the positions of the account being liquidated didn't flip and that the marginal value of the account didn't become positive:
Peer-to-Peer Liquidation
Instead of performing a liquidation on exchange, a liquidator may perform a trade between the account whose position is being liquidated and the liquidator's own account:
The trade is performed at the current market prices, so:
thus
As usual, the protocol ensured, that the positions of the account being liquidated didn't flip, the marginal value didn't become positive, and that the liquidator account didn't fall into a margin call.
Cross Liquidation
When two accounts are in margin call, and these accounts have opposite positions in certain assets; In such a case a liquidator can perform a cross trade between these accounts in order to liquidate negative (short) positions on both of them.
More formally, letj1be the ID of the liquidator's account,j2be the ID of the first account which is in margin call, andj3be the ID of the second margin call account. The margin call accounts have opposite positions in thei1-thandi2-thassets:
The validator executes a cross trade:
and receives his share in the fees:
The magic quotientϕs+ϕb−ϕsϕbis basically the effective fee that was charged:
The trade is performed at the current market price:
As usual, the protocol ensures, that positions of the margin call accounts didn't flip during the trade and the marginal values didn't became positive:
Liquidation Against The Capital
In case the capital is not properly balanced, it could be possible to liquidate a margin call position against the capital.
More formally, letj1be the ID of the liquidator's account, andj2be the ID of the account which is in margin call. Leti1be the ID of an asset whose position in thej2-thaccount is positive (long), sovi1,j2>0, andi2be the ID of an asset whose position in thej2-thaccount is negative (short), so vi2,j2<0. The liquidator initiates a trade between the margin call account and the capital:
A part of the trade fees is added to the liquidator's positions in corresponding assets:
The trade is performed at the current market prices, so:
thus
As usual, the protocol ensured, that the positions of the account being liquidated didn't flip and the marginal value didn't become positive.
Write Off
The TurboSwap protocol uses marginal quotients to liquidate positions at risk before they go underwater, however in extreme market conditions a margin could not be wide enough, and an account may go default before all its short positions are liquidated.
So when an account is in default, it is very unlikely that short positions will ever be paid in full, as the collateral value is less than the debt, so the debtor doesn't have economical incentive to relay. In order to fix this, the protocol writes off some of the debt, to make the rest of the debt worth repaying. Writes off happens during liquidation. Below is a formal description of how write off works.
Lets assume that thej-this in margin call, its position in thei1-thasset is positive (long), and position in thei2-thasset is negative (short):
Letvi1,j′andvi2,j′be the corresponding positions after the liquidation:
Letνj+andνj−be the cumulative values of all the positive (long) and the negative (short) positions in thej-thaccount:
The idea is that the relative change in the total value of the short positions during the liquidation shouldn't be lower than the relative change in the total value of the long positions:
If this constraint is violated, then the protocol writes of a portionxi2of the short position in order to make the constraint hold:
After the write off, the protocol ensures that the short position didn't flip:
Last updated