diff --git a/apps/api/src/app/portfolio/calculator/twr/portfolio-calculator.ts b/apps/api/src/app/portfolio/calculator/twr/portfolio-calculator.ts index 950d9e4d3..6134a1020 100644 --- a/apps/api/src/app/portfolio/calculator/twr/portfolio-calculator.ts +++ b/apps/api/src/app/portfolio/calculator/twr/portfolio-calculator.ts @@ -137,6 +137,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; @@ -554,28 +556,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) @@ -597,19 +598,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; @@ -961,4 +964,67 @@ export class TWRPortfolioCalculator extends PortfolioCalculator { timeWeightedAverageInvestmentBetweenStartAndEndDateWithCurrencyEffect }; } + + 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 + }; + } }