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 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.
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 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 CustomFields
interface, you can include a pl
property as a custom field within the Position
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.
Additionally, you need to implement methods related to individual positions: individualPositions
, individualPositionUpdate
, and individualPositionPLUpdate
.
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. -
Ticks mode. The library ignores your provided
pl
value. Instead, it automatically calculates the P&L values using its own internal formula:((currentPrice - avgPrice) / avgPrice) * 100 * sign
where:
-
Percentage mode. The library ignores your provided
pl
value. Instead, it automatically calculates the P&L values using its own internal formula:((currentPrice - avgPrice) * sign) / tickSize
where:
currentPrice
is the current bid/ask/last price of the symbol.avgPrice
is theavgPrice
value.sign
is theside
value. 1 for long (Buy) positions, -1 for short (Sell) positions.tickSize
is thepipSize
property provided insymbolInfo
if available;minTick
otherwise.