# Profit and loss

## Overview

**Profit and loss** (P&L) values indicate the current profit or loss status of an account or a [position].
P&L values are displayed in the [Account Manager] and on the chart for the related position.

You can control how P&L values are calculated using the [`supportPLUpdate`] flag.
The default value is `true`.
This means you should calculate the P&L value for a position on your backend server and [provide](#provide-pl-values) it to the library.

If [`supportPLUpdate`] is set to `false`, the library automatically calculates P&L values as the difference between the current trade and the average position price.
However, we recommend using **your own P&L calculations**.
This helps avoid any discrepancies between the P&L values of the account and positions due to potential delays.

## Provide P&L values

You can provide P&L data to the library using two distinct methods.
The method you choose determines which parts of the user interface are updated.

:::warning

For both methods, the P&L value must be a number representing the **monetary** profit or loss.
Do not provide it as ticks or a percentage.
This is because the library only uses your value for the *Money* [display mode](#display-modes-and-calculation-logic) and calculates P&L internally for *Ticks* and *Percentage* modes.

:::

### Through position object

This approach updates the P&L value on both the chart's position line and in the [Account Manager].
Since the [`Position`] interface extends the [`WithExtraData`] interface, you can include a `pl` property as a custom field within the position's [`extra`] object.

- When first loading positions, call the [`positions`] method.
- When a position's data changes, call the [`positionUpdate`] method.

### Through plUpdate method

This approach is designed specifically for pushing frequent P&L updates without resending the entire position object.
Once calculated P&L, call the [`plUpdate`] method, providing the `pl` parameter.

This method updates the P&L value in the [Account Manager] only.
It does not affect the P&L displayed on the chart.

## Handling position netting

If [position netting] is enabled, the approaches for providing P&L are the same as [described above](#provide-pl-values).
Additionally, you need to implement methods related to individual positions: [`individualPositions`], [`individualPositionUpdate`], and [`individualPositionPLUpdate`].

:::info

When position netting is enabled, the chart displays P&L for individual positions only.
P&L values for net positions are not shown.

:::

## Display modes and calculation logic

The library allows users to switch between different P&L display modes: *Money*, *Ticks*, and *Percentage* through *Settings → Positions → Profit & loss*.
The library's behavior depends on the user's selected mode.

- **Money mode**. The library displays the exact `pl` value that you provide through either of the [methods above](#provide-pl-values).
- **Ticks mode**. The library ignores your provided `pl` value. Instead, it automatically calculates the P&L values using its own internal formula:

  ```js
  ((currentPrice - avgPrice) * sign) / tickSize
  ```

  where:
  - `currentPrice` is the current bid/ask/last price of the symbol.
  - `avgPrice` is the [`avgPrice`] value.
  - `sign` is the [`side`] value. 1 for long (Buy) positions, -1 for short (Sell) positions.
  - `tickSize` is the [`pipSize`] property provided in [`symbolInfo`] if available; [`minTick`] otherwise.

- **Percentage mode**. The library ignores your provided `pl` value. Instead, it automatically calculates the P&L values using its own internal formula:

  ```js
  ((currentPrice - avgPrice) / avgPrice) * 100 * sign
  ```

  where:
  - `currentPrice` is the current bid/ask/last price of the symbol.
  - `avgPrice` is the [`avgPrice`] value.
  - `sign` is the [`side`] value. `1` for long (Buy) positions, `-1` for short (Sell) positions.

[Account Manager]: https://charting-library-docs.xstaging.tv/latest/trading_terminal/account-manager.md
[position]: https://charting-library-docs.xstaging.tv/latest/trading_terminal/trading-concepts/positions.md
[position netting]: https://charting-library-docs.xstaging.tv/latest/trading_terminal/trading-concepts/positions.md#position-netting

[`avgPrice`]: https://charting-library-docs.xstaging.tv/latest/api/interfaces/Charting_Library.Position#avgprice
[`extra`]: https://charting-library-docs.xstaging.tv/latest/api/interfaces/Charting_Library.WithExtraData#extra
[`individualPositions`]: https://charting-library-docs.xstaging.tv/latest/api/interfaces/Charting_Library.IBrokerTerminal#individualpositions
[`individualPositionPLUpdate`]: https://charting-library-docs.xstaging.tv/latest/api/interfaces/Charting_Library.IBrokerConnectionAdapterHost#individualpositionplupdate
[`individualPositionUpdate`]: https://charting-library-docs.xstaging.tv/latest/api/interfaces/Charting_Library.IBrokerConnectionAdapterHost#individualpositionupdate
[`minTick`]: https://charting-library-docs.xstaging.tv/latest/api/interfaces/Charting_Library.InstrumentInfo#mintick
[`pipSize`]: https://charting-library-docs.xstaging.tv/latest/api/interfaces/Charting_Library.InstrumentInfo#pipsize
[`plUpdate`]: https://charting-library-docs.xstaging.tv/latest/api/interfaces/Charting_Library.IBrokerConnectionAdapterHost#plupdate
[`Position`]: https://charting-library-docs.xstaging.tv/latest/api/interfaces/Charting_Library.Position
[`positions`]: https://charting-library-docs.xstaging.tv/latest/api/interfaces/Charting_Library.IBrokerTerminal#positions
[`positionUpdate`]: https://charting-library-docs.xstaging.tv/latest/api/interfaces/Charting_Library.IBrokerConnectionAdapterHost#positionupdate
[`side`]: https://charting-library-docs.xstaging.tv/latest/api/interfaces/Charting_Library.Position#side
[`supportPLUpdate`]: https://charting-library-docs.xstaging.tv/latest/api/interfaces/Charting_Library.BrokerConfigFlags#supportplupdate
[`symbolInfo`]: https://charting-library-docs.xstaging.tv/latest/api/interfaces/Charting_Library.IBrokerTerminal#symbolinfo
[`WithExtraData`]: https://charting-library-docs.xstaging.tv/latest/api/interfaces/Charting_Library.WithExtraData

---

## Sitemap

- [All documentation pages](https://charting-library-docs.xstaging.tv/llms.txt)
- [Full page map with headings](https://charting-library-docs.xstaging.tv/docs_map.md)
