diff --git a/CHANGELOG.md b/CHANGELOG.md index 2b82a3590..46cf47c0d 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -12,6 +12,10 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 - Migrated the `angular.json` to `project.json` files in the `Nx` workspace - Upgraded `Nx` from version `14.6.4` to `15.0.0` +### Fixed + +- Fixed the performance calculation including `SELL` activities with a significant performance gain + ## 1.205.2 - 16.10.2022 ### Changed diff --git a/apps/api/src/app/portfolio/portfolio-calculator.ts b/apps/api/src/app/portfolio/portfolio-calculator.ts index 90fb31df1..ea1f8e10c 100644 --- a/apps/api/src/app/portfolio/portfolio-calculator.ts +++ b/apps/api/src/app/portfolio/portfolio-calculator.ts @@ -1000,6 +1000,12 @@ export class PortfolioCalculator { for (let i = 0; i < orders.length; i += 1) { const order = orders[i]; + if (PortfolioCalculator.ENABLE_LOGGING) { + console.log(); + console.log(); + console.log(i + 1, order.type, order.itemType); + } + if (order.itemType === 'start') { // Take the unit price of the order as the market price if there are no // orders of this symbol before the start date @@ -1027,9 +1033,19 @@ export class PortfolioCalculator { valueAtStartDate = valueOfInvestmentBeforeTransaction; } - const transactionInvestment = order.quantity - .mul(order.unitPrice) - .mul(this.getFactor(order.type)); + const transactionInvestment = + order.type === 'BUY' + ? order.quantity.mul(order.unitPrice).mul(this.getFactor(order.type)) + : totalInvestment + .div(totalUnits) + .mul(order.quantity) + .mul(this.getFactor(order.type)); + + if (PortfolioCalculator.ENABLE_LOGGING) { + console.log('totalInvestment', totalInvestment.toNumber()); + console.log('order.quantity', order.quantity.toNumber()); + console.log('transactionInvestment', transactionInvestment.toNumber()); + } totalInvestment = totalInvestment.plus(transactionInvestment); @@ -1078,8 +1094,19 @@ export class PortfolioCalculator { ? new Big(0) : totalInvestmentWithGrossPerformanceFromSell.div(totalUnits); + if (PortfolioCalculator.ENABLE_LOGGING) { + console.log( + 'totalInvestmentWithGrossPerformanceFromSell', + totalInvestmentWithGrossPerformanceFromSell.toNumber() + ); + console.log( + 'grossPerformanceFromSells', + grossPerformanceFromSells.toNumber() + ); + } + const newGrossPerformance = valueOfInvestment - .minus(totalInvestmentWithGrossPerformanceFromSell) + .minus(totalInvestment) .plus(grossPerformanceFromSells); // if ( @@ -1141,7 +1168,15 @@ export class PortfolioCalculator { .minus(grossPerformanceAtStartDate) .minus(fees.minus(feesAtStartDate)); - investmentValues[order.date] = totalInvestment; + investmentValues[order.date] = maxTotalInvestment; + } + + if (PortfolioCalculator.ENABLE_LOGGING) { + console.log('totalInvestment', totalInvestment.toNumber()); + console.log( + 'totalGrossPerformance', + grossPerformance.minus(grossPerformanceAtStartDate).toNumber() + ); } if (i === indexOfEndOrder) { diff --git a/apps/client/src/app/components/portfolio-summary/portfolio-summary.component.html b/apps/client/src/app/components/portfolio-summary/portfolio-summary.component.html index 78e30675c..14a21813c 100644 --- a/apps/client/src/app/components/portfolio-summary/portfolio-summary.component.html +++ b/apps/client/src/app/components/portfolio-summary/portfolio-summary.component.html @@ -22,9 +22,6 @@
Sell
- -