# Profit/Loss & Position Marking

<figure><picture><source srcset="https://1690371742-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FsivfyCjOhJUkKrxqsPg4%2Fuploads%2FSVj7UXyPeijgFv3RuiDK%2Fbanner_calculations.png?alt=media&#x26;token=80ff10d7-f2a0-48dd-ab97-62cc1060ad32" media="(prefers-color-scheme: dark)"><img src="https://1690371742-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FsivfyCjOhJUkKrxqsPg4%2Fuploads%2FkugztR3ZPBi3yIxl5GTz%2Fbanner_calculations_l.png?alt=media&#x26;token=ac657fd1-9490-4cb8-be27-a2ba549d2763" alt=""></picture><figcaption></figcaption></figure>

## Profit/Loss

### Funding Payments

The funding rate is accrued continuously on the position size, and paid to/from the margin account in collateral currency at spot. Funding payments are realized when a trader modifies her position size, i.e. by performing an opening or closing trade.

### Opening Trades

Opening a position or increasing the size of an existing position:

```
Locked-in-Value += Index Price * Trade Amount

Cash in margin account += ((Execution Price - Index Price) * Trade Amount
                          - Fee rate * |Trade Amount| * Index Price) / Collateral spot price
                          - Accumulated funding since last trade
```

### Closing Trades

Closing or reducing the size of an existing position:

```
Locked-in-Value += Average Entry Price * Trade Amount
where 
Average Entry Price = |Locked-in-Value| / |Position size|

Cash in margin account += ((Execution Price - Index Price) * Trade Amount
                          - Fee rate * |Trade Amount| * Index Price) / Collateral spot price
                          - Accumulated funding since last trade

```

## Position Marking

The mark price determines Margin Account calculations and Liquidations.

### Unrealized PnL

Current profit and loss of a given open position, in collateral currency:

```
Unrealized PnL = (Mark Price * Position Size  - Locked-in-Value) / Collateral spot price
                - Unpaid accumulated funding
```

### Margin Balance

Current value of margin account including unrealized PnL, in collateral currency:

```
Margin Balance = Cash in margin account + Unrealized PnL
```

### Leverage

Ratio between the position value at mark price in collateral currency and margin balance:

```
Leverage =  (|Position size| * Mark Price / Collateral spot price) / Margin Balance
```

### Margin Rate

Inverse of position leverage:

```
Margin Rate = 1 / Leverage
```


---

# 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://d8x.gitbook.io/d8x/perpetual-contracts-guide/profit-loss-and-position-marking.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.
