# Margin and Profit/Loss Calculations

### **1. Opening Margin**

The **opening margin** consists of two components:

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

Including the **opening loss** (if any) at the time of order placement helps **prevent immediate liquidation** due to unfavorable price movement at the time of opening.

#### **1.1 Initial Margin**

$$
\text{Initial Margin} = \frac{\text{Notional Value}}{\text{Leverage}}
$$

For USDT-margined contracts:

$$
\text{Notional Value} = \text{Order Price} \times \text{Quantity} \times \text{Contract Size}
$$

#### **1.2 Opening Loss**

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

Where:

* **Order Direction** = `1` for Long, `-1` for Short

#### **Example**

* **Contract**: BTC/USDT
* **Order Price**: 60,000 USDT
* **Mark Price**: 55,000 USDT
* **Order Quantity**: 10,000 contracts
* **Contract Size**: 0.0001 BTC
* **Leverage**: 10×

$$
\text{Initial Margin} = \frac{60,000 \times 10,000 \times 0.0001}{10} = 6,000\ \text{USDT}
$$

$$
\text{Opening Loss} = 10,000 \times 0.0001 \times 5,000 = 5,000\ \text{USDT}
$$

$$
\text{Opening Margin} = 6,000 + 5,000 = 11,000\ \text{USDT}
$$

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

When a trader opens **multiple positions**, the system recalculates the **average opening price** using a **weighted average**:

$$
\text{Average Opening Price} = \frac{\sum (\text{Contract Quantity} \times \text{Price})}{\sum \text{Contract Quantity}}
$$

#### **Example**

* Position 1: 0.5 BTC at 5,000 USDT
* Position 2: 0.3 BTC at 6,000 USDT

$$
\text{Total Value} = (0.5 \times 5,000) + (0.3 \times 6,000) = 2,500 + 1,800 = 4,300
$$

$$
\text{Total Quantity} = 0.5 + 0.3 = 0.8\ \text{BTC}
$$

$$
\text{Average Opening Price} = \frac{4,300}{0.8} = 5,375\ \text{USDT}
$$

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

The **unrealized profit and loss (PnL)** is shown in real time and calculated differently for **long** and **short** positions.

#### **For Long Positions**

$$
\text{PnL} = \text{Quantity} \times (\text{Mark Price} - \text{Average Opening Price})
$$

**Example:**

* Quantity: 0.2 BTC
* Opening Price: 7,000
* Mark Price: 7,500

$$
\text{PnL} = 0.2 \times (7,500 - 7,000) = 100\ \text{USDT}
$$

#### **For Short Positions**

$$
\text{PnL} = \text{Quantity} \times (\text{Average Opening Price} - \text{Mark Price})
$$

**Example:**

* Quantity: 0.4 BTC
* Opening Price: 6,000
* Mark Price: 5,000

$$
\text{PnL} = 0.4 \times (6,000 - 5,000) = 400\ \text{USDT}
$$


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://docs.coinlocally.com/perpetual-contracts/usdt-margined-perpetual-contract/margin-and-profit-loss-calculations.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
