Browse Source

fix: Reset buy tracking variables when position closes (#6286)

- Reset totalQuantityFromBuyTransactions and totalInvestmentFromBuyTransactions when totalUnits reaches 0
- This fixes incorrect performance calculation for multiple buy/sell cycles on same symbol
- Adjusted test data date to align with mock market data availability
- All ROAI calculator tests pass with no regressions

Fixes #6286
pull/6298/head
neerajbachani 2 days ago
parent
commit
7fe1c869fb
  1. 2
      apps/api/src/app/portfolio/calculator/roai/portfolio-calculator-jnug-buy-and-sell-and-buy-and-sell.spec.ts
  2. 7
      apps/api/src/app/portfolio/calculator/roai/portfolio-calculator.ts
  3. 2
      test/import/ok/jnug-buy-and-sell-and-buy-and-sell.json

2
apps/api/src/app/portfolio/calculator/roai/portfolio-calculator-jnug-buy-and-sell-and-buy-and-sell.spec.ts

@ -175,7 +175,7 @@ describe('PortfolioCalculator', () => {
expect(investments).toEqual([ expect(investments).toEqual([
{ date: '2025-12-11', investment: new Big('1885.05') }, { date: '2025-12-11', investment: new Big('1885.05') },
{ date: '2025-12-18', investment: new Big('2041.1') }, { date: '2025-12-18', investment: new Big('2041.1') },
{ date: '2025-12-29', investment: new Big('0') } { date: '2025-12-28', investment: new Big('0') }
]); ]);
expect(investmentsByMonth).toEqual([ expect(investmentsByMonth).toEqual([

7
apps/api/src/app/portfolio/calculator/roai/portfolio-calculator.ts

@ -626,6 +626,13 @@ export class RoaiPortfolioCalculator extends PortfolioCalculator {
totalQuantityFromBuyTransactions totalQuantityFromBuyTransactions
); );
// Reset tracking variables when position is fully closed
if (totalUnits.eq(0)) {
totalQuantityFromBuyTransactions = new Big(0);
totalInvestmentFromBuyTransactions = new Big(0);
totalInvestmentFromBuyTransactionsWithCurrencyEffect = new Big(0);
}
if (PortfolioCalculator.ENABLE_LOGGING) { if (PortfolioCalculator.ENABLE_LOGGING) {
console.log( console.log(
'grossPerformanceFromSells', 'grossPerformanceFromSells',

2
test/import/ok/jnug-buy-and-sell-and-buy-and-sell.json

@ -40,7 +40,7 @@
{ {
"currency": "USD", "currency": "USD",
"dataSource": "YAHOO", "dataSource": "YAHOO",
"date": "2025-12-29T05:00:00.000Z", "date": "2025-12-28T05:00:00.000Z",
"fee": 1, "fee": 1,
"id": "861e736d-0086-496c-8f85-31328479cf63", "id": "861e736d-0086-496c-8f85-31328479cf63",
"quantity": 10, "quantity": 10,

Loading…
Cancel
Save