diff --git a/apps/api/src/app/portfolio/calculator/constantPortfolioReturn/portfolio-calculator.ts b/apps/api/src/app/portfolio/calculator/constantPortfolioReturn/portfolio-calculator.ts index 88ff11b47..587b79146 100644 --- a/apps/api/src/app/portfolio/calculator/constantPortfolioReturn/portfolio-calculator.ts +++ b/apps/api/src/app/portfolio/calculator/constantPortfolioReturn/portfolio-calculator.ts @@ -72,59 +72,38 @@ export class CPRPortfolioCalculator extends TWRPortfolioCalculator { } @LogPerformance - public async getChart({ - dateRange = 'max', - withDataDecimation = true, - withTimeWeightedReturn = false + public async getPerformanceWithTimeWeightedReturn({ + start, + end }: { - dateRange?: DateRange; - withDataDecimation?: boolean; - withTimeWeightedReturn?: boolean; - }): Promise { - const { endDate, startDate } = getIntervalFromDateRange( - dateRange, - this.getStartDate() - ); - - const daysInMarket = differenceInDays(endDate, startDate) + 1; - const step = withDataDecimation - ? Math.round( - daysInMarket / - Math.min( - daysInMarket, - this.configurationService.get('MAX_CHART_ITEMS') - ) - ) - : 1; - + start: Date; + end: Date; + }): Promise<{ chart: HistoricalDataItem[] }> { let item = await super.getPerformance({ - end: endDate, - start: startDate + end, + start }); - if (!withTimeWeightedReturn) { - return item.chart; - } else { - let itemResult = item.chart; - let dates = itemResult.map((item) => parseDate(item.date)); - let timeWeighted = await this.getTimeWeightedChartData({ - dates - }); + let itemResult = item.chart; + let dates = itemResult.map((item) => parseDate(item.date)); + let timeWeighted = await this.getTimeWeightedChartData({ + dates + }); - return itemResult.map((item) => { - let timeWeightedItem = timeWeighted.find( - (timeWeightedItem) => timeWeightedItem.date === item.date - ); - if (timeWeightedItem) { - item.timeWeightedPerformance = - timeWeightedItem.netPerformanceInPercentage; - item.timeWeightedPerformanceWithCurrencyEffect = - timeWeightedItem.netPerformanceInPercentageWithCurrencyEffect; - } + item.chart = itemResult.map((itemInt) => { + let timeWeightedItem = timeWeighted.find( + (timeWeightedItem) => timeWeightedItem.date === itemInt.date + ); + if (timeWeightedItem) { + itemInt.timeWeightedPerformance = + timeWeightedItem.netPerformanceInPercentage; + itemInt.timeWeightedPerformanceWithCurrencyEffect = + timeWeightedItem.netPerformanceInPercentageWithCurrencyEffect; + } - return item; - }); - } + return itemInt; + }); + return item; } @LogPerformance @@ -314,8 +293,14 @@ export class CPRPortfolioCalculator extends TWRPortfolioCalculator { netPerformanceInPercentageWithCurrencyEffect: Big, newTotalInvestment: Big ) { - const previousPrice = this.marketMap[previousDate][holding]; - const currentPrice = this.marketMap[date][holding] ?? previousPrice; + const previousPrice = + Object.keys(this.marketMap).indexOf(previousDate) > 0 + ? this.marketMap[previousDate][holding] + : undefined; + const priceDictionary = this.marketMap[date]; + let currentPrice = + priceDictionary !== undefined ? priceDictionary[holding] : previousPrice; + currentPrice ??= previousPrice; const previousHolding = timelineHoldings[previousDate][holding]; const priceInBaseCurrency = currentPrice diff --git a/apps/api/src/app/portfolio/calculator/portfolio-calculator.ts b/apps/api/src/app/portfolio/calculator/portfolio-calculator.ts index a3d490252..d1ee11f0a 100644 --- a/apps/api/src/app/portfolio/calculator/portfolio-calculator.ts +++ b/apps/api/src/app/portfolio/calculator/portfolio-calculator.ts @@ -674,6 +674,7 @@ export abstract class PortfolioCalculator { return this.snapshot.totalLiabilitiesWithCurrencyEffect; } + @LogPerformance public async getPerformance({ end, start }) { await this.snapshotPromise; diff --git a/apps/api/src/app/portfolio/portfolio.service.ts b/apps/api/src/app/portfolio/portfolio.service.ts index d6f8bae78..b833c3bca 100644 --- a/apps/api/src/app/portfolio/portfolio.service.ts +++ b/apps/api/src/app/portfolio/portfolio.service.ts @@ -1237,11 +1237,13 @@ export class PortfolioService { }); const { endDate, startDate } = getIntervalFromDateRange(dateRange); + const range = { end: endDate, start: startDate }; - const { chart } = await portfolioCalculator.getPerformance({ - end: endDate, - start: startDate - }); + const { chart } = await (calculateTimeWeightedPerformance + ? ( + portfolioCalculator as CPRPortfolioCalculator + ).getPerformanceWithTimeWeightedReturn(range) + : portfolioCalculator.getPerformance(range)); const { netPerformance,