From dc999cc098c37f68e7aa09a1a46e1bffba7042c0 Mon Sep 17 00:00:00 2001 From: Thomas Kaul <4159106+dtslvr@users.noreply.github.com> Date: Thu, 11 Jan 2024 20:45:44 +0100 Subject: [PATCH] Refactoring --- .../src/app/portfolio/portfolio-calculator.ts | 54 ++++++++++--------- .../src/app/portfolio/portfolio.service.ts | 8 +-- .../exchange-rate-data.service.ts | 6 +-- .../historical-data-item.interface.ts | 4 +- 4 files changed, 37 insertions(+), 35 deletions(-) diff --git a/apps/api/src/app/portfolio/portfolio-calculator.ts b/apps/api/src/app/portfolio/portfolio-calculator.ts index 5d32a9c00..8ed6d2770 100644 --- a/apps/api/src/app/portfolio/portfolio-calculator.ts +++ b/apps/api/src/app/portfolio/portfolio-calculator.ts @@ -1,6 +1,7 @@ import { TimelineInfoInterface } from '@ghostfolio/api/app/portfolio/interfaces/timeline-info.interface'; import { ExchangeRateDataService } from '@ghostfolio/api/services/exchange-rate-data/exchange-rate-data.service'; import { IDataGatheringItem } from '@ghostfolio/api/services/interfaces/interfaces'; +import { DEFAULT_CURRENCY } from '@ghostfolio/common/config'; import { DATE_FORMAT, parseDate, resetHours } from '@ghostfolio/common/helper'; import { DataProviderInfo, @@ -53,7 +54,7 @@ import { TransactionPointSymbol } from './interfaces/transaction-point-symbol.in import { TransactionPoint } from './interfaces/transaction-point.interface'; export class PortfolioCalculator { - private static ENABLE_LOGGING = false; + private static readonly ENABLE_LOGGING = false; private currency: string; private currentRateService: CurrentRateService; @@ -364,14 +365,6 @@ export class PortfolioCalculator { accumulatedValuesByDate[dateString] ?.totalInvestmentValueWithCurrencyEffect ?? new Big(0) ).add(investmentValueWithCurrencyEffect), - totalTimeWeightedInvestmentValue: ( - accumulatedValuesByDate[dateString] - ?.totalTimeWeightedInvestmentValue ?? new Big(0) - ).add(timeWeightedInvestmentValue), - totalTimeWeightedInvestmentValueWithCurrencyEffect: ( - accumulatedValuesByDate[dateString] - ?.totalTimeWeightedInvestmentValueWithCurrencyEffect ?? new Big(0) - ).add(timeWeightedInvestmentValueWithCurrencyEffect), totalNetPerformanceValue: ( accumulatedValuesByDate[dateString]?.totalNetPerformanceValue ?? new Big(0) @@ -379,7 +372,15 @@ export class PortfolioCalculator { totalNetPerformanceValueWithCurrencyEffect: ( accumulatedValuesByDate[dateString] ?.totalNetPerformanceValueWithCurrencyEffect ?? new Big(0) - ).add(netPerformanceValueWithCurrencyEffect) + ).add(netPerformanceValueWithCurrencyEffect), + totalTimeWeightedInvestmentValue: ( + accumulatedValuesByDate[dateString] + ?.totalTimeWeightedInvestmentValue ?? new Big(0) + ).add(timeWeightedInvestmentValue), + totalTimeWeightedInvestmentValueWithCurrencyEffect: ( + accumulatedValuesByDate[dateString] + ?.totalTimeWeightedInvestmentValueWithCurrencyEffect ?? new Big(0) + ).add(timeWeightedInvestmentValueWithCurrencyEffect) }; } } @@ -440,14 +441,14 @@ export class PortfolioCalculator { return { currentValue: new Big(0), grossPerformance: new Big(0), - grossPerformanceWithCurrencyEffect: new Big(0), grossPerformancePercentage: new Big(0), grossPerformancePercentageWithCurrencyEffect: new Big(0), + grossPerformanceWithCurrencyEffect: new Big(0), hasErrors: false, netPerformance: new Big(0), - netPerformanceWithCurrencyEffect: new Big(0), netPerformancePercentage: new Big(0), netPerformancePercentageWithCurrencyEffect: new Big(0), + netPerformanceWithCurrencyEffect: new Big(0), positions: [], totalInvestment: new Big(0) }; @@ -565,14 +566,14 @@ export class PortfolioCalculator { const { grossPerformance, - grossPerformanceWithCurrencyEffect, grossPerformancePercentage, grossPerformancePercentageWithCurrencyEffect, + grossPerformanceWithCurrencyEffect, hasErrors, netPerformance, - netPerformanceWithCurrencyEffect, netPerformancePercentage, netPerformancePercentageWithCurrencyEffect, + netPerformanceWithCurrencyEffect, timeWeightedInvestment, timeWeightedInvestmentWithCurrencyEffect, totalInvestment, @@ -598,15 +599,15 @@ export class PortfolioCalculator { fee: item.fee, firstBuyDate: item.firstBuyDate, grossPerformance: !hasErrors ? grossPerformance ?? null : null, - grossPerformanceWithCurrencyEffect: !hasErrors - ? grossPerformanceWithCurrencyEffect ?? null - : null, grossPerformancePercentage: !hasErrors ? grossPerformancePercentage ?? null : null, grossPerformancePercentageWithCurrencyEffect: !hasErrors ? grossPerformancePercentageWithCurrencyEffect ?? null : null, + grossPerformanceWithCurrencyEffect: !hasErrors + ? grossPerformanceWithCurrencyEffect ?? null + : null, investment: totalInvestment, investmentWithCurrencyEffect: totalInvestmentWithCurrencyEffect, marketPrice: @@ -614,15 +615,15 @@ export class PortfolioCalculator { marketPriceInBaseCurrency: marketPriceInBaseCurrency?.toNumber() ?? null, netPerformance: !hasErrors ? netPerformance ?? null : null, - netPerformanceWithCurrencyEffect: !hasErrors - ? netPerformanceWithCurrencyEffect ?? null - : null, netPerformancePercentage: !hasErrors ? netPerformancePercentage ?? null : null, netPerformancePercentageWithCurrencyEffect: !hasErrors ? netPerformancePercentageWithCurrencyEffect ?? null : null, + netPerformanceWithCurrencyEffect: !hasErrors + ? netPerformanceWithCurrencyEffect ?? null + : null, quantity: item.quantity, symbol: item.symbol, tags: item.tags, @@ -877,8 +878,8 @@ export class PortfolioCalculator { for (const currentPosition of positions) { if ( - currentPosition.marketPriceInBaseCurrency && - currentPosition.investment + currentPosition.investment && + currentPosition.marketPriceInBaseCurrency ) { currentValue = currentValue.plus( new Big(currentPosition.marketPriceInBaseCurrency).mul( @@ -1194,9 +1195,9 @@ export class PortfolioCalculator { investmentValues: {}, netPerformance: new Big(0), netPerformancePercentage: new Big(0), + netPerformanceValues: {}, netPerformanceWithCurrencyEffect: new Big(0), - netPerformanceWithCurrencyEffectPercentage: new Big(0), - netPerformanceValues: {} + netPerformanceWithCurrencyEffectPercentage: new Big(0) }; } @@ -1328,8 +1329,9 @@ export class PortfolioCalculator { const exchangeRateAtOrderDate = exchangeRates[order.date]; if (!exchangeRateAtOrderDate) { - console.error( - `${symbol}: No exchange rate found for date ${order.date}` + Logger.error( + `No exchange rate has been found for ${DEFAULT_CURRENCY}${order.currency} at ${order.date}`, + 'PortfolioCalculator' ); } diff --git a/apps/api/src/app/portfolio/portfolio.service.ts b/apps/api/src/app/portfolio/portfolio.service.ts index 100d1fcf3..bbd81a501 100644 --- a/apps/api/src/app/portfolio/portfolio.service.ts +++ b/apps/api/src/app/portfolio/portfolio.service.ts @@ -1230,10 +1230,6 @@ export class PortfolioService { if (itemOfToday) { currentNetPerformance = new Big(itemOfToday.netPerformance); - currentNetPerformanceWithCurrencyEffect = new Big( - itemOfToday.netPerformanceWithCurrencyEffect - ); - currentNetPerformancePercent = new Big( itemOfToday.netPerformanceInPercentage ).div(100); @@ -1241,6 +1237,10 @@ export class PortfolioService { currentNetPerformancePercentWithCurrencyEffect = new Big( itemOfToday.netPerformanceInPercentageWithCurrencyEffect ).div(100); + + currentNetPerformanceWithCurrencyEffect = new Big( + itemOfToday.netPerformanceWithCurrencyEffect + ); } accountBalanceItems = accountBalanceItems.filter(({ date }) => { diff --git a/apps/api/src/services/exchange-rate-data/exchange-rate-data.service.ts b/apps/api/src/services/exchange-rate-data/exchange-rate-data.service.ts index 7389a3ee8..49faecb75 100644 --- a/apps/api/src/services/exchange-rate-data/exchange-rate-data.service.ts +++ b/apps/api/src/services/exchange-rate-data/exchange-rate-data.service.ts @@ -53,13 +53,13 @@ export class ExchangeRateDataService { return {}; } + let currentDate = resetHours(startDate); + let dates: Date[] = []; + let exchangeRatesByCurrency: { [currency: string]: { [dateString: string]: number }; } = {}; - let dates: Date[] = []; - let currentDate = resetHours(startDate); - while (isAfter(endDate, currentDate)) { dates.push(currentDate); currentDate = addDays(currentDate, 1); 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 18238ee36..049a87fb9 100644 --- a/libs/common/src/lib/interfaces/historical-data-item.interface.ts +++ b/libs/common/src/lib/interfaces/historical-data-item.interface.ts @@ -4,9 +4,9 @@ export interface HistoricalDataItem { grossPerformancePercent?: number; marketPrice?: number; netPerformance?: number; - netPerformanceWithCurrencyEffect?: number; netPerformanceInPercentage?: number; netPerformanceInPercentageWithCurrencyEffect?: number; + netPerformanceWithCurrencyEffect?: number; netWorth?: number; netWorthInPercentage?: number; quantity?: number; @@ -14,6 +14,6 @@ export interface HistoricalDataItem { totalInvestment?: number; totalInvestmentValueWithCurrencyEffect?: number; value?: number; - valueWithCurrencyEffect?: number; valueInPercentage?: number; + valueWithCurrencyEffect?: number; }