Skip to content
Open
Show file tree
Hide file tree
Changes from 6 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions src/pages/portfolio/Overview.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,7 @@ export const Overview = () => {
PositionsTableColumnKey.Size,
PositionsTableColumnKey.Value,
PositionsTableColumnKey.PnL,
PositionsTableColumnKey.RealizedPnL,
PositionsTableColumnKey.Margin,
PositionsTableColumnKey.AverageOpen,
PositionsTableColumnKey.Oracle,
Expand Down
1 change: 1 addition & 0 deletions src/pages/portfolio/Portfolio.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -131,6 +131,7 @@ const PortfolioPage = () => {
FillsTableColumnKey.Price,
FillsTableColumnKey.Total,
FillsTableColumnKey.Fee,
FillsTableColumnKey.ClosedPnl,
FillsTableColumnKey.Liquidity,
]
}
Expand Down
1 change: 1 addition & 0 deletions src/pages/portfolio/Positions.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,7 @@ export const Positions = () => {
PositionsTableColumnKey.Size,
PositionsTableColumnKey.Value,
PositionsTableColumnKey.PnL,
PositionsTableColumnKey.RealizedPnL,
PositionsTableColumnKey.Margin,
PositionsTableColumnKey.AverageOpen,
PositionsTableColumnKey.Oracle,
Expand Down
2 changes: 2 additions & 0 deletions src/pages/trade/HorizontalPanel.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -150,6 +150,7 @@ export const HorizontalPanel = ({ isOpen = true, setIsOpen, handleStartResize }:
PositionsTableColumnKey.Size,
PositionsTableColumnKey.Value,
PositionsTableColumnKey.PnL,
PositionsTableColumnKey.RealizedPnL,
PositionsTableColumnKey.Margin,
PositionsTableColumnKey.AverageOpen,
PositionsTableColumnKey.Oracle,
Expand Down Expand Up @@ -326,6 +327,7 @@ export const HorizontalPanel = ({ isOpen = true, setIsOpen, handleStartResize }:
FillsTableColumnKey.Price,
FillsTableColumnKey.Total,
FillsTableColumnKey.Fee,
FillsTableColumnKey.ClosedPnl,
FillsTableColumnKey.Liquidity,
].filter(isTruthy)
}
Expand Down
4 changes: 4 additions & 0 deletions src/types/indexer/indexerManual.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ import {
IndexerPerpetualMarketStatus,
IndexerPerpetualMarketType,
IndexerPerpetualPositionResponseObject,
IndexerPositionSide,
IndexerTradeResponseObject,
IndexerTransferResponseObject,
} from './indexerApiGen';
Expand Down Expand Up @@ -156,6 +157,9 @@ export interface IndexerCompositeFillObject {
clientMetadata?: string | null;
subaccountNumber?: number;
market?: string;
positionSideBefore?: IndexerPositionSide;
positionSizeBefore?: number;
entryPriceBefore?: number;
}

export interface IndexerWsParentSubaccountSubscribedResponse {
Expand Down
57 changes: 57 additions & 0 deletions src/views/tables/FillsTable.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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');
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

instead of parseFloat, you can use maybeBigNumber

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

used MustNumber instead!


// Old fills are not supported so we show -- instead of 0
if (fill.positionSizeBefore === undefined) {
return '--';
}

// 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');

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;
} else {
closingPnl = (fill.entryPriceBefore! - price) * closingAmount;
}

return closingPnl - fee;
};

export type FillTableRow = {
marketSummary: Nullable<PerpetualMarketSummary>;
stepSizeDecimals: number;
Expand Down Expand Up @@ -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}
/>
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do we want to show positive/negative color for this cell?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

talked with shrenuj about it and we'll keep it neutral since that's what hyperliquid does

</TableCell>
);
},
},
[FillsTableColumnKey.Type]: {
columnKey: 'type',
getCellValue: (row) => row.type,
Expand Down
27 changes: 27 additions & 0 deletions src/views/tables/PositionsTable.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@ export enum PositionsTableColumnKey {
Size = 'Size',
Value = 'Value',
PnL = 'PnL',
RealizedPnL = 'RealizedPnL',
Margin = 'Margin',
AverageOpen = 'AverageOpen',
Oracle = 'Oracle',
Expand Down Expand Up @@ -210,6 +211,32 @@ const getPositionsTableColumnDef = ({
);
},
},
[PositionsTableColumnKey.RealizedPnL]: {
columnKey: 'realizedPnl',
getCellValue: (row) => row.realizedPnl.toNumber(),
label: stringGetter({ key: STRING_KEYS.REALIZED_PNL }),
renderCell: ({ realizedPnl }) => {
return !isTablet ? (
<TableCell>
<$OutputSigned
sign={getNumberSign(realizedPnl)}
type={OutputType.Fiat}
value={realizedPnl}
showSign={ShowSign.Negative}
/>
</TableCell>
) : (
<TableCell stacked>
<$HighlightOutput
isNegative={MustBigNumber(realizedPnl).isNegative()}
type={OutputType.Fiat}
value={realizedPnl}
showSign={ShowSign.Both}
/>
</TableCell>
);
},
},
[PositionsTableColumnKey.Market]: {
columnKey: 'market',
getCellValue: (row) => row.marketSummary?.displayableTicker,
Expand Down
Loading