diff --git a/CHANGELOG.md b/CHANGELOG.md index c379a3fd9..c0f356bd4 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -9,6 +9,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 ### Changed +- Refactored the calculation of the chart - Upgraded `ng-extract-i18n-merge` from version `2.5.0` to `2.6.0` ## 1.247.0 - 2023-03-23 diff --git a/apps/api/src/app/portfolio/portfolio-calculator.ts b/apps/api/src/app/portfolio/portfolio-calculator.ts index e11f1ed14..57835298c 100644 --- a/apps/api/src/app/portfolio/portfolio-calculator.ts +++ b/apps/api/src/app/portfolio/portfolio-calculator.ts @@ -182,10 +182,10 @@ export class PortfolioCalculator { return isBefore(parseDate(transactionPoint.date), end); }) ?? []; - const firstIndex = transactionPointsBeforeEndDate.length; + const currencies: { [symbol: string]: string } = {}; const dates: Date[] = []; const dataGatheringItems: IDataGatheringItem[] = []; - const currencies: { [symbol: string]: string } = {}; + const firstIndex = transactionPointsBeforeEndDate.length; let day = start; @@ -235,27 +235,24 @@ export class PortfolioCalculator { } } - const currentValuesBySymbol: { - [symbol: string]: { [date: string]: Big }; - } = {}; - - const investmentValuesBySymbol: { - [symbol: string]: { [date: string]: Big }; - } = {}; - - const maxInvestmentValuesBySymbol: { - [symbol: string]: { [date: string]: Big }; + const valuesByDate: { + [date: string]: { + maxTotalInvestmentValue: Big; + totalCurrentValue: Big; + totalInvestmentValue: Big; + totalNetPerformanceValue: Big; + }; } = {}; - const netPerformanceValuesBySymbol: { - [symbol: string]: { [date: string]: Big }; + const valuesBySymbol: { + [symbol: string]: { + currentValues: { [date: string]: Big }; + investmentValues: { [date: string]: Big }; + maxInvestmentValues: { [date: string]: Big }; + netPerformanceValues: { [date: string]: Big }; + }; } = {}; - const totalCurrentValues: { [date: string]: Big } = {}; - const totalNetPerformanceValues: { [date: string]: Big } = {}; - const totalInvestmentValues: { [date: string]: Big } = {}; - const maxTotalInvestmentValues: { [date: string]: Big } = {}; - for (const symbol of Object.keys(symbols)) { const { currentValues, @@ -271,68 +268,67 @@ export class PortfolioCalculator { isChartMode: true }); - currentValuesBySymbol[symbol] = currentValues; - netPerformanceValuesBySymbol[symbol] = netPerformanceValues; - investmentValuesBySymbol[symbol] = investmentValues; - maxInvestmentValuesBySymbol[symbol] = maxInvestmentValues; + valuesBySymbol[symbol] = { + currentValues, + investmentValues, + maxInvestmentValues, + netPerformanceValues + }; } for (const currentDate of dates) { const dateString = format(currentDate, DATE_FORMAT); - for (const symbol of Object.keys(netPerformanceValuesBySymbol)) { - totalCurrentValues[dateString] = - totalCurrentValues[dateString] ?? new Big(0); - - if (currentValuesBySymbol[symbol]?.[dateString]) { - totalCurrentValues[dateString] = totalCurrentValues[dateString].add( - currentValuesBySymbol[symbol][dateString] - ); - } - - totalNetPerformanceValues[dateString] = - totalNetPerformanceValues[dateString] ?? new Big(0); - - if (netPerformanceValuesBySymbol[symbol]?.[dateString]) { - totalNetPerformanceValues[dateString] = totalNetPerformanceValues[ - dateString - ].add(netPerformanceValuesBySymbol[symbol][dateString]); - } - - totalInvestmentValues[dateString] = - totalInvestmentValues[dateString] ?? new Big(0); - - if (investmentValuesBySymbol[symbol]?.[dateString]) { - totalInvestmentValues[dateString] = totalInvestmentValues[ - dateString - ].add(investmentValuesBySymbol[symbol][dateString]); - } - - maxTotalInvestmentValues[dateString] = - maxTotalInvestmentValues[dateString] ?? new Big(0); - - if (maxInvestmentValuesBySymbol[symbol]?.[dateString]) { - maxTotalInvestmentValues[dateString] = maxTotalInvestmentValues[ - dateString - ].add(maxInvestmentValuesBySymbol[symbol][dateString]); - } + for (const symbol of Object.keys(valuesBySymbol)) { + const symbolValues = valuesBySymbol[symbol]; + + const currentValue = + symbolValues.currentValues?.[dateString] ?? new Big(0); + const investmentValue = + symbolValues.investmentValues?.[dateString] ?? new Big(0); + const maxInvestmentValue = + symbolValues.maxInvestmentValues?.[dateString] ?? new Big(0); + const netPerformanceValue = + symbolValues.netPerformanceValues?.[dateString] ?? new Big(0); + + valuesByDate[dateString] = { + totalCurrentValue: ( + valuesByDate[dateString]?.totalCurrentValue ?? new Big(0) + ).add(currentValue), + totalInvestmentValue: ( + valuesByDate[dateString]?.totalInvestmentValue ?? new Big(0) + ).add(investmentValue), + maxTotalInvestmentValue: ( + valuesByDate[dateString]?.maxTotalInvestmentValue ?? new Big(0) + ).add(maxInvestmentValue), + totalNetPerformanceValue: ( + valuesByDate[dateString]?.totalNetPerformanceValue ?? new Big(0) + ).add(netPerformanceValue) + }; } } - return Object.keys(totalNetPerformanceValues).map((date) => { - const netPerformanceInPercentage = maxTotalInvestmentValues[date].eq(0) + return Object.entries(valuesByDate).map(([date, values]) => { + const { + maxTotalInvestmentValue, + totalCurrentValue, + totalInvestmentValue, + totalNetPerformanceValue + } = values; + + const netPerformanceInPercentage = maxTotalInvestmentValue.eq(0) ? 0 - : totalNetPerformanceValues[date] - .div(maxTotalInvestmentValues[date]) + : totalNetPerformanceValue + .div(maxTotalInvestmentValue) .mul(100) .toNumber(); return { date, netPerformanceInPercentage, - netPerformance: totalNetPerformanceValues[date].toNumber(), - totalInvestment: totalInvestmentValues[date].toNumber(), - value: totalCurrentValues[date].toNumber() + netPerformance: totalNetPerformanceValue.toNumber(), + totalInvestment: totalInvestmentValue.toNumber(), + value: totalCurrentValue.toNumber() }; }); }