Browse Source

Refactoring

pull/2834/head
Thomas Kaul 2 years ago
parent
commit
dc999cc098
  1. 54
      apps/api/src/app/portfolio/portfolio-calculator.ts
  2. 8
      apps/api/src/app/portfolio/portfolio.service.ts
  3. 6
      apps/api/src/services/exchange-rate-data/exchange-rate-data.service.ts
  4. 4
      libs/common/src/lib/interfaces/historical-data-item.interface.ts

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

@ -1,6 +1,7 @@
import { TimelineInfoInterface } from '@ghostfolio/api/app/portfolio/interfaces/timeline-info.interface'; 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 { ExchangeRateDataService } from '@ghostfolio/api/services/exchange-rate-data/exchange-rate-data.service';
import { IDataGatheringItem } from '@ghostfolio/api/services/interfaces/interfaces'; 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 { DATE_FORMAT, parseDate, resetHours } from '@ghostfolio/common/helper';
import { import {
DataProviderInfo, DataProviderInfo,
@ -53,7 +54,7 @@ import { TransactionPointSymbol } from './interfaces/transaction-point-symbol.in
import { TransactionPoint } from './interfaces/transaction-point.interface'; import { TransactionPoint } from './interfaces/transaction-point.interface';
export class PortfolioCalculator { export class PortfolioCalculator {
private static ENABLE_LOGGING = false; private static readonly ENABLE_LOGGING = false;
private currency: string; private currency: string;
private currentRateService: CurrentRateService; private currentRateService: CurrentRateService;
@ -364,14 +365,6 @@ export class PortfolioCalculator {
accumulatedValuesByDate[dateString] accumulatedValuesByDate[dateString]
?.totalInvestmentValueWithCurrencyEffect ?? new Big(0) ?.totalInvestmentValueWithCurrencyEffect ?? new Big(0)
).add(investmentValueWithCurrencyEffect), ).add(investmentValueWithCurrencyEffect),
totalTimeWeightedInvestmentValue: (
accumulatedValuesByDate[dateString]
?.totalTimeWeightedInvestmentValue ?? new Big(0)
).add(timeWeightedInvestmentValue),
totalTimeWeightedInvestmentValueWithCurrencyEffect: (
accumulatedValuesByDate[dateString]
?.totalTimeWeightedInvestmentValueWithCurrencyEffect ?? new Big(0)
).add(timeWeightedInvestmentValueWithCurrencyEffect),
totalNetPerformanceValue: ( totalNetPerformanceValue: (
accumulatedValuesByDate[dateString]?.totalNetPerformanceValue ?? accumulatedValuesByDate[dateString]?.totalNetPerformanceValue ??
new Big(0) new Big(0)
@ -379,7 +372,15 @@ export class PortfolioCalculator {
totalNetPerformanceValueWithCurrencyEffect: ( totalNetPerformanceValueWithCurrencyEffect: (
accumulatedValuesByDate[dateString] accumulatedValuesByDate[dateString]
?.totalNetPerformanceValueWithCurrencyEffect ?? new Big(0) ?.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 { return {
currentValue: new Big(0), currentValue: new Big(0),
grossPerformance: new Big(0), grossPerformance: new Big(0),
grossPerformanceWithCurrencyEffect: new Big(0),
grossPerformancePercentage: new Big(0), grossPerformancePercentage: new Big(0),
grossPerformancePercentageWithCurrencyEffect: new Big(0), grossPerformancePercentageWithCurrencyEffect: new Big(0),
grossPerformanceWithCurrencyEffect: new Big(0),
hasErrors: false, hasErrors: false,
netPerformance: new Big(0), netPerformance: new Big(0),
netPerformanceWithCurrencyEffect: new Big(0),
netPerformancePercentage: new Big(0), netPerformancePercentage: new Big(0),
netPerformancePercentageWithCurrencyEffect: new Big(0), netPerformancePercentageWithCurrencyEffect: new Big(0),
netPerformanceWithCurrencyEffect: new Big(0),
positions: [], positions: [],
totalInvestment: new Big(0) totalInvestment: new Big(0)
}; };
@ -565,14 +566,14 @@ export class PortfolioCalculator {
const { const {
grossPerformance, grossPerformance,
grossPerformanceWithCurrencyEffect,
grossPerformancePercentage, grossPerformancePercentage,
grossPerformancePercentageWithCurrencyEffect, grossPerformancePercentageWithCurrencyEffect,
grossPerformanceWithCurrencyEffect,
hasErrors, hasErrors,
netPerformance, netPerformance,
netPerformanceWithCurrencyEffect,
netPerformancePercentage, netPerformancePercentage,
netPerformancePercentageWithCurrencyEffect, netPerformancePercentageWithCurrencyEffect,
netPerformanceWithCurrencyEffect,
timeWeightedInvestment, timeWeightedInvestment,
timeWeightedInvestmentWithCurrencyEffect, timeWeightedInvestmentWithCurrencyEffect,
totalInvestment, totalInvestment,
@ -598,15 +599,15 @@ export class PortfolioCalculator {
fee: item.fee, fee: item.fee,
firstBuyDate: item.firstBuyDate, firstBuyDate: item.firstBuyDate,
grossPerformance: !hasErrors ? grossPerformance ?? null : null, grossPerformance: !hasErrors ? grossPerformance ?? null : null,
grossPerformanceWithCurrencyEffect: !hasErrors
? grossPerformanceWithCurrencyEffect ?? null
: null,
grossPerformancePercentage: !hasErrors grossPerformancePercentage: !hasErrors
? grossPerformancePercentage ?? null ? grossPerformancePercentage ?? null
: null, : null,
grossPerformancePercentageWithCurrencyEffect: !hasErrors grossPerformancePercentageWithCurrencyEffect: !hasErrors
? grossPerformancePercentageWithCurrencyEffect ?? null ? grossPerformancePercentageWithCurrencyEffect ?? null
: null, : null,
grossPerformanceWithCurrencyEffect: !hasErrors
? grossPerformanceWithCurrencyEffect ?? null
: null,
investment: totalInvestment, investment: totalInvestment,
investmentWithCurrencyEffect: totalInvestmentWithCurrencyEffect, investmentWithCurrencyEffect: totalInvestmentWithCurrencyEffect,
marketPrice: marketPrice:
@ -614,15 +615,15 @@ export class PortfolioCalculator {
marketPriceInBaseCurrency: marketPriceInBaseCurrency:
marketPriceInBaseCurrency?.toNumber() ?? null, marketPriceInBaseCurrency?.toNumber() ?? null,
netPerformance: !hasErrors ? netPerformance ?? null : null, netPerformance: !hasErrors ? netPerformance ?? null : null,
netPerformanceWithCurrencyEffect: !hasErrors
? netPerformanceWithCurrencyEffect ?? null
: null,
netPerformancePercentage: !hasErrors netPerformancePercentage: !hasErrors
? netPerformancePercentage ?? null ? netPerformancePercentage ?? null
: null, : null,
netPerformancePercentageWithCurrencyEffect: !hasErrors netPerformancePercentageWithCurrencyEffect: !hasErrors
? netPerformancePercentageWithCurrencyEffect ?? null ? netPerformancePercentageWithCurrencyEffect ?? null
: null, : null,
netPerformanceWithCurrencyEffect: !hasErrors
? netPerformanceWithCurrencyEffect ?? null
: null,
quantity: item.quantity, quantity: item.quantity,
symbol: item.symbol, symbol: item.symbol,
tags: item.tags, tags: item.tags,
@ -877,8 +878,8 @@ export class PortfolioCalculator {
for (const currentPosition of positions) { for (const currentPosition of positions) {
if ( if (
currentPosition.marketPriceInBaseCurrency && currentPosition.investment &&
currentPosition.investment currentPosition.marketPriceInBaseCurrency
) { ) {
currentValue = currentValue.plus( currentValue = currentValue.plus(
new Big(currentPosition.marketPriceInBaseCurrency).mul( new Big(currentPosition.marketPriceInBaseCurrency).mul(
@ -1194,9 +1195,9 @@ export class PortfolioCalculator {
investmentValues: {}, investmentValues: {},
netPerformance: new Big(0), netPerformance: new Big(0),
netPerformancePercentage: new Big(0), netPerformancePercentage: new Big(0),
netPerformanceValues: {},
netPerformanceWithCurrencyEffect: new Big(0), netPerformanceWithCurrencyEffect: new Big(0),
netPerformanceWithCurrencyEffectPercentage: new Big(0), netPerformanceWithCurrencyEffectPercentage: new Big(0)
netPerformanceValues: {}
}; };
} }
@ -1328,8 +1329,9 @@ export class PortfolioCalculator {
const exchangeRateAtOrderDate = exchangeRates[order.date]; const exchangeRateAtOrderDate = exchangeRates[order.date];
if (!exchangeRateAtOrderDate) { if (!exchangeRateAtOrderDate) {
console.error( Logger.error(
`${symbol}: No exchange rate found for date ${order.date}` `No exchange rate has been found for ${DEFAULT_CURRENCY}${order.currency} at ${order.date}`,
'PortfolioCalculator'
); );
} }

8
apps/api/src/app/portfolio/portfolio.service.ts

@ -1230,10 +1230,6 @@ export class PortfolioService {
if (itemOfToday) { if (itemOfToday) {
currentNetPerformance = new Big(itemOfToday.netPerformance); currentNetPerformance = new Big(itemOfToday.netPerformance);
currentNetPerformanceWithCurrencyEffect = new Big(
itemOfToday.netPerformanceWithCurrencyEffect
);
currentNetPerformancePercent = new Big( currentNetPerformancePercent = new Big(
itemOfToday.netPerformanceInPercentage itemOfToday.netPerformanceInPercentage
).div(100); ).div(100);
@ -1241,6 +1237,10 @@ export class PortfolioService {
currentNetPerformancePercentWithCurrencyEffect = new Big( currentNetPerformancePercentWithCurrencyEffect = new Big(
itemOfToday.netPerformanceInPercentageWithCurrencyEffect itemOfToday.netPerformanceInPercentageWithCurrencyEffect
).div(100); ).div(100);
currentNetPerformanceWithCurrencyEffect = new Big(
itemOfToday.netPerformanceWithCurrencyEffect
);
} }
accountBalanceItems = accountBalanceItems.filter(({ date }) => { accountBalanceItems = accountBalanceItems.filter(({ date }) => {

6
apps/api/src/services/exchange-rate-data/exchange-rate-data.service.ts

@ -53,13 +53,13 @@ export class ExchangeRateDataService {
return {}; return {};
} }
let currentDate = resetHours(startDate);
let dates: Date[] = [];
let exchangeRatesByCurrency: { let exchangeRatesByCurrency: {
[currency: string]: { [dateString: string]: number }; [currency: string]: { [dateString: string]: number };
} = {}; } = {};
let dates: Date[] = [];
let currentDate = resetHours(startDate);
while (isAfter(endDate, currentDate)) { while (isAfter(endDate, currentDate)) {
dates.push(currentDate); dates.push(currentDate);
currentDate = addDays(currentDate, 1); currentDate = addDays(currentDate, 1);

4
libs/common/src/lib/interfaces/historical-data-item.interface.ts

@ -4,9 +4,9 @@ export interface HistoricalDataItem {
grossPerformancePercent?: number; grossPerformancePercent?: number;
marketPrice?: number; marketPrice?: number;
netPerformance?: number; netPerformance?: number;
netPerformanceWithCurrencyEffect?: number;
netPerformanceInPercentage?: number; netPerformanceInPercentage?: number;
netPerformanceInPercentageWithCurrencyEffect?: number; netPerformanceInPercentageWithCurrencyEffect?: number;
netPerformanceWithCurrencyEffect?: number;
netWorth?: number; netWorth?: number;
netWorthInPercentage?: number; netWorthInPercentage?: number;
quantity?: number; quantity?: number;
@ -14,6 +14,6 @@ export interface HistoricalDataItem {
totalInvestment?: number; totalInvestment?: number;
totalInvestmentValueWithCurrencyEffect?: number; totalInvestmentValueWithCurrencyEffect?: number;
value?: number; value?: number;
valueWithCurrencyEffect?: number;
valueInPercentage?: number; valueInPercentage?: number;
valueWithCurrencyEffect?: number;
} }

Loading…
Cancel
Save