Skip to content

Commit ea26132

Browse files
authored
Fix position chart repair trade markers
Exclude repair counter-trades from position chart markers and use reserve notional as the marker USD value fallback for vault-style trades.
1 parent 05328e4 commit ea26132

2 files changed

Lines changed: 34 additions & 1 deletion

File tree

src/lib/trade-executor/helpers/position-chart.test.ts

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -151,6 +151,15 @@ describe('buildPositionChartsModel', () => {
151151
planned_quantity: '0',
152152
executed_quantity: '0',
153153
flags: []
154+
}),
155+
createTrade({
156+
trade_id: 4,
157+
trade_type: 'repair',
158+
planned_quantity: '5',
159+
executed_quantity: '0',
160+
planned_reserve: '5',
161+
executed_reserve: '0',
162+
repaired_trade_id: 2
154163
})
155164
]
156165
});
@@ -161,6 +170,24 @@ describe('buildPositionChartsModel', () => {
161170
expect(model.underlyingPrice.markers[0].tradeId).toBe(1);
162171
});
163172

173+
test('uses planned reserve amount for vault marker USD value when executed quantity is zero', () => {
174+
const payload = createPayload({
175+
trades: [
176+
createTrade({
177+
planned_quantity: '123.45',
178+
executed_quantity: '0',
179+
planned_reserve: '123.45',
180+
executed_reserve: '0'
181+
})
182+
]
183+
});
184+
185+
const model = buildPositionChartsModel(payload);
186+
187+
expect(model.underlyingPrice.markers).toHaveLength(1);
188+
expect(model.underlyingPrice.markers[0].tradeValueUsd).toBe(123.45);
189+
});
190+
164191
test('falls back to underlying_price samples when price_history is missing', () => {
165192
const payload = createPayload({
166193
price_history: [],

src/lib/trade-executor/helpers/position-chart.ts

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -96,6 +96,7 @@ function normalisePoints(points: PositionChartPoint[]) {
9696
function getMarkerTrades(trades: TradeInfo[], tradePathBase: string) {
9797
return trades
9898
.filter((trade) => !trade.repaired_at)
99+
.filter((trade) => !trade.isRepair)
99100
.filter((trade) => {
100101
const plannedQuantity = trade.planned_quantity;
101102
const executedQuantity = trade.executed_quantity ?? 0;
@@ -114,7 +115,7 @@ function getMarkerTrades(trades: TradeInfo[], tradePathBase: string) {
114115
directionLabel: trade.direction === TradeDirections.Enter ? 'Increase' : 'Decrease',
115116
quantity: trade.executed_quantity ?? trade.planned_quantity ?? null,
116117
price: trade.executed_price ?? null,
117-
tradeValueUsd: trade.value,
118+
tradeValueUsd: getTradeValueUsd(trade),
118119
tradeUrl: `${tradePathBase}/trade-${trade.trade_id}`
119120
}
120121
];
@@ -163,3 +164,8 @@ function getNearestPoint(points: PositionChartPoint[], timestamp: number) {
163164
function getTradeTimestamp(trade: TradeInfo) {
164165
return trade.executed_at ?? trade.failed_at ?? trade.started_at ?? trade.opened_at ?? null;
165166
}
167+
168+
function getTradeValueUsd(trade: TradeInfo) {
169+
const reserveValue = Math.abs(trade.executed_reserve || trade.planned_reserve || 0);
170+
return reserveValue || trade.value;
171+
}

0 commit comments

Comments
 (0)