> ## Documentation Index
> Fetch the complete documentation index at: https://docs.fairground.fi/llms.txt
> Use this file to discover all available pages before exploring further.

# Miscellaneous UI

> UI-specific components and their descriptions

## Segment PnL and Lifetime PnL

Fairground separates a position's recent performance from its performance over its entire lifecycle.

**Segment PnL** focuses on the period since the position last changed, while **Lifetime PnL** carries across every change from original position open to full position close.

Overall, Segment PnL is best used to understand position performance on a trade-by-trade basis, whereas Lifetime PnL is best used to understand overall position profitability.

<Accordion title="Segment & Lifetime PnL math">
  A position's *current value* is its margin plus unrealized PnL at the current price. This is the value that would be returned to the user if they closed the position at the current price.

  ```math theme={null}
  V_{\mathrm{now}}
  =
  M+\sigma\,(P_{\mathrm{now}}-P_{\mathrm{entry}})\,S
  ```

  Where:

  * $M$ is the margin currently backing the position
  * $P_{\mathrm{entry}}$ is the entry price and $P_{\mathrm{now}}$ is the current mark price
  * $S$ is the position size
  * $\sigma=+1$ for a long and $\sigma=-1$ for a short

  $V_{\mathrm{last\ touch}}^+$ is what the current value of the position was at the time of the most recent margin or size change (the last "touch").

  **Segment PnL:**

  ```math theme={null}
  \mathrm{SegmentPnL}
  =
  V_{\mathrm{now}}-V_{\mathrm{last\ touch}}^+
  ```

  **Lifetime PnL:**

  ```math theme={null}
  \mathrm{LifetimePnL}
  =
  V_{\mathrm{now}}+C_{\mathrm{out}}-C_{\mathrm{in}}
  ```

  Where:

  * $V_{\mathrm{now}}$ is the marked value defined above, taken as zero once the position closes
  * $C_{\mathrm{out}}$ is all cash credited back to your wallet from the position
  * $C_{\mathrm{in}}$ is all cash debited from your wallet for the position (e.g. margin added)

  Because deposits and withdrawals enter as cash flows ($C_{\mathrm{in}}$ and
  $C_{\mathrm{out}}$), adding margin alone doesn't create a profit and removing margin
  alone doesn't create a loss.
</Accordion>

### Segment PnL

Segment PnL is the gain or loss on the current *segment* of your position. A segment is
the stretch of time during which the position hasn't been interacted with by the trader. A new segment begins
whenever you change the position's margin or size.

The first segment begins when the position opens. Later actions can start a new segment:

| Action                                   | Segment treatment                                                       |
| ---------------------------------------- | ----------------------------------------------------------------------- |
| Add margin                               | Starts a new segment after a margin add                                 |
| Remove margin                            | Starts a new segment after a margin remove                              |
| Increase the position                    | Starts a new segment after a position increase                          |
| Partially close or decrease the position | Starts a new segment from the remaining position                        |
| ADL size reduction                       | Starts a new segment by treating the reduction like a position decrease |
| Fully close the position                 | Ends the final segment                                                  |

### Lifetime PnL

Lifetime PnL is the total dollar gain or loss across the position's complete lifecycle. It does not reset when you add or remove margin, increase the position, or partially close it.

### Example

Say you open a long with `$1,000` of margin at `10×` leverage, giving you `$10,000` of
exposure at an entry price of `$100`. Your position value, the margin plus any
unrealized profit or loss starts at `$1,000`, the same as your margin.

Segment PnL and Lifetime PnL both start at `$0`.

<Steps>
  <Step title="Price rises 2% to $102 → position value $1,200">
    A 2% price move on 10× leverage is a 20% gain on margin. Unrealized PnL is
    `+$200`, lifting position value to `$1,200` <br />- Segment PnL: `+$200
            (+20%)` <br />- Lifetime PnL: `+$200 (+20%)`
  </Step>

  <Step title="Add $300 margin → position value $1,500">
    Adding margin isn't a "gain", so it opens a fresh segment from the new
    `$1,500` baseline and segment PnL resets to `$0`. The `$300` is recorded
    as cash in, so Lifetime PnL stays at `+$200`. Your leverage also drops: the
    same `~$10,000` of exposure now sits on more margin, taking you from `10×`
    to about `6.8×`.
    <br />- Segment PnL: `+ $0 (+0%)` <br />- Lifetime PnL: `+$200 (+15.4%)`
  </Step>

  <Step title="Price rises to $105 → position value $1,800">
    Another `+$300` of unrealized profit brings your position value to `$1,800`
    <br />- Segment PnL: `+$300 (+20%)` measured from the new `$1,500` position value
    <br />- Lifetime PnL: `+$500 (+38.5%)`.
  </Step>

  <Step title="Position is closed at $1,800">
    `$1,300` of margin was placed across the two deposits and `$1,800` total was withdrawn
    <br />- Segment PnL: `+$300 (+20%)`
    <br />- Lifetime PnL: `+$500 (+38.5%)`
  </Step>
</Steps>
