# Calculations of DEX perpetual contract

### 1. Margin Calculation

**1. What is a margin?**

In order for users to buy and/or sell contracts within the crypto market, a small amount of funds at a certain rate (based on the contract price) is needed as a guarantee for a contract to be fulfilled. This funding is known as the margin for crypto contracts.

**2. How to calculate margin?**

Under cross margin, all available funds in the user's account are treated as available margin.

Margin Calculation for USDC-margined Contracts

Initial Margin = Size * |Quantity| * Contract Multiplier * Mark Price / Leverage, the initial margin varies based on the price of the coin traded.

**3. Margin vs. Leverage**

Leverage is a common financial trading system known as the margin system. "Leverage" allows investors to magnify their trades, which exposing them to greater profits and risks.

Under cross margin, when users open a certain quantity of long or short positions, Initial Margin = Position Value / Selected Leverage

For example:

Imagine the current BTC price is at 10,000 USDC/BTC and a user wishes to open a perpetual contract equivalent to 1 BTC with 10x leverage. The quantity of long position = No. of BTC Opened / Size = 1 / 0.0001 = 10,000.

Initial Margin = Size x Quantity x BTC Price / Leverage = 0.0001 x 10,000 x 10,000 / 10 = 1,000 USDC

**4. Margin Ratio**

Initial Margin Ratio: 1 / Leverage

Maintenance Margin Ratio: The minimum margin rate required for users to maintain their current positions

Margin Ratio = (Cross Margin Balance + Cross Margin UPL - All Maker Fees) / (Maintenance Margin + Pre-liquidation Fees)

### 2. P&L Calculations

OKX DEX Perpetual supports the buy/sell mode for cross margin (USDC-margined), and the contract P&L is calculated as follows:

Click on the image to view the full spreadsheet

Term | Explanation |
---|---|

Open Interest | Long positions are denoted as positive numbers, short positions as negative numbers |

PnL | Profit from Long Position = Contract Size * |Number of Contracts| * Multiplier * (Mark Price - Average Entry Price) Profit from Short Position = Contract Size * |Number of Contracts| * Multiplier * (Average Entry Price - Mark Price) |

PnL% | Profit / Initial Margin |

### 3. Assets

Term | Explanation |
---|---|

Account Equity | Account Equity represents the total value of your account, measured in USDC. It includes both your USDC balance and the USDC value of any open positions, taking into account unrealized profits or losses. Account Equity = USDC Balance + Unrealized Profits/Losses |

Available Margin | Available Margin is the collateral that can be used to open positions. Available Margin = Asset Balance - Order IMR Occupied - Position IMR Occupied - Total Order Fees |

Available Balance | Available Balance refers to the amount you can withdraw from your account. Available Balance = Account Balance - max[(Position IMR Occupied + Order IMR Occupied) - UPL, 0] - Total Order Fees |