From faf7601559167932601db8b5492d386a9742a503 Mon Sep 17 00:00:00 2001 From: Reto Kaul Date: Thu, 9 May 2024 14:25:50 +0200 Subject: [PATCH] Improve calculation --- .../calculator/portfolio-calculator.ts | 31 +++++++++++++------ 1 file changed, 21 insertions(+), 10 deletions(-) diff --git a/apps/api/src/app/portfolio/calculator/portfolio-calculator.ts b/apps/api/src/app/portfolio/calculator/portfolio-calculator.ts index 047adb063..66ad2ffa1 100644 --- a/apps/api/src/app/portfolio/calculator/portfolio-calculator.ts +++ b/apps/api/src/app/portfolio/calculator/portfolio-calculator.ts @@ -1118,9 +1118,24 @@ export abstract class PortfolioCalculator { historicalDataItem.netPerformanceInPercentageWithCurrencyEffect; } - totalInvestmentValuesWithCurrencyEffect.push( - historicalDataItem.totalInvestmentValueWithCurrencyEffect - ); + const netPerformanceWithCurrencyEffectSinceStartDate = + historicalDataItem.netPerformanceWithCurrencyEffect - + netPerformanceWithCurrencyEffectAtStartDate; + + if (historicalDataItem.totalInvestmentValueWithCurrencyEffect > 0) { + totalInvestmentValuesWithCurrencyEffect.push( + historicalDataItem.totalInvestmentValueWithCurrencyEffect + ); + } + + const timeWeightedInvestmentValue = + totalInvestmentValuesWithCurrencyEffect.length > 0 + ? sum(totalInvestmentValuesWithCurrencyEffect) / + totalInvestmentValuesWithCurrencyEffect.length + : 0; + + // TODO: Not sure if this is correct + console.log(historicalDataItem.totalInvestmentValueWithCurrencyEffect); // TODO: Normalize remaining metrics newChartData.push({ @@ -1128,14 +1143,10 @@ export abstract class PortfolioCalculator { netPerformance: historicalDataItem.netPerformance - netPerformanceAtStartDate, netPerformanceWithCurrencyEffect: - historicalDataItem.netPerformanceWithCurrencyEffect - - netPerformanceWithCurrencyEffectAtStartDate, + netPerformanceWithCurrencyEffectSinceStartDate, netPerformanceInPercentageWithCurrencyEffect: - ((historicalDataItem.netPerformanceWithCurrencyEffect - - netPerformanceWithCurrencyEffectAtStartDate) / - // TODO: Not sure if this is correct - (sum(totalInvestmentValuesWithCurrencyEffect) / - totalInvestmentValuesWithCurrencyEffect.length)) * + (netPerformanceWithCurrencyEffectSinceStartDate / + timeWeightedInvestmentValue) * 100 }); }