> 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
```


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## 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, and the optional `goal` query parameter:

```
GET https://d8x.gitbook.io/d8x/perpetual-contracts-guide/profit-loss-and-position-marking.md?ask=<question>&goal=<endgoal>
```

`ask` is the immediate question: it should be specific, self-contained, and written in natural language.
`goal` is optional and describes the broader end goal you are ultimately trying to accomplish on behalf of the user. GitBook uses it to tailor the answer towards what is most useful for that goal.

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.
