From b9e580a82e8bfbe56cd18ada60c5c03e5118a2d4 Mon Sep 17 00:00:00 2001 From: Reto Kaul Date: Thu, 17 Feb 2022 13:25:32 +0100 Subject: [PATCH] Improvements --- .../app/portfolio/portfolio-calculator-new.ts | 58 +++++++++---------- 1 file changed, 28 insertions(+), 30 deletions(-) diff --git a/apps/api/src/app/portfolio/portfolio-calculator-new.ts b/apps/api/src/app/portfolio/portfolio-calculator-new.ts index 38a436bf9..ebc17e18a 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); + const overall = this.calculateOverallPerformance(positions, initialValues); return { ...overall, @@ -393,28 +393,19 @@ export class PortfolioCalculatorNew { }; } - private calculateOverallPerformance(positions: TimelinePosition[]) { + private calculateOverallPerformance( + positions: TimelinePosition[], + initialValues: { [symbol: string]: Big } + ) { let currentValue = 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 totalGrossPerformanceFluctuation = new Big(0); + let sumOfWeights = 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) { currentValue = currentValue.plus( @@ -436,20 +427,21 @@ export class PortfolioCalculatorNew { hasErrors = true; } - if ( - currentPosition.grossPerformancePercentage && - !totalGrossPerformanceFluctuation.eq(0) - ) { - const ratioOfCurrentPosition = currentPosition.grossPerformance - .abs() - .div(totalGrossPerformanceFluctuation); + if (currentPosition.grossPerformancePercentage) { + // Use the average from the initial value and the current investment as + // a weight + const weight = (initialValues[currentPosition.symbol] ?? new Big(0)) + .plus(currentPosition.investment) + .div(2); + + sumOfWeights = sumOfWeights.plus(weight); grossPerformancePercentage = grossPerformancePercentage.plus( - currentPosition.grossPerformancePercentage.mul(ratioOfCurrentPosition) + currentPosition.grossPerformancePercentage.mul(weight) ); netPerformancePercentage = netPerformancePercentage.plus( - currentPosition.netPerformancePercentage.mul(ratioOfCurrentPosition) + currentPosition.netPerformancePercentage.mul(weight) ); } else if (!currentPosition.quantity.eq(0)) { Logger.warn( @@ -459,6 +451,9 @@ export class PortfolioCalculatorNew { } } + grossPerformancePercentage = grossPerformancePercentage.div(sumOfWeights); + netPerformancePercentage = netPerformancePercentage.div(sumOfWeights); + return { currentValue, grossPerformance, @@ -743,12 +738,15 @@ export class PortfolioCalculatorNew { .mul(order.unitPrice) .mul(this.getFactor(order.type)); - if ( - !initialValue && - order.itemType !== 'start' && - order.itemType !== 'end' - ) { - initialValue = transactionInvestment; + if (i >= indexOfStartOrder && !initialValue) { + if ( + i === indexOfStartOrder && + !valueOfInvestmentBeforeTransaction.eq(0) + ) { + initialValue = valueOfInvestmentBeforeTransaction; + } else if (transactionInvestment.gt(0)) { + initialValue = transactionInvestment; + } } fees = fees.plus(order.fee);