Browse Source

Improve calculation of overall performance percentage

pull/701/head
Reto Kaul 3 years ago
parent
commit
7e9e7c9928
  1. 48
      apps/api/src/app/portfolio/portfolio-calculator-new.ts

48
apps/api/src/app/portfolio/portfolio-calculator-new.ts

@ -272,7 +272,7 @@ export class PortfolioCalculatorNew {
transactionCount: item.transactionCount transactionCount: item.transactionCount
}); });
} }
const overall = this.calculateOverallPerformance(positions, initialValues); const overall = this.calculateOverallPerformance(positions);
return { return {
...overall, ...overall,
@ -393,18 +393,27 @@ export class PortfolioCalculatorNew {
}; };
} }
private calculateOverallPerformance( private calculateOverallPerformance(positions: TimelinePosition[]) {
positions: TimelinePosition[],
initialValues: { [p: string]: Big }
) {
let hasErrors = false;
let currentValue = new Big(0); let currentValue = new Big(0);
let totalInvestment = new Big(0);
let grossPerformance = new Big(0); let grossPerformance = new Big(0);
let grossPerformancePercentage = new Big(0); let grossPerformancePercentage = new Big(0);
let hasErrors = false;
let netPerformance = new Big(0); let netPerformance = new Big(0);
let netPerformancePercentage = new Big(0); let netPerformancePercentage = new Big(0);
let completeInitialValue = new Big(0); let totalGrossPerformanceFluctuation = new Big(0);
let totalInvestment = new Big(0);
for (const currentPosition of positions) {
if (
currentPosition.grossPerformance &&
currentPosition.grossPerformancePercentage
) {
totalGrossPerformanceFluctuation =
totalGrossPerformanceFluctuation.plus(
currentPosition.grossPerformance.abs()
);
}
}
for (const currentPosition of positions) { for (const currentPosition of positions) {
if (currentPosition.marketPrice) { if (currentPosition.marketPrice) {
@ -414,11 +423,14 @@ export class PortfolioCalculatorNew {
} else { } else {
hasErrors = true; hasErrors = true;
} }
totalInvestment = totalInvestment.plus(currentPosition.investment); totalInvestment = totalInvestment.plus(currentPosition.investment);
if (currentPosition.grossPerformance) { if (currentPosition.grossPerformance) {
grossPerformance = grossPerformance.plus( grossPerformance = grossPerformance.plus(
currentPosition.grossPerformance currentPosition.grossPerformance
); );
netPerformance = netPerformance.plus(currentPosition.netPerformance); netPerformance = netPerformance.plus(currentPosition.netPerformance);
} else if (!currentPosition.quantity.eq(0)) { } else if (!currentPosition.quantity.eq(0)) {
hasErrors = true; hasErrors = true;
@ -426,15 +438,18 @@ export class PortfolioCalculatorNew {
if ( if (
currentPosition.grossPerformancePercentage && currentPosition.grossPerformancePercentage &&
initialValues[currentPosition.symbol] !totalGrossPerformanceFluctuation.eq(0)
) { ) {
const currentInitialValue = initialValues[currentPosition.symbol]; const ratioOfCurrentPosition = currentPosition.grossPerformance
completeInitialValue = completeInitialValue.plus(currentInitialValue); .abs()
.div(totalGrossPerformanceFluctuation);
grossPerformancePercentage = grossPerformancePercentage.plus( grossPerformancePercentage = grossPerformancePercentage.plus(
currentPosition.grossPerformancePercentage.mul(currentInitialValue) currentPosition.grossPerformancePercentage.mul(ratioOfCurrentPosition)
); );
netPerformancePercentage = netPerformancePercentage.plus( netPerformancePercentage = netPerformancePercentage.plus(
currentPosition.netPerformancePercentage.mul(currentInitialValue) currentPosition.netPerformancePercentage.mul(ratioOfCurrentPosition)
); );
} else if (!currentPosition.quantity.eq(0)) { } else if (!currentPosition.quantity.eq(0)) {
Logger.warn( Logger.warn(
@ -444,13 +459,6 @@ export class PortfolioCalculatorNew {
} }
} }
if (!completeInitialValue.eq(0)) {
grossPerformancePercentage =
grossPerformancePercentage.div(completeInitialValue);
netPerformancePercentage =
netPerformancePercentage.div(completeInitialValue);
}
return { return {
currentValue, currentValue,
grossPerformance, grossPerformance,

Loading…
Cancel
Save