Browse Source

Various improvements

pull/3393/head
Thomas Kaul 1 year ago
parent
commit
e209bca0da
  1. 10
      apps/api/src/app/portfolio/calculator/portfolio-calculator.ts
  2. 2
      apps/api/src/app/portfolio/calculator/twr/portfolio-calculator.ts
  3. 93
      apps/api/src/app/portfolio/portfolio.service.ts
  4. 4
      libs/common/src/lib/models/portfolio-snapshot.ts

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

@ -187,7 +187,7 @@ export abstract class PortfolioCalculator {
if (!transactionPoints.length) {
return {
chartData: [],
historicalData: [],
currentValueInBaseCurrency: new Big(0),
grossPerformance: new Big(0),
grossPerformancePercentage: new Big(0),
@ -577,7 +577,7 @@ export abstract class PortfolioCalculator {
}
}
const chartData: HistoricalDataItem[] = Object.entries(
const historicalData: HistoricalDataItem[] = Object.entries(
accumulatedValuesByDate
).map(([date, values]) => {
const {
@ -629,8 +629,8 @@ export abstract class PortfolioCalculator {
return {
...overall,
chartData,
errors,
historicalData,
positions,
totalInterestWithCurrencyEffect,
totalLiabilitiesWithCurrencyEffect,
@ -1095,7 +1095,7 @@ export abstract class PortfolioCalculator {
public async getPerformance({ end, start }) {
await this.snapshotPromise;
const { chartData } = this.snapshot;
const { historicalData } = this.snapshot;
const newChartData: HistoricalDataItem[] = [];
@ -1104,7 +1104,7 @@ export abstract class PortfolioCalculator {
let netPerformanceInPercentageWithCurrencyEffectAtStartDate: number;
let totalInvestmentValuesWithCurrencyEffect: number[] = [];
for (let historicalDataItem of chartData) {
for (let historicalDataItem of historicalData) {
if (
!isBefore(parseDate(historicalDataItem.date), subDays(start, 1)) &&
!isAfter(parseDate(historicalDataItem.date), end)

2
apps/api/src/app/portfolio/calculator/twr/portfolio-calculator.ts

@ -115,7 +115,7 @@ export class TWRPortfolioCalculator extends PortfolioCalculator {
totalInterestWithCurrencyEffect,
totalInvestment,
totalInvestmentWithCurrencyEffect,
chartData: [],
historicalData: [],
netPerformancePercentage: totalTimeWeightedInvestment.eq(0)
? new Big(0)
: netPerformance.div(totalTimeWeightedInvestment),

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

@ -70,7 +70,7 @@ import {
parseISO,
set
} from 'date-fns';
import { isEmpty, isNumber, uniq, uniqBy } from 'lodash';
import { isEmpty, isNumber, last, uniq, uniqBy } from 'lodash';
import { PortfolioCalculator } from './calculator/portfolio-calculator';
import {
@ -1181,62 +1181,15 @@ export class PortfolioService {
this.request.user.Settings.settings.isExperimentalFeatures
});
const {
chartData,
currentValueInBaseCurrency,
errors,
grossPerformance,
grossPerformancePercentage,
grossPerformancePercentageWithCurrencyEffect,
grossPerformanceWithCurrencyEffect,
hasErrors,
netPerformance,
netPerformancePercentage,
netPerformancePercentageWithCurrencyEffect,
netPerformanceWithCurrencyEffect,
totalInvestment
} = await portfolioCalculator.getSnapshot();
const { historicalData, errors, hasErrors } =
await portfolioCalculator.getSnapshot();
console.timeEnd('------- PortfolioService.getPerformance - 3');
console.time('------- PortfolioService.getPerformance - 4');
let currentNetPerformance = netPerformance;
let currentNetPerformancePercentage = netPerformancePercentage;
let currentNetPerformancePercentageWithCurrencyEffect =
netPerformancePercentageWithCurrencyEffect;
let currentNetPerformanceWithCurrencyEffect =
netPerformanceWithCurrencyEffect;
let currentNetWorth = 0;
console.timeEnd('------- PortfolioService.getPerformance - 4');
console.time('------- PortfolioService.getPerformance - 5');
const itemOfToday = chartData.find(({ date }) => {
return date === format(new Date(), DATE_FORMAT);
});
if (itemOfToday) {
currentNetPerformance = new Big(itemOfToday.netPerformance);
currentNetPerformancePercentage = new Big(
itemOfToday.netPerformanceInPercentage
).div(100);
currentNetPerformancePercentageWithCurrencyEffect = new Big(
itemOfToday.netPerformanceInPercentageWithCurrencyEffect
).div(100);
currentNetPerformanceWithCurrencyEffect = new Big(
itemOfToday.netPerformanceWithCurrencyEffect
);
currentNetWorth = itemOfToday.netWorth;
}
console.timeEnd('------- PortfolioService.getPerformance - 5');
console.timeEnd('------ PortfolioService.getPerformance');
@ -1246,27 +1199,37 @@ export class PortfolioService {
start: startDate
});
const {
grossPerformancePercent,
netPerformance,
netPerformanceInPercentage,
netPerformanceInPercentageWithCurrencyEffect,
netPerformanceWithCurrencyEffect,
netWorth,
totalInvestment,
valueWithCurrencyEffect
} = last(chart);
return {
chart,
errors,
hasErrors,
firstOrderDate: parseDate(chartData[0]?.date),
firstOrderDate: parseDate(historicalData[0]?.date),
performance: {
currentNetWorth,
currentValueInBaseCurrency: currentValueInBaseCurrency.toNumber(),
grossPerformance: grossPerformance.toNumber(),
grossPerformancePercentage: grossPerformancePercentage.toNumber(),
grossPerformancePercentageWithCurrencyEffect:
grossPerformancePercentageWithCurrencyEffect.toNumber(),
grossPerformanceWithCurrencyEffect:
grossPerformanceWithCurrencyEffect.toNumber(),
netPerformance: currentNetPerformance.toNumber(),
netPerformancePercentage: currentNetPerformancePercentage.toNumber(),
netPerformance,
netPerformanceWithCurrencyEffect,
totalInvestment,
currentNetWorth: netWorth,
currentValueInBaseCurrency: valueWithCurrencyEffect,
// TODO
grossPerformance: 0,
grossPerformancePercentage: grossPerformancePercent / 100,
grossPerformancePercentageWithCurrencyEffect: 0 / 100,
// TODO
grossPerformanceWithCurrencyEffect: 0,
netPerformancePercentage: netPerformanceInPercentage / 100,
netPerformancePercentageWithCurrencyEffect:
currentNetPerformancePercentageWithCurrencyEffect.toNumber(),
netPerformanceWithCurrencyEffect:
currentNetPerformanceWithCurrencyEffect.toNumber(),
totalInvestment: totalInvestment.toNumber()
netPerformanceInPercentageWithCurrencyEffect / 100
}
};
}

4
libs/common/src/lib/models/portfolio-snapshot.ts

@ -6,8 +6,6 @@ import { Big } from 'big.js';
import { Transform, Type } from 'class-transformer';
export class PortfolioSnapshot {
chartData: HistoricalDataItem[];
@Transform(transformToBig, { toClassOnly: true })
@Type(() => Big)
currentValueInBaseCurrency: Big;
@ -32,6 +30,8 @@ export class PortfolioSnapshot {
hasErrors: boolean;
historicalData: HistoricalDataItem[];
@Transform(transformToBig, { toClassOnly: true })
@Type(() => Big)
netAnnualizedPerformance?: Big;

Loading…
Cancel
Save