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) );