-
Notifications
You must be signed in to change notification settings - Fork 105
feat: closed pnl #1966
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
feat: closed pnl #1966
Changes from 6 commits
85b737a
9d9907b
3da0dbd
7f2d73a
6ec1bac
dbebe3c
2e0461e
7986f42
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -55,12 +55,53 @@ export enum FillsTableColumnKey { | |
| AmountTag = 'Amount-Tag', | ||
| Total = 'Total', | ||
| Fee = 'Fee', | ||
| ClosedPnl = 'ClosedPnl', | ||
|
|
||
| // Tablet Only | ||
| TypeAmount = 'Type-Amount', | ||
| PriceFee = 'Price-Fee', | ||
| } | ||
|
|
||
| const calculateClosedPnl = (fill: FillTableRow) => { | ||
| const fee = parseFloat(fill.fee ?? '0'); | ||
|
||
|
|
||
| // Old fills are not supported so we show -- instead of 0 | ||
| if (fill.positionSizeBefore === undefined) { | ||
| return '--'; | ||
pp346 marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| } | ||
|
|
||
| // No position before = opening trade, only fees realize | ||
| if (fill.positionSizeBefore === 0) { | ||
| return -fee; | ||
| } | ||
|
|
||
| // Check if position is reducing (opposite side) | ||
| const isReducing = | ||
| (fill.positionSideBefore === 'LONG' && fill.side === 'SELL') || | ||
| (fill.positionSideBefore === 'SHORT' && fill.side === 'BUY'); | ||
pp346 marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
|
|
||
| if (!isReducing) { | ||
| // Position increasing (same side), only fees realize | ||
| return -fee; | ||
| } | ||
|
|
||
| const size = parseFloat(fill.size ?? '0'); | ||
| const price = parseFloat(fill.price ?? '0'); | ||
|
|
||
| // Position reducing - cap closing amount to actual position size | ||
| const closingAmount = Math.min(size, fill.positionSizeBefore); | ||
|
|
||
| // Calculate P&L only on the closing portion | ||
| let closingPnl: number; | ||
| if (fill.positionSideBefore === 'LONG') { | ||
| closingPnl = (price - fill.entryPriceBefore!) * closingAmount; | ||
pp346 marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| } else { | ||
| closingPnl = (fill.entryPriceBefore! - price) * closingAmount; | ||
| } | ||
pp346 marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
|
|
||
| return closingPnl - fee; | ||
| }; | ||
|
|
||
| export type FillTableRow = { | ||
| marketSummary: Nullable<PerpetualMarketSummary>; | ||
| stepSizeDecimals: number; | ||
|
|
@@ -215,6 +256,22 @@ const getFillsTableColumnDef = ({ | |
| </TableCell> | ||
| ), | ||
| }, | ||
| [FillsTableColumnKey.ClosedPnl]: { | ||
| columnKey: 'closedPnl', | ||
| getCellValue: (row) => calculateClosedPnl(row), | ||
| label: stringGetter({ key: STRING_KEYS.CLOSED_PNL }), | ||
| renderCell: (row) => { | ||
| const closedPnl = calculateClosedPnl(row); | ||
| return ( | ||
| <TableCell> | ||
| <Output | ||
| type={closedPnl === '--' ? OutputType.Text : OutputType.Fiat} | ||
| value={closedPnl} | ||
| /> | ||
|
||
| </TableCell> | ||
| ); | ||
| }, | ||
| }, | ||
| [FillsTableColumnKey.Type]: { | ||
| columnKey: 'type', | ||
| getCellValue: (row) => row.type, | ||
|
|
||
Uh oh!
There was an error while loading. Please reload this page.