From 5799b9e71cb1bfe52124c78a03cbfbbc94722379 Mon Sep 17 00:00:00 2001 From: Thomas <4159106+dtslvr@users.noreply.github.com> Date: Sun, 29 Jan 2023 12:32:25 +0100 Subject: [PATCH] Refactoring --- apps/api/src/app/portfolio/portfolio-calculator.ts | 8 +++----- libs/common/src/lib/helper.ts | 8 ++++++-- 2 files changed, 9 insertions(+), 7 deletions(-) diff --git a/apps/api/src/app/portfolio/portfolio-calculator.ts b/apps/api/src/app/portfolio/portfolio-calculator.ts index f6b695603..8ac2ba781 100644 --- a/apps/api/src/app/portfolio/portfolio-calculator.ts +++ b/apps/api/src/app/portfolio/portfolio-calculator.ts @@ -1269,9 +1269,7 @@ export class PortfolioCalculator { } const averageInvestmentBetweenStartAndEndDate = getAverage( - Object.values(investmentValues).map((investmentValue) => { - return investmentValue.toNumber(); - }) + Object.values(investmentValues) ); const totalGrossPerformance = grossPerformance.minus( @@ -1286,7 +1284,7 @@ export class PortfolioCalculator { averagePriceAtStartDate.eq(0) || averagePriceAtEndDate.eq(0) || orders[indexOfStartOrder].unitPrice.eq(0) - ? averageInvestmentBetweenStartAndEndDate > 0 + ? averageInvestmentBetweenStartAndEndDate.gt(0) ? totalGrossPerformance.div(averageInvestmentBetweenStartAndEndDate) : new Big(0) : // This formula has the issue that buying more units with a price @@ -1307,7 +1305,7 @@ export class PortfolioCalculator { averagePriceAtStartDate.eq(0) || averagePriceAtEndDate.eq(0) || orders[indexOfStartOrder].unitPrice.eq(0) - ? averageInvestmentBetweenStartAndEndDate > 0 + ? averageInvestmentBetweenStartAndEndDate.gt(0) ? totalNetPerformance.div(averageInvestmentBetweenStartAndEndDate) : new Big(0) : // This formula has the issue that buying more units with a price diff --git a/libs/common/src/lib/helper.ts b/libs/common/src/lib/helper.ts index 868a22e7d..150a5b0a6 100644 --- a/libs/common/src/lib/helper.ts +++ b/libs/common/src/lib/helper.ts @@ -133,8 +133,12 @@ export function getAssetProfileIdentifier({ dataSource, symbol }: UniqueAsset) { return `${dataSource}-${symbol}`; } -export function getAverage(aArray: number[]) { - return aArray.reduce((a, b) => a + b, 0) / aArray.length; +export function getAverage(aArray: Big[]) { + if (aArray?.length > 0) { + return aArray.reduce((a, b) => a.plus(b), new Big(0)).div(aArray.length); + } + + return new Big(0); } export function getBackgroundColor(aColorScheme: ColorScheme) {