From 81dc7044f94ee47332d7496c4b7cad88656e93bb Mon Sep 17 00:00:00 2001 From: KenTandrian Date: Sun, 4 May 2025 13:29:09 +0700 Subject: [PATCH] feat(test): extend test cases --- .../portfolio-calculator-baln-buy.spec.ts | 88 +++++++++++++++++++ 1 file changed, 88 insertions(+) diff --git a/apps/api/src/app/portfolio/calculator/roai/portfolio-calculator-baln-buy.spec.ts b/apps/api/src/app/portfolio/calculator/roai/portfolio-calculator-baln-buy.spec.ts index b21192db1..1f8434bd0 100644 --- a/apps/api/src/app/portfolio/calculator/roai/portfolio-calculator-baln-buy.spec.ts +++ b/apps/api/src/app/portfolio/calculator/roai/portfolio-calculator-baln-buy.spec.ts @@ -193,5 +193,93 @@ describe('PortfolioCalculator', () => { { date: '2021-12-01', investment: 0 } ]); }); + + it.only('with BALN.SW buy lower than closing price, calculation after buy date', async () => { + jest.useFakeTimers().setSystemTime(parseDate('2021-12-18').getTime()); + + const activities: Activity[] = [ + { + ...activityDummyData, + date: new Date('2021-11-30'), + feeInAssetProfileCurrency: 1.55, + quantity: 2, + SymbolProfile: { + ...symbolProfileDummyData, + currency: 'CHF', + dataSource: 'YAHOO', + name: 'Bâloise Holding AG', + symbol: 'BALN.SW' + }, + type: 'BUY', + unitPriceInAssetProfileCurrency: 135.0 + } + ]; + + const portfolioCalculator = portfolioCalculatorFactory.createCalculator({ + activities, + calculationType: PerformanceCalculationType.ROAI, + currency: 'CHF', + userId: userDummyData.id + }); + + const portfolioSnapshot = await portfolioCalculator.computeSnapshot(); + const snapshotOnTheDate = portfolioSnapshot.historicalData.find( + (snapshot) => snapshot.date === '2021-11-30' + ); + + /** + * Closing price on 2021-11-30: 136.6 + * Correct calculation: 2 * (136.6 - 135.0) - 1.55 = 1.65 + * Current calculation: 2 * (135.0 - 135.0) - 1.55 = -1.55 + * + * The performance should be calculated based on the closing price of the day, + * not the buy price. + */ + expect(snapshotOnTheDate?.netPerformanceWithCurrencyEffect).toEqual(1.65); + }); + + it.only('with BALN.SW buy lower than closing price, calculation on buy date', async () => { + jest.useFakeTimers().setSystemTime(parseDate('2021-11-30').getTime()); + + const activities: Activity[] = [ + { + ...activityDummyData, + date: new Date('2021-11-30'), + feeInAssetProfileCurrency: 1.55, + quantity: 2, + SymbolProfile: { + ...symbolProfileDummyData, + currency: 'CHF', + dataSource: 'YAHOO', + name: 'Bâloise Holding AG', + symbol: 'BALN.SW' + }, + type: 'BUY', + unitPriceInAssetProfileCurrency: 135.0 + } + ]; + + const portfolioCalculator = portfolioCalculatorFactory.createCalculator({ + activities, + calculationType: PerformanceCalculationType.ROAI, + currency: 'CHF', + userId: userDummyData.id + }); + + const portfolioSnapshot = await portfolioCalculator.computeSnapshot(); + const snapshotOnTheDate = portfolioSnapshot.historicalData.find( + (snapshot) => snapshot.date === '2021-11-30' + ); + + /** + * Closing price on 2021-11-30: 136.6 + * Correct calculation: 2 * (136.6 - 135.0) - 1.55 = 1.65 + * Current calculation: 2 * (135.0 - 135.0) - 1.55 = -1.55 + * + * The performance should be calculated based on the closing price of the day, + * not the buy price. + */ + expect(snapshotOnTheDate?.netPerformanceWithCurrencyEffect).toEqual(1.65); + }); }); });