From 2715880032f7681a451e5868f2462b165d857b0d Mon Sep 17 00:00:00 2001 From: Reto Kaul Date: Sat, 12 Mar 2022 10:26:43 +0100 Subject: [PATCH] Fix percentage performance issue with recent transactions --- .../app/portfolio/portfolio-calculator-new.ts | 27 ++++++++++++++----- 1 file changed, 21 insertions(+), 6 deletions(-) diff --git a/apps/api/src/app/portfolio/portfolio-calculator-new.ts b/apps/api/src/app/portfolio/portfolio-calculator-new.ts index 8df16f785..b067e3a6c 100644 --- a/apps/api/src/app/portfolio/portfolio-calculator-new.ts +++ b/apps/api/src/app/portfolio/portfolio-calculator-new.ts @@ -33,7 +33,10 @@ import { TransactionPointSymbol } from './interfaces/transaction-point-symbol.in import { TransactionPoint } from './interfaces/transaction-point.interface'; export class PortfolioCalculatorNew { - private static readonly ENABLE_LOGGING = false; + private static readonly CALCULATE_PERCENTAGE_PERFORMANCE_WITH_MAX_INVESTMENT = + true; + + private static readonly ENABLE_LOGGING = true; private currency: string; private currentRateService: CurrentRateService; @@ -767,7 +770,7 @@ export class PortfolioCalculatorNew { totalInvestment = totalInvestment.plus(transactionInvestment); - if (totalInvestment.gt(maxTotalInvestment)) { + if (i >= indexOfStartOrder && totalInvestment.gt(maxTotalInvestment)) { maxTotalInvestment = totalInvestment; } @@ -886,11 +889,17 @@ export class PortfolioCalculatorNew { .minus(fees.minus(feesAtStartDate)); const grossPerformancePercentage = + PortfolioCalculatorNew.CALCULATE_PERCENTAGE_PERFORMANCE_WITH_MAX_INVESTMENT || averagePriceAtStartDate.eq(0) || averagePriceAtEndDate.eq(0) || orders[indexOfStartOrder].unitPrice.eq(0) - ? totalGrossPerformance.div(maxTotalInvestment) - : unitPriceAtEndDate + ? maxTotalInvestment.gt(0) + ? totalGrossPerformance.div(maxTotalInvestment) + : new Big(0) + : // This formula has the issue that buying more units with a price + // lower than the average buying price results in a positive + // performance even if the market pricse stays constant + unitPriceAtEndDate .div(averagePriceAtEndDate) .div( orders[indexOfStartOrder].unitPrice.div(averagePriceAtStartDate) @@ -902,11 +911,17 @@ export class PortfolioCalculatorNew { : new Big(0); const netPerformancePercentage = + PortfolioCalculatorNew.CALCULATE_PERCENTAGE_PERFORMANCE_WITH_MAX_INVESTMENT || averagePriceAtStartDate.eq(0) || averagePriceAtEndDate.eq(0) || orders[indexOfStartOrder].unitPrice.eq(0) - ? totalNetPerformance.div(maxTotalInvestment) - : unitPriceAtEndDate + ? maxTotalInvestment.gt(0) + ? totalNetPerformance.div(maxTotalInvestment) + : new Big(0) + : // This formula has the issue that buying more units with a price + // lower than the average buying price results in a positive + // performance even if the market pricse stays constant + unitPriceAtEndDate .minus(feesPerUnit) .div(averagePriceAtEndDate) .div(