> For the complete documentation index, see [llms.txt](https://docs.coinlocally.com/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://docs.coinlocally.com/perpetual-contracts/coin-margined-perpetual-contracts/margin-and-profit-loss-calculations.md).

# Margin and Profit/Loss Calculations

### **1. Open Position Margin**

The **opening margin** consists of:

* **Initial Margin**
* **Opening Loss**

Including **opening loss** in the margin calculation helps prevent **immediate liquidation** due to unfavorable price movements upon order placement.

#### **1.1 Initial Margin**

$$
\text{Initial Margin} = \frac{\text{Quantity} \times \text{Contract Size}}{\text{Order Price} \times \text{Leverage}}
$$

#### **1.2 Opening Loss**

$$
\text{Opening Loss} = \text{Quantity} \times \text{Contract Size} \times \left| \min\left(0,\ \text{Order Direction} \times \left( \frac{1}{\text{Order Price}} - \frac{1}{\text{Mark Price}} \right) \right) \right|
$$

Where:

* **Order Direction** = `1` for **long**, `-1` for **short**

#### **Example**

* **Contract Type**: Coin-margined (e.g., BTC/USD)
* **Order Price**: 60,000 USD
* **Mark Price**: 55,000 USD
* **Quantity**: 12,000 contracts
* **Contract Size**: 10 USD
* **Leverage**: 10×

**Calculation:**

**Initial Margin**:

$$
\frac{12,000 \times 10}{60,000 \times 10} = 0.2\ \text{BTC}
$$

**Opening Loss**:

$$
12,000 \times 10 \times \left| \min\left(0, 1 \times \left( \frac{1}{60,000} - \frac{1}{55,000} \right) \right) \right| = 0.181819\ \text{BTC}
$$

**Opening Margin**:

$$
0.2 + 0.181819 = 0.381819\ \text{BTC}
$$

### **2. Average Opening Price**

When multiple entries are made into the same position, the **average opening price** is recalculated using a **BTC-denominated formula**:

$$
\text{Average Opening Price} = \frac{\text{Total Contracts}}{\text{Total BTC Value}}
$$

#### **Example**

* Position 1: 1,000 contracts @ 5,000 USD
* Position 2: 2,000 contracts @ 6,000 USD

$$
\text{Total Contracts} = 1,000 + 2,000 = 3,000
$$

$$
\text{Total BTC Value} = \left( \frac{1,000}{5,000} \right) + \left( \frac{2,000}{6,000} \right) = 0.2 + 0.333333 = 0.533333\ \text{BTC}
$$

$$
\text{Average Opening Price} = \frac{3,000}{0.533333} = 5,625.00\ \text{USD}
$$

### **3. Profit and Loss Calculation**

PnL is calculated based on the **difference between inverse prices**, since these are **reverse (coin-margined)** contracts. Profits and losses are settled in **the base currency (e.g., BTC)**, not in USD.

#### **3.1 For Long Positions**

$$
\text{Unrealized PnL} = \text{Quantity} \times \left( \frac{1}{\text{Average Open Price}} - \frac{1}{\text{Mark Price}} \right)
$$

**Example**:

* 1,000 contracts long
* Average Price: 5,000
* Mark Price: 5,500

$$
\= 1,000 \times \left( \frac{1}{5,000} - \frac{1}{5,500} \right) = 0.01819\ \text{BTC}
$$

#### **3.2 For Short Positions**

$$
\text{Unrealized PnL} = \text{Quantity} \times \left( \frac{1}{\text{Mark Price}} - \frac{1}{\text{Average Open Price}} \right)
$$

**Example**:

* 1,000 contracts short
* Average Price: 5,000
* Mark Price: 4,500

$$
\= 1,000 \times \left( \frac{1}{4,500} - \frac{1}{5,000} \right) = 0.02223\ \text{BTC}
$$

### **Important Notes**

* **Reverse contracts** (coin-margined) are settled in the **base currency** (e.g., BTC).
* USD values are used **only for quotation purposes**, not for settlement.
* This structure ensures that traders **earn and lose in the underlying asset** they’re trading.
