Browse Source

Improve percentage performance algorithm

pull/750/head
Reto Kaul 3 years ago
parent
commit
f7d78a9a73
  1. 17
      apps/api/src/app/portfolio/portfolio-calculator-new.ts

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

@ -691,6 +691,7 @@ export class PortfolioCalculatorNew {
let grossPerformanceAtStartDate = new Big(0); let grossPerformanceAtStartDate = new Big(0);
let grossPerformanceFromSells = new Big(0); let grossPerformanceFromSells = new Big(0);
let initialValue: Big; let initialValue: Big;
let investmentAtStartDate: Big;
let lastAveragePrice = new Big(0); let lastAveragePrice = new Big(0);
let lastTransactionInvestment = new Big(0); let lastTransactionInvestment = new Big(0);
let lastValueOfInvestmentBeforeTransaction = new Big(0); let lastValueOfInvestmentBeforeTransaction = new Big(0);
@ -773,6 +774,10 @@ export class PortfolioCalculatorNew {
averagePriceAtStartDate = totalInvestment.div(totalUnits); averagePriceAtStartDate = totalInvestment.div(totalUnits);
} }
if (!investmentAtStartDate && i >= indexOfStartOrder) {
investmentAtStartDate = totalInvestment ?? new Big(0);
}
const valueOfInvestmentBeforeTransaction = totalUnits.mul( const valueOfInvestmentBeforeTransaction = totalUnits.mul(
order.unitPrice order.unitPrice
); );
@ -901,13 +906,17 @@ export class PortfolioCalculatorNew {
.minus(grossPerformanceAtStartDate) .minus(grossPerformanceAtStartDate)
.minus(fees.minus(feesAtStartDate)); .minus(fees.minus(feesAtStartDate));
const maxInvestmentBetweenStartAndEndDate = initialValue.plus(
maxTotalInvestment.minus(investmentAtStartDate)
);
const grossPerformancePercentage = const grossPerformancePercentage =
PortfolioCalculatorNew.CALCULATE_PERCENTAGE_PERFORMANCE_WITH_MAX_INVESTMENT || PortfolioCalculatorNew.CALCULATE_PERCENTAGE_PERFORMANCE_WITH_MAX_INVESTMENT ||
averagePriceAtStartDate.eq(0) || averagePriceAtStartDate.eq(0) ||
averagePriceAtEndDate.eq(0) || averagePriceAtEndDate.eq(0) ||
orders[indexOfStartOrder].unitPrice.eq(0) orders[indexOfStartOrder].unitPrice.eq(0)
? maxTotalInvestment.gt(0) ? maxInvestmentBetweenStartAndEndDate.gt(0)
? totalGrossPerformance.div(maxTotalInvestment) ? totalGrossPerformance.div(maxInvestmentBetweenStartAndEndDate)
: new Big(0) : new Big(0)
: // This formula has the issue that buying more units with a price : // This formula has the issue that buying more units with a price
// lower than the average buying price results in a positive // lower than the average buying price results in a positive
@ -928,8 +937,8 @@ export class PortfolioCalculatorNew {
averagePriceAtStartDate.eq(0) || averagePriceAtStartDate.eq(0) ||
averagePriceAtEndDate.eq(0) || averagePriceAtEndDate.eq(0) ||
orders[indexOfStartOrder].unitPrice.eq(0) orders[indexOfStartOrder].unitPrice.eq(0)
? maxTotalInvestment.gt(0) ? maxInvestmentBetweenStartAndEndDate.gt(0)
? totalNetPerformance.div(maxTotalInvestment) ? totalNetPerformance.div(maxInvestmentBetweenStartAndEndDate)
: new Big(0) : new Big(0)
: // This formula has the issue that buying more units with a price : // This formula has the issue that buying more units with a price
// lower than the average buying price results in a positive // lower than the average buying price results in a positive

Loading…
Cancel
Save