From d0b26963aa9c81c49f7e4a1380e83481315bb158 Mon Sep 17 00:00:00 2001 From: KenTandrian Date: Thu, 11 Sep 2025 23:41:09 +0700 Subject: [PATCH 1/5] fix(api): add logic for selling all units --- .../app/portfolio/calculator/portfolio-calculator.ts | 10 +++++++++- 1 file changed, 9 insertions(+), 1 deletion(-) diff --git a/apps/api/src/app/portfolio/calculator/portfolio-calculator.ts b/apps/api/src/app/portfolio/calculator/portfolio-calculator.ts index e4d9cdfe8..d3984ad0a 100644 --- a/apps/api/src/app/portfolio/calculator/portfolio-calculator.ts +++ b/apps/api/src/app/portfolio/calculator/portfolio-calculator.ts @@ -937,11 +937,19 @@ export abstract class PortfolioCalculator { ); } } else if (type === 'SELL') { - if (oldAccumulatedSymbol.investment.gt(0)) { + if ( + oldAccumulatedSymbol.quantity.gt(0) && + oldAccumulatedSymbol.quantity.eq(quantity) + ) { + // Selling all units, so investment should be 0 + investment = new Big(0); + } else if (oldAccumulatedSymbol.investment.gt(0)) { + // Selling part of a positive investment investment = oldAccumulatedSymbol.investment.minus( quantity.mul(oldAccumulatedSymbol.averagePrice) ); } else { + // Selling part of a negative investment (short sell) investment = oldAccumulatedSymbol.investment.minus( quantity.mul(unitPrice) ); From 1cca2a4aba89692b553a8ef504ad8d0a15de101f Mon Sep 17 00:00:00 2001 From: KenTandrian Date: Thu, 11 Sep 2025 23:41:29 +0700 Subject: [PATCH 2/5] feat(test): add test case for repeating decimals --- ...folio-calculator-msft-buy-and-sell.spec.ts | 146 ++++++++++++++++++ 1 file changed, 146 insertions(+) create mode 100644 apps/api/src/app/portfolio/calculator/roai/portfolio-calculator-msft-buy-and-sell.spec.ts diff --git a/apps/api/src/app/portfolio/calculator/roai/portfolio-calculator-msft-buy-and-sell.spec.ts b/apps/api/src/app/portfolio/calculator/roai/portfolio-calculator-msft-buy-and-sell.spec.ts new file mode 100644 index 000000000..9bc874fd2 --- /dev/null +++ b/apps/api/src/app/portfolio/calculator/roai/portfolio-calculator-msft-buy-and-sell.spec.ts @@ -0,0 +1,146 @@ +import { Activity } from '@ghostfolio/api/app/order/interfaces/activities.interface'; +import { + activityDummyData, + symbolProfileDummyData, + userDummyData +} from '@ghostfolio/api/app/portfolio/calculator/portfolio-calculator-test-utils'; +import { PortfolioCalculatorFactory } from '@ghostfolio/api/app/portfolio/calculator/portfolio-calculator.factory'; +import { CurrentRateService } from '@ghostfolio/api/app/portfolio/current-rate.service'; +import { CurrentRateServiceMock } from '@ghostfolio/api/app/portfolio/current-rate.service.mock'; +import { RedisCacheService } from '@ghostfolio/api/app/redis-cache/redis-cache.service'; +import { RedisCacheServiceMock } from '@ghostfolio/api/app/redis-cache/redis-cache.service.mock'; +import { ConfigurationService } from '@ghostfolio/api/services/configuration/configuration.service'; +import { ExchangeRateDataService } from '@ghostfolio/api/services/exchange-rate-data/exchange-rate-data.service'; +import { PortfolioSnapshotService } from '@ghostfolio/api/services/queues/portfolio-snapshot/portfolio-snapshot.service'; +import { PortfolioSnapshotServiceMock } from '@ghostfolio/api/services/queues/portfolio-snapshot/portfolio-snapshot.service.mock'; +import { parseDate } from '@ghostfolio/common/helper'; +import { PerformanceCalculationType } from '@ghostfolio/common/types/performance-calculation-type.type'; + +jest.mock('@ghostfolio/api/app/portfolio/current-rate.service', () => { + return { + CurrentRateService: jest.fn().mockImplementation(() => { + return CurrentRateServiceMock; + }) + }; +}); + +jest.mock( + '@ghostfolio/api/services/queues/portfolio-snapshot/portfolio-snapshot.service', + () => { + return { + // eslint-disable-next-line @typescript-eslint/naming-convention + PortfolioSnapshotService: jest.fn().mockImplementation(() => { + return PortfolioSnapshotServiceMock; + }) + }; + } +); + +jest.mock('@ghostfolio/api/app/redis-cache/redis-cache.service', () => { + return { + // eslint-disable-next-line @typescript-eslint/naming-convention + RedisCacheService: jest.fn().mockImplementation(() => { + return RedisCacheServiceMock; + }) + }; +}); + +describe('PortfolioCalculator', () => { + let configurationService: ConfigurationService; + let currentRateService: CurrentRateService; + let exchangeRateDataService: ExchangeRateDataService; + let portfolioCalculatorFactory: PortfolioCalculatorFactory; + let portfolioSnapshotService: PortfolioSnapshotService; + let redisCacheService: RedisCacheService; + + beforeEach(() => { + configurationService = new ConfigurationService(); + currentRateService = new CurrentRateService(null, null, null, null); + exchangeRateDataService = new ExchangeRateDataService( + null, + null, + null, + null + ); + portfolioSnapshotService = new PortfolioSnapshotService(null); + redisCacheService = new RedisCacheService(null, null); + portfolioCalculatorFactory = new PortfolioCalculatorFactory( + configurationService, + currentRateService, + exchangeRateDataService, + portfolioSnapshotService, + redisCacheService + ); + }); + + describe('get transaction point', () => { + it('with MSFT buy and sell with multiple of 3', async () => { + jest.useFakeTimers().setSystemTime(parseDate('2024-04-01').getTime()); + + const activities: Activity[] = [ + { + ...activityDummyData, + date: new Date('2024-03-08'), + feeInAssetProfileCurrency: 0, + quantity: 1, + SymbolProfile: { + ...symbolProfileDummyData, + currency: 'USD', + dataSource: 'YAHOO', + name: 'Microsoft Inc.', + symbol: 'MSFT' + }, + type: 'BUY', + unitPriceInAssetProfileCurrency: 408 + }, + { + ...activityDummyData, + date: new Date('2024-03-13'), + quantity: 2, + feeInAssetProfileCurrency: 0, + SymbolProfile: { + ...symbolProfileDummyData, + currency: 'USD', + dataSource: 'YAHOO', + name: 'Microsoft Inc.', + symbol: 'MSFT' + }, + type: 'BUY', + unitPriceInAssetProfileCurrency: 400 + }, + { + ...activityDummyData, + date: new Date('2024-03-14'), + quantity: 3, + feeInAssetProfileCurrency: 0, + SymbolProfile: { + ...symbolProfileDummyData, + currency: 'USD', + dataSource: 'YAHOO', + name: 'Microsoft Inc.', + symbol: 'MSFT' + }, + type: 'SELL', + unitPriceInAssetProfileCurrency: 411 + } + ]; + + const portfolioCalculator = portfolioCalculatorFactory.createCalculator({ + activities, + calculationType: PerformanceCalculationType.ROAI, + currency: 'USD', + userId: userDummyData.id + }); + + const transactionPoints = portfolioCalculator.getTransactionPoints(); + const lastTransactionPoint = + transactionPoints[transactionPoints.length - 1]; + const position = lastTransactionPoint.items.find( + (item) => item.symbol === 'MSFT' + ); + + expect(position.investment.toNumber()).toBe(0); + expect(position.quantity.toNumber()).toBe(0); + }); + }); +}); From ca268b2331657a7c2aaee9e6731a8d76b07dc1f7 Mon Sep 17 00:00:00 2001 From: KenTandrian Date: Thu, 11 Sep 2025 23:43:42 +0700 Subject: [PATCH 3/5] feat(docs): update changelog --- CHANGELOG.md | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index 03e366105..b270a331e 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -19,6 +19,10 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 - Refactored the rules component to standalone - Refactored the subscription interstitial dialog component to standalone +### Fixed + +- Fixed an issue in the investment calculation when selling all units of a holding + ## 2.197.0 - 2025-09-07 ### Added From 3e12ee3220ef88f9a2a92d8be64553c10948d5aa Mon Sep 17 00:00:00 2001 From: KenTandrian Date: Thu, 18 Sep 2025 22:46:15 +0700 Subject: [PATCH 4/5] feat(api): update rounding logic --- .../portfolio/calculator/portfolio-calculator.ts | 15 ++++++--------- 1 file changed, 6 insertions(+), 9 deletions(-) diff --git a/apps/api/src/app/portfolio/calculator/portfolio-calculator.ts b/apps/api/src/app/portfolio/calculator/portfolio-calculator.ts index d3984ad0a..807566217 100644 --- a/apps/api/src/app/portfolio/calculator/portfolio-calculator.ts +++ b/apps/api/src/app/portfolio/calculator/portfolio-calculator.ts @@ -937,25 +937,22 @@ export abstract class PortfolioCalculator { ); } } else if (type === 'SELL') { - if ( - oldAccumulatedSymbol.quantity.gt(0) && - oldAccumulatedSymbol.quantity.eq(quantity) - ) { - // Selling all units, so investment should be 0 - investment = new Big(0); - } else if (oldAccumulatedSymbol.investment.gt(0)) { - // Selling part of a positive investment + if (oldAccumulatedSymbol.investment.gt(0)) { investment = oldAccumulatedSymbol.investment.minus( quantity.mul(oldAccumulatedSymbol.averagePrice) ); } else { - // Selling part of a negative investment (short sell) investment = oldAccumulatedSymbol.investment.minus( quantity.mul(unitPrice) ); } } + // Reset to zero if quantity is (almost) zero to avoid rounding issues + if (newQuantity.abs().lt(Number.EPSILON)) { + investment = new Big(0); + } + currentTransactionPointItem = { currency, dataSource, From f2c0f94a65d69efd629ca491223e5e896b1492dd Mon Sep 17 00:00:00 2001 From: KenTandrian Date: Thu, 18 Sep 2025 22:48:14 +0700 Subject: [PATCH 5/5] feat(docs): update changelog --- CHANGELOG.md | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index bbacdbf0f..e3c716e43 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -11,6 +11,10 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 - Refreshed the cryptocurrencies list +### Fixed + +- Fixed an issue in the investment calculation when selling all units of a holding + ## 2.200.0 - 2025-09-17 ### Changed @@ -67,10 +71,6 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 - Fixed the holdings table on the public page -### Fixed - -- Fixed an issue in the investment calculation when selling all units of a holding - ## 2.197.0 - 2025-09-07 ### Added