Skip to content

Open Source Wealth Management Software. Angular + NestJS + Prisma + Nx + TypeScript 🤍

Bugfix/improve performance calculation for sell activitities (#1388)

Thomas Kaul048900d01b0e
diff --git a/CHANGELOG.md b/CHANGELOG.md
index 2b82a35904..46cf47c0dc 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 90fb31df16..ea1f8e10c1 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 78e30675cf..14a21813c4 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 @@
<div class="row px-3 py-1">
<div class="d-flex flex-grow-1" i18n>Sell</div>
<div class="d-flex justify-content-end">
- <span *ngIf="summary?.totalSell || summary?.totalSell === 0" class="mr-1"
- >-</span
- >
<gf-value
class="justify-content-end"
[currency]="baseCurrency"