Browse Source

Fix percentage performance issue with recent transactions

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

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

@ -33,7 +33,10 @@ import { TransactionPointSymbol } from './interfaces/transaction-point-symbol.in
import { TransactionPoint } from './interfaces/transaction-point.interface'; import { TransactionPoint } from './interfaces/transaction-point.interface';
export class PortfolioCalculatorNew { export class PortfolioCalculatorNew {
private static readonly ENABLE_LOGGING = false; private static readonly CALCULATE_PERCENTAGE_PERFORMANCE_WITH_MAX_INVESTMENT =
true;
private static readonly ENABLE_LOGGING = true;
private currency: string; private currency: string;
private currentRateService: CurrentRateService; private currentRateService: CurrentRateService;
@ -767,7 +770,7 @@ export class PortfolioCalculatorNew {
totalInvestment = totalInvestment.plus(transactionInvestment); totalInvestment = totalInvestment.plus(transactionInvestment);
if (totalInvestment.gt(maxTotalInvestment)) { if (i >= indexOfStartOrder && totalInvestment.gt(maxTotalInvestment)) {
maxTotalInvestment = totalInvestment; maxTotalInvestment = totalInvestment;
} }
@ -886,11 +889,17 @@ export class PortfolioCalculatorNew {
.minus(fees.minus(feesAtStartDate)); .minus(fees.minus(feesAtStartDate));
const grossPerformancePercentage = const grossPerformancePercentage =
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)
? totalGrossPerformance.div(maxTotalInvestment) ? maxTotalInvestment.gt(0)
: unitPriceAtEndDate ? totalGrossPerformance.div(maxTotalInvestment)
: new Big(0)
: // This formula has the issue that buying more units with a price
// lower than the average buying price results in a positive
// performance even if the market pricse stays constant
unitPriceAtEndDate
.div(averagePriceAtEndDate) .div(averagePriceAtEndDate)
.div( .div(
orders[indexOfStartOrder].unitPrice.div(averagePriceAtStartDate) orders[indexOfStartOrder].unitPrice.div(averagePriceAtStartDate)
@ -902,11 +911,17 @@ export class PortfolioCalculatorNew {
: new Big(0); : new Big(0);
const netPerformancePercentage = const netPerformancePercentage =
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)
? totalNetPerformance.div(maxTotalInvestment) ? maxTotalInvestment.gt(0)
: unitPriceAtEndDate ? totalNetPerformance.div(maxTotalInvestment)
: new Big(0)
: // This formula has the issue that buying more units with a price
// lower than the average buying price results in a positive
// performance even if the market pricse stays constant
unitPriceAtEndDate
.minus(feesPerUnit) .minus(feesPerUnit)
.div(averagePriceAtEndDate) .div(averagePriceAtEndDate)
.div( .div(

Loading…
Cancel
Save