From c5198c2678d06f9c3f77fda9ce244a604ca00a6d Mon Sep 17 00:00:00 2001 From: Thomas Kaul <4159106+dtslvr@users.noreply.github.com> Date: Fri, 2 Oct 2026 21:06:26 +0200 Subject: [PATCH] Carry forward of market prices between chart dates --- .../app/portfolio/calculator/portfolio-calculator.ts | 9 ++------- ...l-buy-with-market-price-between-chart-dates.spec.ts | 10 ++++++---- .../api/src/app/portfolio/current-rate.service.mock.ts | 4 +++- 3 files changed, 11 insertions(+), 12 deletions(-) diff --git a/apps/api/src/app/portfolio/calculator/portfolio-calculator.ts b/apps/api/src/app/portfolio/calculator/portfolio-calculator.ts index 8dfe6bccbb..eb1e6f9fac 100644 --- a/apps/api/src/app/portfolio/calculator/portfolio-calculator.ts +++ b/apps/api/src/app/portfolio/calculator/portfolio-calculator.ts @@ -810,7 +810,7 @@ export abstract class PortfolioCalculator { while ( indexOfMarketPriceDate < marketPriceDates.length && - marketPriceDates[indexOfMarketPriceDate] < dateString + marketPriceDates[indexOfMarketPriceDate] <= dateString ) { lastMarketPrice = marketSymbolMap[marketPriceDates[indexOfMarketPriceDate]][ @@ -830,13 +830,8 @@ export abstract class PortfolioCalculator { } } - const marketPrice = marketSymbolMap[dateString]?.[assetProfileIdentifier]; - const unitPrice = - marketPrice ?? - lastMarketPrice ?? - lastActivityUnitPrice ?? - unitPriceAtEndDate; + lastMarketPrice ?? lastActivityUnitPrice ?? unitPriceAtEndDate; if (activitiesOfDate?.length > 0) { for (const activity of activitiesOfDate) { diff --git a/apps/api/src/app/portfolio/calculator/roai/portfolio-calculator-manual-buy-with-market-price-between-chart-dates.spec.ts b/apps/api/src/app/portfolio/calculator/roai/portfolio-calculator-manual-buy-with-market-price-between-chart-dates.spec.ts index e1fafe3810..989409e77b 100644 --- a/apps/api/src/app/portfolio/calculator/roai/portfolio-calculator-manual-buy-with-market-price-between-chart-dates.spec.ts +++ b/apps/api/src/app/portfolio/calculator/roai/portfolio-calculator-manual-buy-with-market-price-between-chart-dates.spec.ts @@ -120,13 +120,14 @@ describe('PortfolioCalculator', () => { ); /** - * The only historical market price is on 2023-06-15, which is not a - * chart date (every third day from 2021-01-03) + * The only historical market prices are on 2023-06-14 and 2023-06-15, + * which are not chart dates (every third day from 2021-01-03) */ + expect(historicalDataByDate['2023-06-14']).toBeUndefined(); expect(historicalDataByDate['2023-06-15']).toBeUndefined(); /** - * The chart dates before the market price use the unit price of the + * The chart dates before the market prices use the unit price of the * activity: 50 */ expect(historicalDataByDate['2023-06-13']).toMatchObject({ @@ -136,7 +137,8 @@ describe('PortfolioCalculator', () => { }); /** - * The chart dates after the market price carry it forward: 100 + * The chart dates after the market prices carry the latest one forward + * (100), not the first one (80) */ expect(historicalDataByDate['2023-06-16']).toMatchObject({ netPerformance: 500, // 10 * (100 - 50) = 500 diff --git a/apps/api/src/app/portfolio/current-rate.service.mock.ts b/apps/api/src/app/portfolio/current-rate.service.mock.ts index 869e0de974..18739ddab5 100644 --- a/apps/api/src/app/portfolio/current-rate.service.mock.ts +++ b/apps/api/src/app/portfolio/current-rate.service.mock.ts @@ -33,7 +33,9 @@ function mockGetValue({ case '6c0c5cee-0208-4975-b473-03baf2518497': // Sparse market data with the quote of today, which falls back to the // latest market price - if ( + if (isSameDay(parseDate('2023-06-14'), date)) { + return { marketPrice: 80 }; + } else if ( isSameDay(parseDate('2023-06-15'), date) || isSameDay(parseDate('2024-01-31'), date) ) {