> For the complete documentation index, see [llms.txt](https://d8x.gitbook.io/d8x/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://d8x.gitbook.io/d8x/perpetual-contracts-guide/profit-loss-and-position-marking.md).

# Profit/Loss & Position Marking

<figure><picture><source srcset="/files/kv87fJjXyd3WrbhpON29" media="(prefers-color-scheme: dark)"><img src="/files/zhyK0yE6hv5tx6Ao6r03" 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
```
