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

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

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

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

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

Loading…
Cancel
Save