From 90c4779ff3b56a9f9b67f75925eb92384667e41e Mon Sep 17 00:00:00 2001 From: Thomas Kaul <4159106+dtslvr@users.noreply.github.com> Date: Thu, 1 Feb 2024 22:08:57 +0100 Subject: [PATCH] Refactoring --- ...folio-calculator-novn-buy-and-sell.spec.ts | 4 ++-- .../src/app/portfolio/portfolio-calculator.ts | 19 +++++++++++++------ .../historical-data-item.interface.ts | 1 + 3 files changed, 16 insertions(+), 8 deletions(-) diff --git a/apps/api/src/app/portfolio/portfolio-calculator-novn-buy-and-sell.spec.ts b/apps/api/src/app/portfolio/portfolio-calculator-novn-buy-and-sell.spec.ts index dd1b31f2e..03ececc10 100644 --- a/apps/api/src/app/portfolio/portfolio-calculator-novn-buy-and-sell.spec.ts +++ b/apps/api/src/app/portfolio/portfolio-calculator-novn-buy-and-sell.spec.ts @@ -85,7 +85,7 @@ describe('PortfolioCalculator', () => { expect(chartData[0]).toEqual({ date: '2022-03-07', - investment: 151.6, + investmentValueWithCurrencyEffect: 151.6, netPerformance: 0, netPerformanceInPercentage: 0, netPerformanceInPercentageWithCurrencyEffect: 0, @@ -98,7 +98,7 @@ describe('PortfolioCalculator', () => { expect(chartData[chartData.length - 1]).toEqual({ date: '2022-04-11', - investment: 0, + investmentValueWithCurrencyEffect: 0, netPerformance: 19.86, netPerformanceInPercentage: 13.100263852242744, netPerformanceInPercentageWithCurrencyEffect: 13.100263852242744, diff --git a/apps/api/src/app/portfolio/portfolio-calculator.ts b/apps/api/src/app/portfolio/portfolio-calculator.ts index d7acfdd47..2ad733221 100644 --- a/apps/api/src/app/portfolio/portfolio-calculator.ts +++ b/apps/api/src/app/portfolio/portfolio-calculator.ts @@ -3,6 +3,7 @@ import { IDataGatheringItem } from '@ghostfolio/api/services/interfaces/interfac import { DATE_FORMAT, parseDate, resetHours } from '@ghostfolio/common/helper'; import { DataProviderInfo, + HistoricalDataItem, ResponseError, SymbolMetrics, TimelinePosition @@ -173,7 +174,11 @@ export class PortfolioCalculator { this.transactionPoints = transactionPoints; } - public async getChartData(start: Date, end = new Date(Date.now()), step = 1) { + public async getChartData( + start: Date, + end = new Date(Date.now()), + step = 1 + ): Promise { const symbols: { [symbol: string]: boolean } = {}; const transactionPointsBeforeEndDate = @@ -242,7 +247,7 @@ export class PortfolioCalculator { const accumulatedValuesByDate: { [date: string]: { - investmentValue: Big; + investmentValueWithCurrencyEffect: Big; totalCurrentValue: Big; totalCurrentValueWithCurrencyEffect: Big; totalInvestmentValue: Big; @@ -344,8 +349,9 @@ export class PortfolioCalculator { ] ?? new Big(0); accumulatedValuesByDate[dateString] = { - investmentValue: ( - accumulatedValuesByDate[dateString]?.investmentValue ?? new Big(0) + investmentValueWithCurrencyEffect: ( + accumulatedValuesByDate[dateString] + ?.investmentValueWithCurrencyEffect ?? new Big(0) ).add(investmentValueWithCurrencyEffect), totalCurrentValue: ( accumulatedValuesByDate[dateString]?.totalCurrentValue ?? new Big(0) @@ -384,7 +390,7 @@ export class PortfolioCalculator { return Object.entries(accumulatedValuesByDate).map(([date, values]) => { const { - investmentValue, + investmentValueWithCurrencyEffect, totalCurrentValue, totalCurrentValueWithCurrencyEffect, totalInvestmentValue, @@ -414,7 +420,8 @@ export class PortfolioCalculator { date, netPerformanceInPercentage, netPerformanceInPercentageWithCurrencyEffect, - investment: investmentValue.toNumber(), + investmentValueWithCurrencyEffect: + investmentValueWithCurrencyEffect.toNumber(), netPerformance: totalNetPerformanceValue.toNumber(), netPerformanceWithCurrencyEffect: totalNetPerformanceValueWithCurrencyEffect.toNumber(), diff --git a/libs/common/src/lib/interfaces/historical-data-item.interface.ts b/libs/common/src/lib/interfaces/historical-data-item.interface.ts index 049a87fb9..0b45cf0b7 100644 --- a/libs/common/src/lib/interfaces/historical-data-item.interface.ts +++ b/libs/common/src/lib/interfaces/historical-data-item.interface.ts @@ -2,6 +2,7 @@ export interface HistoricalDataItem { averagePrice?: number; date: string; grossPerformancePercent?: number; + investmentValueWithCurrencyEffect?: number; marketPrice?: number; netPerformance?: number; netPerformanceInPercentage?: number;