diff --git a/CHANGELOG.md b/CHANGELOG.md index 4fedaa3a3..54bf28b49 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -5,6 +5,12 @@ All notable changes to this project will be documented in this file. The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/), and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html). +## Unreleased + +### Fixed + +- Fixed the misleading `netPerformancePercentWithCurrencyEffect` for reopened multi-trade positions by calculating it against the net invested capital + ## 3.22.0 - 2026-07-08 ### Added diff --git a/apps/api/src/app/portfolio/calculator/roai/portfolio-calculator-multi-trade-currency-effect.spec.ts b/apps/api/src/app/portfolio/calculator/roai/portfolio-calculator-multi-trade-currency-effect.spec.ts new file mode 100644 index 000000000..e0b438b9a --- /dev/null +++ b/apps/api/src/app/portfolio/calculator/roai/portfolio-calculator-multi-trade-currency-effect.spec.ts @@ -0,0 +1,222 @@ +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, resetHours } from '@ghostfolio/common/helper'; +import { Activity } from '@ghostfolio/common/interfaces'; +import { PerformanceCalculationType } from '@ghostfolio/common/types/performance-calculation-type.type'; + +import { Big } from 'big.js'; +import { addDays, format, isBefore } from 'date-fns'; + +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 { + PortfolioSnapshotService: jest.fn().mockImplementation(() => { + return PortfolioSnapshotServiceMock; + }) + }; + } +); + +jest.mock('@ghostfolio/api/app/redis-cache/redis-cache.service', () => { + return { + 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, null); + + redisCacheService = new RedisCacheService(null, null); + + portfolioCalculatorFactory = new PortfolioCalculatorFactory( + configurationService, + currentRateService, + exchangeRateDataService, + portfolioSnapshotService, + redisCacheService + ); + }); + + describe('get current positions', () => { + it('calculates currency-effect return against net invested capital after round-trip trades', async () => { + jest.useFakeTimers().setSystemTime(parseDate('2024-03-02').getTime()); + + const exchangeRates: { [date: string]: number } = {}; + const marketPrices: { [date: string]: number } = {}; + + for ( + let date = resetHours(parseDate('2023-12-31')); + isBefore(date, parseDate('2024-03-03')); + date = addDays(date, 1) + ) { + const dateString = format(date, 'yyyy-MM-dd'); + + exchangeRates[dateString] = isBefore(date, parseDate('2024-02-01')) + ? 4 + : isBefore(date, parseDate('2024-03-01')) + ? 4.1 + : 3.9; + + marketPrices[dateString] = isBefore(date, parseDate('2024-02-01')) + ? 100 + : isBefore(date, parseDate('2024-03-01')) + ? 120 + : 500; + } + + jest + .spyOn(exchangeRateDataService, 'getExchangeRatesByCurrency') + .mockResolvedValue({ + MYRMYR: Object.fromEntries( + Object.keys(exchangeRates).map((date) => [date, 1]) + ), + USDMYR: exchangeRates + }); + + jest + .spyOn(currentRateService, 'getValues') + .mockImplementation(async ({ dataGatheringItems, dateQuery }) => { + const values = []; + + for ( + let date = resetHours(dateQuery.gte); + isBefore(date, dateQuery.lt); + date = addDays(date, 1) + ) { + const dateString = format(date, 'yyyy-MM-dd'); + + for (const dataGatheringItem of dataGatheringItems) { + values.push({ + date, + dataSource: dataGatheringItem.dataSource, + marketPrice: marketPrices[dateString], + symbol: dataGatheringItem.symbol + }); + } + } + + return { values, dataProviderInfos: [], errors: [] }; + }); + + const activities: Activity[] = [ + { + ...activityDummyData, + date: parseDate('2024-01-01'), + feeInAssetProfileCurrency: 0, + feeInBaseCurrency: 0, + quantity: 10, + SymbolProfile: { + ...symbolProfileDummyData, + currency: 'USD', + dataSource: 'YAHOO', + name: 'Micron Technology Inc.', + symbol: 'MU' + }, + type: 'BUY', + unitPriceInAssetProfileCurrency: 100 + }, + { + ...activityDummyData, + date: parseDate('2024-02-01'), + feeInAssetProfileCurrency: 0, + feeInBaseCurrency: 0, + quantity: 10, + SymbolProfile: { + ...symbolProfileDummyData, + currency: 'USD', + dataSource: 'YAHOO', + name: 'Micron Technology Inc.', + symbol: 'MU' + }, + type: 'SELL', + unitPriceInAssetProfileCurrency: 120 + }, + { + ...activityDummyData, + date: parseDate('2024-03-01'), + feeInAssetProfileCurrency: 0, + feeInBaseCurrency: 0, + quantity: 3, + SymbolProfile: { + ...symbolProfileDummyData, + currency: 'USD', + dataSource: 'YAHOO', + name: 'Micron Technology Inc.', + symbol: 'MU' + }, + type: 'BUY', + unitPriceInAssetProfileCurrency: 500 + } + ]; + + const portfolioCalculator = portfolioCalculatorFactory.createCalculator({ + activities, + calculationType: PerformanceCalculationType.ROAI, + currency: 'MYR', + userId: userDummyData.id + }); + + const portfolioSnapshot = await portfolioCalculator.computeSnapshot(); + + const position = portfolioSnapshot.positions.find(({ symbol }) => { + return symbol === 'MU'; + }); + + expect(position).toMatchObject({ + grossPerformanceWithCurrencyEffect: new Big(920), + investmentWithCurrencyEffect: new Big(5850), + netPerformanceWithCurrencyEffectMap: { + max: new Big(920) + }, + quantity: new Big(3), + valueInBaseCurrency: new Big(5850) + }); + + expect( + position.netPerformancePercentageWithCurrencyEffectMap.max + ).toEqual(new Big(920).div(5850)); + }); + }); +}); diff --git a/apps/api/src/app/portfolio/calculator/roai/portfolio-calculator.ts b/apps/api/src/app/portfolio/calculator/roai/portfolio-calculator.ts index d5efc4bf2..75eacc1e5 100644 --- a/apps/api/src/app/portfolio/calculator/roai/portfolio-calculator.ts +++ b/apps/api/src/app/portfolio/calculator/roai/portfolio-calculator.ts @@ -155,6 +155,7 @@ export class RoaiPortfolioCalculator extends PortfolioCalculator { let grossPerformanceAtStartDateWithCurrencyEffect = new Big(0); let grossPerformanceFromSells = new Big(0); let grossPerformanceFromSellsWithCurrencyEffect = new Big(0); + let hasClosedPosition = false; let initialValue: Big; let initialValueWithCurrencyEffect: Big; let investmentAtStartDate: Big; @@ -625,6 +626,10 @@ export class RoaiPortfolioCalculator extends PortfolioCalculator { ); if (totalUnits.eq(0)) { + if (order.type === 'SELL') { + hasClosedPosition = true; + } + // Reset tracking variables when position is fully closed totalInvestmentFromBuyTransactions = new Big(0); totalInvestmentFromBuyTransactionsWithCurrencyEffect = new Big(0); @@ -922,9 +927,24 @@ export class RoaiPortfolioCalculator extends PortfolioCalculator { new Big(0)) ) ?? new Big(0); - netPerformancePercentageWithCurrencyEffectMap[dateRange] = average.gt(0) - ? netPerformanceWithCurrencyEffectMap[dateRange].div(average) - : new Big(0); + // For a position that was fully closed and reopened, the accumulated + // investment average spans lots that are no longer held and no longer + // reflects the net invested capital. Use the current net investment as + // the base instead to avoid a misleading percentage. + if ( + dateRange === 'max' && + hasClosedPosition && + totalInvestmentWithCurrencyEffect.gt(0) + ) { + netPerformancePercentageWithCurrencyEffectMap[dateRange] = + netPerformanceWithCurrencyEffectMap[dateRange].div( + totalInvestmentWithCurrencyEffect + ); + } else { + netPerformancePercentageWithCurrencyEffectMap[dateRange] = average.gt(0) + ? netPerformanceWithCurrencyEffectMap[dateRange].div(average) + : new Big(0); + } } if (PortfolioCalculator.ENABLE_LOGGING) {