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 { 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'
);
}

8
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 }) => {

6
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);

4
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;
}

Loading…
Cancel
Save