Browse Source

Fix gross performance Percentage Calculations

pull/5027/head
Daniel Devaud 1 year ago
parent
commit
0faf4c8719
  1. 52
      apps/api/src/app/portfolio/portfolio-calculator.ts

52
apps/api/src/app/portfolio/portfolio-calculator.ts

@ -1153,10 +1153,7 @@ export class PortfolioCalculator {
const maxInvestmentValues: { [date: string]: Big } = {}; const maxInvestmentValues: { [date: string]: Big } = {};
let maxTotalInvestment = new Big(0); let maxTotalInvestment = new Big(0);
const netPerformanceValuesPercentage: { [date: string]: Big } = {}; const netPerformanceValuesPercentage: { [date: string]: Big } = {};
let initialValue: WithCurrencyEffect<Big> = { let initialValue;
Value: new Big(0),
WithCurrencyEffect: new Big(0)
};
let investmentAtStartDate; let investmentAtStartDate;
const investmentValuesAccumulated: WithCurrencyEffect<{ const investmentValuesAccumulated: WithCurrencyEffect<{
[date: string]: Big; [date: string]: Big;
@ -1507,16 +1504,13 @@ export class PortfolioCalculator {
) )
: new Big(0) : new Big(0)
}; };
const grossPerformancePercentage = { const grossPerformancePercentage = {
Value: this.calculateGrossPerformancePercentage( Value: timeWeightedAverageInvestmentBetweenStartAndEndDate.Value.gt(0)
averagePriceAtStartDate, ? totalGrossPerformance.Value.div(
averagePriceAtEndDate, timeWeightedAverageInvestmentBetweenStartAndEndDate.Value
orders, )
indexOfStartOrder, : new Big(0),
maxInvestmentBetweenStartAndEndDate,
totalGrossPerformance.Value,
unitPriceAtEndDate
),
WithCurrencyEffect: WithCurrencyEffect:
timeWeightedAverageInvestmentBetweenStartAndEndDate.WithCurrencyEffect.gt( timeWeightedAverageInvestmentBetweenStartAndEndDate.WithCurrencyEffect.gt(
0 0
@ -1992,32 +1986,6 @@ export class PortfolioCalculator {
}; };
} }
private calculateGrossPerformancePercentage(
averagePriceAtStartDate: Big,
averagePriceAtEndDate: Big,
orders: PortfolioOrderItem[],
indexOfStartOrder: number,
maxInvestmentBetweenStartAndEndDate: Big,
totalGrossPerformance: Big,
unitPriceAtEndDate: Big
) {
return PortfolioCalculator.CALCULATE_PERCENTAGE_PERFORMANCE_WITH_MAX_INVESTMENT ||
averagePriceAtStartDate.eq(0) ||
averagePriceAtEndDate.eq(0) ||
orders[indexOfStartOrder].unitPrice.eq(0)
? maxInvestmentBetweenStartAndEndDate.gt(0)
? totalGrossPerformance.div(maxInvestmentBetweenStartAndEndDate)
: 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 price stays constant
unitPriceAtEndDate
.div(averagePriceAtEndDate)
.div(orders[indexOfStartOrder].unitPrice.div(averagePriceAtStartDate))
.minus(1);
}
private calculateInvestmentSpecificMetrics( private calculateInvestmentSpecificMetrics(
averagePriceAtStartDate: Big, averagePriceAtStartDate: Big,
i: number, i: number,
@ -2101,12 +2069,12 @@ export class PortfolioCalculator {
initialValue = this.calculateInitialValue( initialValue = this.calculateInitialValue(
i, i,
indexOfStartOrder, indexOfStartOrder,
initialValue.Value, initialValue?.Value,
valueOfInvestmentBeforeTransaction, valueOfInvestmentBeforeTransaction,
transactionInvestment, transactionInvestment,
order, order,
marketSymbolMap, marketSymbolMap,
initialValue.WithCurrencyEffect initialValue?.WithCurrencyEffect
); );
fees.Value = fees.Value.plus(order.fee); fees.Value = fees.Value.plus(order.fee);
@ -2301,7 +2269,7 @@ export class PortfolioCalculator {
marketSymbolMap: { [date: string]: { [symbol: string]: Big } }, marketSymbolMap: { [date: string]: { [symbol: string]: Big } },
initialValueWithCurrencyEffect: Big initialValueWithCurrencyEffect: Big
) { ) {
if (i >= indexOfStartOrder && initialValue.gt(0)) { if (i >= indexOfStartOrder && !initialValue) {
if ( if (
i === indexOfStartOrder && i === indexOfStartOrder &&
!valueOfInvestmentBeforeTransaction.Value.eq(0) !valueOfInvestmentBeforeTransaction.Value.eq(0)

Loading…
Cancel
Save