Browse Source

Improvements

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

58
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); const overall = this.calculateOverallPerformance(positions, initialValues);
return { return {
...overall, ...overall,
@ -393,28 +393,19 @@ export class PortfolioCalculatorNew {
}; };
} }
private calculateOverallPerformance(positions: TimelinePosition[]) { private calculateOverallPerformance(
positions: TimelinePosition[],
initialValues: { [symbol: string]: Big }
) {
let currentValue = new Big(0); let currentValue = 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 hasErrors = false;
let netPerformance = new Big(0); let netPerformance = new Big(0);
let netPerformancePercentage = new Big(0); let netPerformancePercentage = new Big(0);
let totalGrossPerformanceFluctuation = new Big(0); let sumOfWeights = new Big(0);
let totalInvestment = 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) {
currentValue = currentValue.plus( currentValue = currentValue.plus(
@ -436,20 +427,21 @@ export class PortfolioCalculatorNew {
hasErrors = true; hasErrors = true;
} }
if ( if (currentPosition.grossPerformancePercentage) {
currentPosition.grossPerformancePercentage && // Use the average from the initial value and the current investment as
!totalGrossPerformanceFluctuation.eq(0) // a weight
) { const weight = (initialValues[currentPosition.symbol] ?? new Big(0))
const ratioOfCurrentPosition = currentPosition.grossPerformance .plus(currentPosition.investment)
.abs() .div(2);
.div(totalGrossPerformanceFluctuation);
sumOfWeights = sumOfWeights.plus(weight);
grossPerformancePercentage = grossPerformancePercentage.plus( grossPerformancePercentage = grossPerformancePercentage.plus(
currentPosition.grossPerformancePercentage.mul(ratioOfCurrentPosition) currentPosition.grossPerformancePercentage.mul(weight)
); );
netPerformancePercentage = netPerformancePercentage.plus( netPerformancePercentage = netPerformancePercentage.plus(
currentPosition.netPerformancePercentage.mul(ratioOfCurrentPosition) currentPosition.netPerformancePercentage.mul(weight)
); );
} else if (!currentPosition.quantity.eq(0)) { } else if (!currentPosition.quantity.eq(0)) {
Logger.warn( Logger.warn(
@ -459,6 +451,9 @@ export class PortfolioCalculatorNew {
} }
} }
grossPerformancePercentage = grossPerformancePercentage.div(sumOfWeights);
netPerformancePercentage = netPerformancePercentage.div(sumOfWeights);
return { return {
currentValue, currentValue,
grossPerformance, grossPerformance,
@ -743,12 +738,15 @@ export class PortfolioCalculatorNew {
.mul(order.unitPrice) .mul(order.unitPrice)
.mul(this.getFactor(order.type)); .mul(this.getFactor(order.type));
if ( if (i >= indexOfStartOrder && !initialValue) {
!initialValue && if (
order.itemType !== 'start' && i === indexOfStartOrder &&
order.itemType !== 'end' !valueOfInvestmentBeforeTransaction.eq(0)
) { ) {
initialValue = transactionInvestment; initialValue = valueOfInvestmentBeforeTransaction;
} else if (transactionInvestment.gt(0)) {
initialValue = transactionInvestment;
}
} }
fees = fees.plus(order.fee); fees = fees.plus(order.fee);

Loading…
Cancel
Save