Trading
Margin

Margining

As part of default settings on the dYdX Chain open source software, each market has two risk parameters, Initial Margin Fraction (IMF) and Maintenance Margin Fraction (MMF):

  • Initial Margin Fraction: A percentage (fixed until certain level of Open Interest) that determines the minimum collateral required to open or increase positions.

  • Maintenance Margin Fraction: A percentage (fixed) that determines the minimum collateral required to maintain positions and avoid liquidation.

Open-Interest-Based IMF

The IMF of a perpetual market scales linearly according to the current open_notional in the market, starting at open_notional_lower_cap to open_notional_upper_cap (USDC denominated):

open_notional = open_interest * oracle_price

scaling_factor = (open_notional - open_notional_lower_cap) / (open_notional_upper_cap - open_notional_lower_cap)

IMF_increase = scaling_factor * (1 - base_IMF)

effective_IMF = Min(base_IMF + Max(IMF_increase, 0), 100%)

I.e. the effective IMF is the base IMF while open_notinal < lower_cap, and increases linearly until open_notional = upper_cap, at which point the IMF stays at 100% (requiring 1:1 collateral for trading). Importantly, the MMF (Maintenance Margin Fraction) does not change.

The Open Notional Lower Cap (opens in a new tab) and Open Notional Upper Cap (opens in a new tab) are parameters defined as part of the market's Liquidity Tier (opens in a new tab).

Detailed Margin Calculation

The margin requirement for a single position is calculated as follows:

Initial Margin Requirement = abs(S × P × I) Maintenance Margin Requirement = abs(S × P × M)

Where:

  • S is the size of the position (positive if long, negative if short)
  • P is the oracle price for the market
  • I is the initial margin fraction for the market
  • M is the maintenance margin fraction for the market

The margin requirement for the account as a whole is the sum of the margin requirement over each market i in which the account holds a position:

Total Initial Margin Requirement = Σ abs(Si × Pi × Ii) Total Maintenance Margin Requirement = Σ abs(Si × Pi × Mi)

The total margin requirement is compared against the total value of the account, which incorporates the quote asset (USDC) balance of the account as well as the value of the positions held by the account:

Total Account Value = Q + Σ (Si × Pi)

The Total Account Value is also referred to as equity.

Where:

  • Q is the account's USDC balance (note that Q may be negative). In the API, this is called quoteBalance. Every time a transfer, deposit or withdrawal occurs for an account, the balance changes. Also, when a position is modified for an account, the quoteBalance changes. Also funding payments and liquidations will change an account's quoteBalance.
  • S and P are as defined above (note that S may be negative)

An account cannot open new positions or increase the size of existing positions if it would lead the total account value of the account to drop below the total initial margin requirement. If the total account value ever falls below the total maintenance margin requirement, the account may be liquidated.

Free collateral is calculated as:

Free collateral = Total Account Value - Total Initial Margin Requirement

Equity and free collateral can be tracked over time using the latest oracle price.