From d11f60308dd4361d463e10e2e0829ee96588eeb0 Mon Sep 17 00:00:00 2001 From: Thomas Kaul <4159106+dtslvr@users.noreply.github.com> Date: Thu, 1 Feb 2024 21:19:14 +0100 Subject: [PATCH] Refactoring --- .../timeline-specification.interface.ts | 6 --- ...folio-calculator-novn-buy-and-sell.spec.ts | 2 + .../src/app/portfolio/portfolio-calculator.ts | 54 ++++++++----------- .../interfaces/symbol-metrics.interface.ts | 3 ++ 4 files changed, 26 insertions(+), 39 deletions(-) delete mode 100644 apps/api/src/app/portfolio/interfaces/timeline-specification.interface.ts diff --git a/apps/api/src/app/portfolio/interfaces/timeline-specification.interface.ts b/apps/api/src/app/portfolio/interfaces/timeline-specification.interface.ts deleted file mode 100644 index 1cd6510c7..000000000 --- a/apps/api/src/app/portfolio/interfaces/timeline-specification.interface.ts +++ /dev/null @@ -1,6 +0,0 @@ -export type Accuracy = 'day' | 'month' | 'year'; - -export interface TimelineSpecification { - accuracy: Accuracy; - start: string; -} 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 3e30374c4..dd1b31f2e 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,6 +85,7 @@ describe('PortfolioCalculator', () => { expect(chartData[0]).toEqual({ date: '2022-03-07', + investment: 151.6, netPerformance: 0, netPerformanceInPercentage: 0, netPerformanceInPercentageWithCurrencyEffect: 0, @@ -97,6 +98,7 @@ describe('PortfolioCalculator', () => { expect(chartData[chartData.length - 1]).toEqual({ date: '2022-04-11', + investment: 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 af076f6f0..ebfc5ff39 100644 --- a/apps/api/src/app/portfolio/portfolio-calculator.ts +++ b/apps/api/src/app/portfolio/portfolio-calculator.ts @@ -14,8 +14,6 @@ import Big from 'big.js'; import { addDays, addMilliseconds, - addMonths, - addYears, differenceInDays, endOfDay, format, @@ -32,10 +30,6 @@ import { CurrentRateService } from './current-rate.service'; import { CurrentPositions } from './interfaces/current-positions.interface'; import { PortfolioOrderItem } from './interfaces/portfolio-calculator.interface'; import { PortfolioOrder } from './interfaces/portfolio-order.interface'; -import { - Accuracy, - TimelineSpecification -} from './interfaces/timeline-specification.interface'; import { TransactionPointSymbol } from './interfaces/transaction-point-symbol.interface'; import { TransactionPoint } from './interfaces/transaction-point.interface'; @@ -270,6 +264,7 @@ export class PortfolioCalculator { netPerformanceValuesWithCurrencyEffect: { [date: string]: Big }; timeWeightedInvestmentValues: { [date: string]: Big }; timeWeightedInvestmentValuesWithCurrencyEffect: { [date: string]: Big }; + transactionValues: { [date: string]: Big }; }; } = {}; @@ -282,7 +277,8 @@ export class PortfolioCalculator { netPerformanceValues, netPerformanceValuesWithCurrencyEffect, timeWeightedInvestmentValues, - timeWeightedInvestmentValuesWithCurrencyEffect + timeWeightedInvestmentValuesWithCurrencyEffect, + transactionValues } = this.getSymbolMetrics({ end, marketSymbolMap, @@ -302,7 +298,8 @@ export class PortfolioCalculator { netPerformanceValues, netPerformanceValuesWithCurrencyEffect, timeWeightedInvestmentValues, - timeWeightedInvestmentValuesWithCurrencyEffect + timeWeightedInvestmentValuesWithCurrencyEffect, + transactionValues }; } @@ -322,6 +319,9 @@ export class PortfolioCalculator { const investmentValue = symbolValues.investmentValues?.[dateString] ?? new Big(0); + const transactionValue = + symbolValues.transactionValues?.[dateString] ?? new Big(0); + const investmentValueWithCurrencyEffect = symbolValues.investmentValuesWithCurrencyEffect?.[dateString] ?? new Big(0); @@ -342,7 +342,9 @@ export class PortfolioCalculator { ] ?? new Big(0); accumulatedValuesByDate[dateString] = { - investmentValue, + investmentValue: ( + accumulatedValuesByDate[dateString]?.investmentValue ?? new Big(0) + ).add(transactionValue), totalCurrentValue: ( accumulatedValuesByDate[dateString]?.totalCurrentValue ?? new Big(0) ).add(currentValue), @@ -890,17 +892,6 @@ export class PortfolioCalculator { return factor; } - private addToDate(date: Date, accuracy: Accuracy): Date { - switch (accuracy) { - case 'day': - return addDays(date, 1); - case 'month': - return addMonths(date, 1); - case 'year': - return addYears(date, 1); - } - } - private getSymbolMetrics({ end, exchangeRates, @@ -944,6 +935,7 @@ export class PortfolioCalculator { const netPerformanceValues: { [date: string]: Big } = {}; const netPerformanceValuesWithCurrencyEffect: { [date: string]: Big } = {}; const timeWeightedInvestmentValues: { [date: string]: Big } = {}; + const transactionValues: { [date: string]: Big } = {}; const timeWeightedInvestmentValuesWithCurrencyEffect: { [date: string]: Big; @@ -992,7 +984,8 @@ export class PortfolioCalculator { timeWeightedInvestmentValuesWithCurrencyEffect: {}, timeWeightedInvestmentWithCurrencyEffect: new Big(0), totalInvestment: new Big(0), - totalInvestmentWithCurrencyEffect: new Big(0) + totalInvestmentWithCurrencyEffect: new Big(0), + transactionValues: {} }; } @@ -1031,7 +1024,8 @@ export class PortfolioCalculator { timeWeightedInvestmentValuesWithCurrencyEffect: {}, timeWeightedInvestmentWithCurrencyEffect: new Big(0), totalInvestment: new Big(0), - totalInvestmentWithCurrencyEffect: new Big(0) + totalInvestmentWithCurrencyEffect: new Big(0), + transactionValues: {} }; } @@ -1427,6 +1421,10 @@ export class PortfolioCalculator { totalInvestmentDays ) : new Big(0); + + transactionValues[order.date] = ( + transactionValues[order.date] ?? new Big(0) + ).add(transactionInvestmentWithCurrencyEffect); } } @@ -1583,6 +1581,7 @@ export class PortfolioCalculator { timeWeightedInvestmentValuesWithCurrencyEffect, totalInvestment, totalInvestmentWithCurrencyEffect, + transactionValues, grossPerformance: totalGrossPerformance, grossPerformanceWithCurrencyEffect: totalGrossPerformanceWithCurrencyEffect, @@ -1595,15 +1594,4 @@ export class PortfolioCalculator { timeWeightedAverageInvestmentBetweenStartAndEndDateWithCurrencyEffect }; } - - private isNextItemActive( - timelineSpecification: TimelineSpecification[], - currentDate: Date, - i: number - ) { - return ( - i + 1 < timelineSpecification.length && - !isBefore(currentDate, parseDate(timelineSpecification[i + 1].start)) - ); - } } diff --git a/libs/common/src/lib/interfaces/symbol-metrics.interface.ts b/libs/common/src/lib/interfaces/symbol-metrics.interface.ts index 71cec81b2..dc3bd0e34 100644 --- a/libs/common/src/lib/interfaces/symbol-metrics.interface.ts +++ b/libs/common/src/lib/interfaces/symbol-metrics.interface.ts @@ -38,4 +38,7 @@ export interface SymbolMetrics { timeWeightedInvestmentWithCurrencyEffect: Big; totalInvestment: Big; totalInvestmentWithCurrencyEffect: Big; + transactionValues: { + [date: string]: Big; + }; }