Protocol
Margin
Margin is the amount of collateral required to open and maintain a position in a leveraged trading environment. In derp.trade, margin is used to determine the maximum leverage that can be applied to a position, as well as the minimum amount of collateral required to keep the position open.
Only isolated margin accounts are supported in derp.trade. This means that all positions have separate margin, which has to be deposited and withdrawn with every order, and are liquidated separately. This is different from cross-margin, where all positions share the same margin.
Leverage
Maximum leverage is dependent on the market. We aim for the maximum leverage for each market to move dynamically between 1x and 100x, chosen by the protocol depending on available liquidity and AMM skew. Currently, maximum leverage is limited while derp.trade is in development and will continue to increase with future updates.
When opening a position, the user can choose any leverage between 1x and the maximum leverage for that market. The lower the leverage, the more margin is required to open the position.
Margin
Initial margin
To open a position, the user's collateral deposited to a market-specific account must be greater than the initial margin. Depositing an amount equal to the initial margin will result in a position being opened at max leverage. Lower leverage levels can be achieved by depositing additional collateral.
The deposit amounts are calculated by the front-end based on the chosen leverage and position size. Deposits execute automatically when opening an order.
The initial margin rate for a given position is calculated by taking the maintenance margin rate and adding:
- a spread-widen buffer, increasing the rate by the max spread increase before next liquidation check,
- a funding-accrual buffer, increasing the rate by the maximum funding debt the position could take on before the next liquidation check,
- a size buffer, increasing the rate with each predefined step in position size.
Maximum leverage calculation
The inverse of the initial margin rate determines the max leverage of a position.
The exact calculations are as follows:
spread_widen_buffer = maximum_quote_deviation
funding_period_ratio = max(1, ceil(liquidation_interval / funding_interval))
funding_buffer = funding_rate * funding_period_ratio
risk_tier = ceil(position_size * P_mark / imr_risk_step_size)
risk_buffer = imr_risk_step_rate * risk_tier
initial_margin_rate = maintenance_margin_rate
+ spread_widen_buffer
+ funding_buffer
+ risk_buffer
initial_margin = position size * P_mark * initial_margin_rate
Maintenance margin
After opening a position, its value summed with the deposited collateral must be greater than the maintenance margin to prevent liquidation. The maintenance margin is calculated as:
L - total size of all user's long positions
S - total size of all user's short positions
D - the difference between AMM liquidity and total user's PnL
If D >= 0:
mrr_debt = 0
If D < 0:
mrr_debt = D * maintenance_margin_hole_sensitivity / ((L + S) * P_mark)
maintenance_margin_rate = base_maintenance_margin_rate + mrr_debt
maintenance_margin = position size * P_mark
Where maintenance_margin_hole_sensitivity
and base_maintenance_margin_rate
are both configured globally in the protocol, currently equal to 0.1 and 10% respectively.