Browse Source

Add realized dividends to gross performance

- Dividends will  be handled like sell-orders performancewise
This might be one approach to solve issue #3233
pull/5027/head
Dan 9 months ago
parent
commit
6eac5cdd2d
  1. 118
      apps/api/src/app/portfolio/calculator/twr/portfolio-calculator.ts

118
apps/api/src/app/portfolio/calculator/twr/portfolio-calculator.ts

@ -142,6 +142,8 @@ export class TWRPortfolioCalculator extends PortfolioCalculator {
let grossPerformanceAtStartDateWithCurrencyEffect = new Big(0);
let grossPerformanceFromSells = new Big(0);
let grossPerformanceFromSellsWithCurrencyEffect = new Big(0);
let grossPerformanceFromDividends = new Big(0);
let grossPerformanceFromDividendsWithCurrencyEffect = new Big(0);
let initialValue: Big;
let initialValueWithCurrencyEffect: Big;
let investmentAtStartDate: Big;
@ -565,28 +567,27 @@ export class TWRPortfolioCalculator extends PortfolioCalculator {
order.unitPriceInBaseCurrencyWithCurrencyEffect
);
const grossPerformanceFromSell =
order.type === 'SELL'
? order.unitPriceInBaseCurrency
.minus(lastAveragePrice)
.mul(order.quantity)
: new Big(0);
const grossPerformanceFromSellWithCurrencyEffect =
order.type === 'SELL'
? order.unitPriceInBaseCurrencyWithCurrencyEffect
.minus(lastAveragePriceWithCurrencyEffect)
.mul(order.quantity)
: new Big(0);
grossPerformanceFromSells = grossPerformanceFromSells.plus(
grossPerformanceFromSell
);
grossPerformanceFromSellsWithCurrencyEffect =
grossPerformanceFromSellsWithCurrencyEffect.plus(
grossPerformanceFromSellWithCurrencyEffect
);
({
grossPerformanceFromSells,
grossPerformanceFromSellsWithCurrencyEffect
} = this.handleSellOrder(
order,
lastAveragePrice,
lastAveragePriceWithCurrencyEffect,
grossPerformanceFromSells,
grossPerformanceFromSellsWithCurrencyEffect
));
({
grossPerformanceFromDividends,
grossPerformanceFromDividendsWithCurrencyEffect
} = this.handleDividend(
order,
grossPerformanceFromDividends,
grossPerformanceFromDividendsWithCurrencyEffect,
currentExchangeRate,
exchangeRateAtOrderDate
));
lastAveragePrice = totalQuantityFromBuyTransactions.eq(0)
? new Big(0)
@ -608,19 +609,21 @@ export class TWRPortfolioCalculator extends PortfolioCalculator {
grossPerformanceFromSells.toNumber()
);
console.log(
'grossPerformanceFromSellWithCurrencyEffect',
grossPerformanceFromSellWithCurrencyEffect.toNumber()
'grossPerformanceFromSellsWithCurrencyEffect',
grossPerformanceFromSellsWithCurrencyEffect.toNumber()
);
}
const newGrossPerformance = valueOfInvestment
.minus(totalInvestment)
.plus(grossPerformanceFromSells);
.plus(grossPerformanceFromSells)
.plus(grossPerformanceFromDividends);
const newGrossPerformanceWithCurrencyEffect =
valueOfInvestmentWithCurrencyEffect
.minus(totalInvestmentWithCurrencyEffect)
.plus(grossPerformanceFromSellsWithCurrencyEffect);
.plus(grossPerformanceFromSellsWithCurrencyEffect)
.plus(grossPerformanceFromDividendsWithCurrencyEffect);
grossPerformance = newGrossPerformance;
@ -974,4 +977,67 @@ export class TWRPortfolioCalculator extends PortfolioCalculator {
unitPrices: {}
};
}
private handleSellOrder(
order: PortfolioOrderItem,
lastAveragePrice,
lastAveragePriceWithCurrencyEffect,
grossPerformanceFromSells,
grossPerformanceFromSellsWithCurrencyEffect
) {
if (order.type === 'SELL') {
const grossPerformanceFromSell = order.unitPriceInBaseCurrency
.minus(lastAveragePrice)
.mul(order.quantity);
const grossPerformanceFromSellWithCurrencyEffect =
order.unitPriceInBaseCurrencyWithCurrencyEffect
.minus(lastAveragePriceWithCurrencyEffect)
.mul(order.quantity);
grossPerformanceFromSells = grossPerformanceFromSells.plus(
grossPerformanceFromSell
);
grossPerformanceFromSellsWithCurrencyEffect =
grossPerformanceFromSellsWithCurrencyEffect.plus(
grossPerformanceFromSellWithCurrencyEffect
);
}
return {
grossPerformanceFromSells,
grossPerformanceFromSellsWithCurrencyEffect
};
}
private handleDividend(
order: PortfolioOrderItem,
grossPerformanceFromDividends,
grossPerformanceFromDividendsWithCurrencyEffect,
currentExchangeRate: number,
exchangeRateAtDateOfOrder: number
) {
if (order.type === 'DIVIDEND') {
const grossPerformanceFromDividend = order.unitPrice
.mul(currentExchangeRate)
.mul(order.quantity);
const grossPerformanceFromDividendWithCurrencyEffect = order.unitPrice
.mul(exchangeRateAtDateOfOrder)
.mul(order.quantity);
grossPerformanceFromDividends = grossPerformanceFromDividends.plus(
grossPerformanceFromDividend
);
grossPerformanceFromDividendsWithCurrencyEffect =
grossPerformanceFromDividendsWithCurrencyEffect.plus(
grossPerformanceFromDividendWithCurrencyEffect
);
}
return {
grossPerformanceFromDividends,
grossPerformanceFromDividendsWithCurrencyEffect
};
}
}

Loading…
Cancel
Save