From f0c511e3e3c5c1b36e564f75dff2cbf8e63fd8e8 Mon Sep 17 00:00:00 2001 From: Reto Kaul Date: Sat, 19 Mar 2022 10:15:11 +0100 Subject: [PATCH] Fix performance calculation --- .../src/app/portfolio/portfolio-calculator-new.ts | 12 +++++++----- 1 file changed, 7 insertions(+), 5 deletions(-) diff --git a/apps/api/src/app/portfolio/portfolio-calculator-new.ts b/apps/api/src/app/portfolio/portfolio-calculator-new.ts index 7d7c2dc04..6a5659d85 100644 --- a/apps/api/src/app/portfolio/portfolio-calculator-new.ts +++ b/apps/api/src/app/portfolio/portfolio-calculator-new.ts @@ -701,6 +701,7 @@ export class PortfolioCalculatorNew { let totalInvestment = new Big(0); let totalInvestmentWithGrossPerformanceFromSell = new Big(0); let totalUnits = new Big(0); + let valueAtStartDate: Big; // Add a synthetic order at the start and the end date orders.push({ @@ -774,14 +775,15 @@ export class PortfolioCalculatorNew { averagePriceAtStartDate = totalInvestment.div(totalUnits); } - if (!investmentAtStartDate && i >= indexOfStartOrder) { - investmentAtStartDate = totalInvestment ?? new Big(0); - } - const valueOfInvestmentBeforeTransaction = totalUnits.mul( order.unitPrice ); + if (!investmentAtStartDate && i >= indexOfStartOrder) { + investmentAtStartDate = totalInvestment ?? new Big(0); + valueAtStartDate = valueOfInvestmentBeforeTransaction; + } + const transactionInvestment = order.quantity .mul(order.unitPrice) .mul(this.getFactor(order.type)); @@ -906,7 +908,7 @@ export class PortfolioCalculatorNew { .minus(grossPerformanceAtStartDate) .minus(fees.minus(feesAtStartDate)); - const maxInvestmentBetweenStartAndEndDate = initialValue.plus( + const maxInvestmentBetweenStartAndEndDate = valueAtStartDate.plus( maxTotalInvestment.minus(investmentAtStartDate) );