Browse Source

Refactoring

pull/2948/head
Thomas Kaul 2 years ago
parent
commit
d11f60308d
  1. 6
      apps/api/src/app/portfolio/interfaces/timeline-specification.interface.ts
  2. 2
      apps/api/src/app/portfolio/portfolio-calculator-novn-buy-and-sell.spec.ts
  3. 54
      apps/api/src/app/portfolio/portfolio-calculator.ts
  4. 3
      libs/common/src/lib/interfaces/symbol-metrics.interface.ts

6
apps/api/src/app/portfolio/interfaces/timeline-specification.interface.ts

@ -1,6 +0,0 @@
export type Accuracy = 'day' | 'month' | 'year';
export interface TimelineSpecification {
accuracy: Accuracy;
start: string;
}

2
apps/api/src/app/portfolio/portfolio-calculator-novn-buy-and-sell.spec.ts

@ -85,6 +85,7 @@ describe('PortfolioCalculator', () => {
expect(chartData[0]).toEqual({ expect(chartData[0]).toEqual({
date: '2022-03-07', date: '2022-03-07',
investment: 151.6,
netPerformance: 0, netPerformance: 0,
netPerformanceInPercentage: 0, netPerformanceInPercentage: 0,
netPerformanceInPercentageWithCurrencyEffect: 0, netPerformanceInPercentageWithCurrencyEffect: 0,
@ -97,6 +98,7 @@ describe('PortfolioCalculator', () => {
expect(chartData[chartData.length - 1]).toEqual({ expect(chartData[chartData.length - 1]).toEqual({
date: '2022-04-11', date: '2022-04-11',
investment: 0,
netPerformance: 19.86, netPerformance: 19.86,
netPerformanceInPercentage: 13.100263852242744, netPerformanceInPercentage: 13.100263852242744,
netPerformanceInPercentageWithCurrencyEffect: 13.100263852242744, netPerformanceInPercentageWithCurrencyEffect: 13.100263852242744,

54
apps/api/src/app/portfolio/portfolio-calculator.ts

@ -14,8 +14,6 @@ import Big from 'big.js';
import { import {
addDays, addDays,
addMilliseconds, addMilliseconds,
addMonths,
addYears,
differenceInDays, differenceInDays,
endOfDay, endOfDay,
format, format,
@ -32,10 +30,6 @@ import { CurrentRateService } from './current-rate.service';
import { CurrentPositions } from './interfaces/current-positions.interface'; import { CurrentPositions } from './interfaces/current-positions.interface';
import { PortfolioOrderItem } from './interfaces/portfolio-calculator.interface'; import { PortfolioOrderItem } from './interfaces/portfolio-calculator.interface';
import { PortfolioOrder } from './interfaces/portfolio-order.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 { TransactionPointSymbol } from './interfaces/transaction-point-symbol.interface';
import { TransactionPoint } from './interfaces/transaction-point.interface'; import { TransactionPoint } from './interfaces/transaction-point.interface';
@ -270,6 +264,7 @@ export class PortfolioCalculator {
netPerformanceValuesWithCurrencyEffect: { [date: string]: Big }; netPerformanceValuesWithCurrencyEffect: { [date: string]: Big };
timeWeightedInvestmentValues: { [date: string]: Big }; timeWeightedInvestmentValues: { [date: string]: Big };
timeWeightedInvestmentValuesWithCurrencyEffect: { [date: string]: Big }; timeWeightedInvestmentValuesWithCurrencyEffect: { [date: string]: Big };
transactionValues: { [date: string]: Big };
}; };
} = {}; } = {};
@ -282,7 +277,8 @@ export class PortfolioCalculator {
netPerformanceValues, netPerformanceValues,
netPerformanceValuesWithCurrencyEffect, netPerformanceValuesWithCurrencyEffect,
timeWeightedInvestmentValues, timeWeightedInvestmentValues,
timeWeightedInvestmentValuesWithCurrencyEffect timeWeightedInvestmentValuesWithCurrencyEffect,
transactionValues
} = this.getSymbolMetrics({ } = this.getSymbolMetrics({
end, end,
marketSymbolMap, marketSymbolMap,
@ -302,7 +298,8 @@ export class PortfolioCalculator {
netPerformanceValues, netPerformanceValues,
netPerformanceValuesWithCurrencyEffect, netPerformanceValuesWithCurrencyEffect,
timeWeightedInvestmentValues, timeWeightedInvestmentValues,
timeWeightedInvestmentValuesWithCurrencyEffect timeWeightedInvestmentValuesWithCurrencyEffect,
transactionValues
}; };
} }
@ -322,6 +319,9 @@ export class PortfolioCalculator {
const investmentValue = const investmentValue =
symbolValues.investmentValues?.[dateString] ?? new Big(0); symbolValues.investmentValues?.[dateString] ?? new Big(0);
const transactionValue =
symbolValues.transactionValues?.[dateString] ?? new Big(0);
const investmentValueWithCurrencyEffect = const investmentValueWithCurrencyEffect =
symbolValues.investmentValuesWithCurrencyEffect?.[dateString] ?? symbolValues.investmentValuesWithCurrencyEffect?.[dateString] ??
new Big(0); new Big(0);
@ -342,7 +342,9 @@ export class PortfolioCalculator {
] ?? new Big(0); ] ?? new Big(0);
accumulatedValuesByDate[dateString] = { accumulatedValuesByDate[dateString] = {
investmentValue, investmentValue: (
accumulatedValuesByDate[dateString]?.investmentValue ?? new Big(0)
).add(transactionValue),
totalCurrentValue: ( totalCurrentValue: (
accumulatedValuesByDate[dateString]?.totalCurrentValue ?? new Big(0) accumulatedValuesByDate[dateString]?.totalCurrentValue ?? new Big(0)
).add(currentValue), ).add(currentValue),
@ -890,17 +892,6 @@ export class PortfolioCalculator {
return factor; 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({ private getSymbolMetrics({
end, end,
exchangeRates, exchangeRates,
@ -944,6 +935,7 @@ export class PortfolioCalculator {
const netPerformanceValues: { [date: string]: Big } = {}; const netPerformanceValues: { [date: string]: Big } = {};
const netPerformanceValuesWithCurrencyEffect: { [date: string]: Big } = {}; const netPerformanceValuesWithCurrencyEffect: { [date: string]: Big } = {};
const timeWeightedInvestmentValues: { [date: string]: Big } = {}; const timeWeightedInvestmentValues: { [date: string]: Big } = {};
const transactionValues: { [date: string]: Big } = {};
const timeWeightedInvestmentValuesWithCurrencyEffect: { const timeWeightedInvestmentValuesWithCurrencyEffect: {
[date: string]: Big; [date: string]: Big;
@ -992,7 +984,8 @@ export class PortfolioCalculator {
timeWeightedInvestmentValuesWithCurrencyEffect: {}, timeWeightedInvestmentValuesWithCurrencyEffect: {},
timeWeightedInvestmentWithCurrencyEffect: new Big(0), timeWeightedInvestmentWithCurrencyEffect: new Big(0),
totalInvestment: new Big(0), totalInvestment: new Big(0),
totalInvestmentWithCurrencyEffect: new Big(0) totalInvestmentWithCurrencyEffect: new Big(0),
transactionValues: {}
}; };
} }
@ -1031,7 +1024,8 @@ export class PortfolioCalculator {
timeWeightedInvestmentValuesWithCurrencyEffect: {}, timeWeightedInvestmentValuesWithCurrencyEffect: {},
timeWeightedInvestmentWithCurrencyEffect: new Big(0), timeWeightedInvestmentWithCurrencyEffect: new Big(0),
totalInvestment: new Big(0), totalInvestment: new Big(0),
totalInvestmentWithCurrencyEffect: new Big(0) totalInvestmentWithCurrencyEffect: new Big(0),
transactionValues: {}
}; };
} }
@ -1427,6 +1421,10 @@ export class PortfolioCalculator {
totalInvestmentDays totalInvestmentDays
) )
: new Big(0); : new Big(0);
transactionValues[order.date] = (
transactionValues[order.date] ?? new Big(0)
).add(transactionInvestmentWithCurrencyEffect);
} }
} }
@ -1583,6 +1581,7 @@ export class PortfolioCalculator {
timeWeightedInvestmentValuesWithCurrencyEffect, timeWeightedInvestmentValuesWithCurrencyEffect,
totalInvestment, totalInvestment,
totalInvestmentWithCurrencyEffect, totalInvestmentWithCurrencyEffect,
transactionValues,
grossPerformance: totalGrossPerformance, grossPerformance: totalGrossPerformance,
grossPerformanceWithCurrencyEffect: grossPerformanceWithCurrencyEffect:
totalGrossPerformanceWithCurrencyEffect, totalGrossPerformanceWithCurrencyEffect,
@ -1595,15 +1594,4 @@ export class PortfolioCalculator {
timeWeightedAverageInvestmentBetweenStartAndEndDateWithCurrencyEffect timeWeightedAverageInvestmentBetweenStartAndEndDateWithCurrencyEffect
}; };
} }
private isNextItemActive(
timelineSpecification: TimelineSpecification[],
currentDate: Date,
i: number
) {
return (
i + 1 < timelineSpecification.length &&
!isBefore(currentDate, parseDate(timelineSpecification[i + 1].start))
);
}
} }

3
libs/common/src/lib/interfaces/symbol-metrics.interface.ts

@ -38,4 +38,7 @@ export interface SymbolMetrics {
timeWeightedInvestmentWithCurrencyEffect: Big; timeWeightedInvestmentWithCurrencyEffect: Big;
totalInvestment: Big; totalInvestment: Big;
totalInvestmentWithCurrencyEffect: Big; totalInvestmentWithCurrencyEffect: Big;
transactionValues: {
[date: string]: Big;
};
} }

Loading…
Cancel
Save