From 7e9e7c99288ab0625363bead1550987c75bf4298 Mon Sep 17 00:00:00 2001 From: Reto Kaul Date: Mon, 14 Feb 2022 21:24:11 +0100 Subject: [PATCH] Improve calculation of overall performance percentage --- .../app/portfolio/portfolio-calculator-new.ts | 48 +++++++++++-------- 1 file changed, 28 insertions(+), 20 deletions(-) diff --git a/apps/api/src/app/portfolio/portfolio-calculator-new.ts b/apps/api/src/app/portfolio/portfolio-calculator-new.ts index 83bbc663f..38a436bf9 100644 --- a/apps/api/src/app/portfolio/portfolio-calculator-new.ts +++ b/apps/api/src/app/portfolio/portfolio-calculator-new.ts @@ -272,7 +272,7 @@ export class PortfolioCalculatorNew { transactionCount: item.transactionCount }); } - const overall = this.calculateOverallPerformance(positions, initialValues); + const overall = this.calculateOverallPerformance(positions); return { ...overall, @@ -393,18 +393,27 @@ export class PortfolioCalculatorNew { }; } - private calculateOverallPerformance( - positions: TimelinePosition[], - initialValues: { [p: string]: Big } - ) { - let hasErrors = false; + private calculateOverallPerformance(positions: TimelinePosition[]) { let currentValue = new Big(0); - let totalInvestment = new Big(0); let grossPerformance = new Big(0); let grossPerformancePercentage = new Big(0); + let hasErrors = false; let netPerformance = new Big(0); let netPerformancePercentage = new Big(0); - let completeInitialValue = new Big(0); + let totalGrossPerformanceFluctuation = new Big(0); + let totalInvestment = new Big(0); + + for (const currentPosition of positions) { + if ( + currentPosition.grossPerformance && + currentPosition.grossPerformancePercentage + ) { + totalGrossPerformanceFluctuation = + totalGrossPerformanceFluctuation.plus( + currentPosition.grossPerformance.abs() + ); + } + } for (const currentPosition of positions) { if (currentPosition.marketPrice) { @@ -414,11 +423,14 @@ export class PortfolioCalculatorNew { } else { hasErrors = true; } + totalInvestment = totalInvestment.plus(currentPosition.investment); + if (currentPosition.grossPerformance) { grossPerformance = grossPerformance.plus( currentPosition.grossPerformance ); + netPerformance = netPerformance.plus(currentPosition.netPerformance); } else if (!currentPosition.quantity.eq(0)) { hasErrors = true; @@ -426,15 +438,18 @@ export class PortfolioCalculatorNew { if ( currentPosition.grossPerformancePercentage && - initialValues[currentPosition.symbol] + !totalGrossPerformanceFluctuation.eq(0) ) { - const currentInitialValue = initialValues[currentPosition.symbol]; - completeInitialValue = completeInitialValue.plus(currentInitialValue); + const ratioOfCurrentPosition = currentPosition.grossPerformance + .abs() + .div(totalGrossPerformanceFluctuation); + grossPerformancePercentage = grossPerformancePercentage.plus( - currentPosition.grossPerformancePercentage.mul(currentInitialValue) + currentPosition.grossPerformancePercentage.mul(ratioOfCurrentPosition) ); + netPerformancePercentage = netPerformancePercentage.plus( - currentPosition.netPerformancePercentage.mul(currentInitialValue) + currentPosition.netPerformancePercentage.mul(ratioOfCurrentPosition) ); } else if (!currentPosition.quantity.eq(0)) { Logger.warn( @@ -444,13 +459,6 @@ export class PortfolioCalculatorNew { } } - if (!completeInitialValue.eq(0)) { - grossPerformancePercentage = - grossPerformancePercentage.div(completeInitialValue); - netPerformancePercentage = - netPerformancePercentage.div(completeInitialValue); - } - return { currentValue, grossPerformance,