From efd3fea6f2b0f4ade0b34274b66d17fa7917cf44 Mon Sep 17 00:00:00 2001 From: Dan Date: Sat, 3 May 2025 15:59:20 +0200 Subject: [PATCH] Smaller fixes for New Calculator --- ...rtfolio-calculator-symbolmetrics-helper.ts | 54 ++++++++++++++----- .../src/app/portfolio/portfolio.service.ts | 22 ++++---- 2 files changed, 52 insertions(+), 24 deletions(-) diff --git a/apps/api/src/app/portfolio/calculator/roi/portfolio-calculator-symbolmetrics-helper.ts b/apps/api/src/app/portfolio/calculator/roi/portfolio-calculator-symbolmetrics-helper.ts index 8d02dacc4..848ce68a0 100644 --- a/apps/api/src/app/portfolio/calculator/roi/portfolio-calculator-symbolmetrics-helper.ts +++ b/apps/api/src/app/portfolio/calculator/roi/portfolio-calculator-symbolmetrics-helper.ts @@ -59,20 +59,11 @@ export class RoiPortfolioCalculatorSymbolMetricsHelper { new Big(0)) ) ?? new Big(0); - let investmentBasis = - symbolMetricsHelper.symbolMetrics.currentValuesWithCurrencyEffect[ - rangeStartDateString - ]; - - if ( - !symbolMetricsHelper.symbolMetrics.currentValuesWithCurrencyEffect[ - rangeStartDateString - ]?.gt(0) - ) { - investmentBasis = - symbolMetricsHelper.symbolMetrics - .timeWeightedInvestmentValuesWithCurrencyEffect[rangeEndDateString]; - } + const investmentBasis = this.calculateInvestmentBasis( + symbolMetricsHelper, + rangeStartDateString, + rangeEndDateString + ); symbolMetricsHelper.symbolMetrics.netPerformancePercentageWithCurrencyEffectMap[ dateRange @@ -865,4 +856,39 @@ export class RoiPortfolioCalculatorSymbolMetricsHelper { symbolMetricsHelper.exchangeRateAtOrderDate ); } + + private calculateInvestmentBasis( + symbolMetricsHelper: PortfolioCalculatorSymbolMetricsHelperObject, + rangeStartDateString: string, + rangeEndDateString: string + ) { + let investmentBasis = this.getValueOrZero( + symbolMetricsHelper.symbolMetrics.currentValuesWithCurrencyEffect[ + rangeStartDateString + ] + ).plus( + this.getValueOrZero( + symbolMetricsHelper.symbolMetrics + .timeWeightedInvestmentValuesWithCurrencyEffect[rangeEndDateString] + )?.minus( + this.getValueOrZero( + symbolMetricsHelper.symbolMetrics + .timeWeightedInvestmentValuesWithCurrencyEffect[ + rangeStartDateString + ] + ) + ) + ); + + if (!investmentBasis.gt(0)) { + investmentBasis = + symbolMetricsHelper.symbolMetrics + .timeWeightedInvestmentValuesWithCurrencyEffect[rangeEndDateString]; + } + return investmentBasis; + } + + private getValueOrZero(value: Big | undefined) { + return value ?? new Big(0); + } } diff --git a/apps/api/src/app/portfolio/portfolio.service.ts b/apps/api/src/app/portfolio/portfolio.service.ts index d93767ab2..3cfa7ae83 100644 --- a/apps/api/src/app/portfolio/portfolio.service.ts +++ b/apps/api/src/app/portfolio/portfolio.service.ts @@ -248,16 +248,18 @@ export class PortfolioService { activities: Activity[]; groupBy?: GroupBy; }): Promise { - let dividends = activities.map(({ currency, date, value }) => { - return { - date: format(date, DATE_FORMAT), - investment: this.exchangeRateDataService.toCurrency( - value, - currency, - this.getUserCurrency() - ) - }; - }); + let dividends = activities.map( + ({ currency, date, value, SymbolProfile }) => { + return { + date: format(date, DATE_FORMAT), + investment: this.exchangeRateDataService.toCurrency( + value, + currency ?? SymbolProfile.currency, + this.getUserCurrency() + ) + }; + } + ); if (groupBy) { dividends = this.getDividendsByGroup({ dividends, groupBy });