From 9d6214e93ac40cd537f16ebdba01efc1a4d7abd9 Mon Sep 17 00:00:00 2001 From: Thomas Kaul <4159106+dtslvr@users.noreply.github.com> Date: Sat, 13 Jul 2024 18:24:03 +0200 Subject: [PATCH] Bugfix/fix fees calculation in portfolio summary (#3567) * Fix fees calculation * Update changelog --- CHANGELOG.md | 4 ++++ .../app/portfolio/calculator/portfolio-calculator.ts | 11 +++++++++-- ...ulator-baln-buy-and-sell-in-two-activities.spec.ts | 1 + .../portfolio-calculator-baln-buy-and-sell.spec.ts | 1 + .../twr/portfolio-calculator-baln-buy.spec.ts | 1 + ...o-calculator-btcusd-buy-and-sell-partially.spec.ts | 1 + .../calculator/twr/portfolio-calculator-fee.spec.ts | 1 + .../twr/portfolio-calculator-googl-buy.spec.ts | 3 ++- .../calculator/twr/portfolio-calculator-item.spec.ts | 1 + ...lio-calculator-novn-buy-and-sell-partially.spec.ts | 1 + .../portfolio-calculator-novn-buy-and-sell.spec.ts | 1 + .../portfolio/calculator/twr/portfolio-calculator.ts | 4 ++-- libs/common/src/lib/models/timeline-position.ts | 4 ++++ 13 files changed, 29 insertions(+), 5 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index d3694f580..1f7dfd026 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 - Improved the chart of the holdings tab on the home page (experimental) - Separated the icon purposes in the `site.webmanifest` +### Fixed + +- Fixed an issue in the portfolio summary with the currency conversion of fees + ## 2.95.0 - 2024-07-12 ### Added diff --git a/apps/api/src/app/portfolio/calculator/portfolio-calculator.ts b/apps/api/src/app/portfolio/calculator/portfolio-calculator.ts index e021eb2d4..ec56a247c 100644 --- a/apps/api/src/app/portfolio/calculator/portfolio-calculator.ts +++ b/apps/api/src/app/portfolio/calculator/portfolio-calculator.ts @@ -300,6 +300,12 @@ export abstract class PortfolioCalculator { const errors: ResponseError['errors'] = []; for (const item of lastTransactionPoint.items) { + const feeInBaseCurrency = item.fee.mul( + exchangeRatesByCurrency[`${item.currency}${this.currency}`]?.[ + lastTransactionPoint.date + ] + ); + const marketPriceInBaseCurrency = ( marketSymbolMap[endDateString]?.[item.symbol] ?? item.averagePrice ).mul( @@ -340,10 +346,11 @@ export abstract class PortfolioCalculator { hasAnySymbolMetricsErrors = hasAnySymbolMetricsErrors || hasErrors; positions.push({ - dividend: totalDividend, - dividendInBaseCurrency: totalDividendInBaseCurrency, + feeInBaseCurrency, timeWeightedInvestment, timeWeightedInvestmentWithCurrencyEffect, + dividend: totalDividend, + dividendInBaseCurrency: totalDividendInBaseCurrency, averagePrice: item.averagePrice, currency: item.currency, dataSource: item.dataSource, diff --git a/apps/api/src/app/portfolio/calculator/twr/portfolio-calculator-baln-buy-and-sell-in-two-activities.spec.ts b/apps/api/src/app/portfolio/calculator/twr/portfolio-calculator-baln-buy-and-sell-in-two-activities.spec.ts index 340f16b87..3eb166d2e 100644 --- a/apps/api/src/app/portfolio/calculator/twr/portfolio-calculator-baln-buy-and-sell-in-two-activities.spec.ts +++ b/apps/api/src/app/portfolio/calculator/twr/portfolio-calculator-baln-buy-and-sell-in-two-activities.spec.ts @@ -168,6 +168,7 @@ describe('PortfolioCalculator', () => { dividend: new Big('0'), dividendInBaseCurrency: new Big('0'), fee: new Big('3.2'), + feeInBaseCurrency: new Big('3.2'), firstBuyDate: '2021-11-22', grossPerformance: new Big('-12.6'), grossPerformancePercentage: new Big('-0.04408677396780965649'), diff --git a/apps/api/src/app/portfolio/calculator/twr/portfolio-calculator-baln-buy-and-sell.spec.ts b/apps/api/src/app/portfolio/calculator/twr/portfolio-calculator-baln-buy-and-sell.spec.ts index 53ebdf19f..a3adde84b 100644 --- a/apps/api/src/app/portfolio/calculator/twr/portfolio-calculator-baln-buy-and-sell.spec.ts +++ b/apps/api/src/app/portfolio/calculator/twr/portfolio-calculator-baln-buy-and-sell.spec.ts @@ -153,6 +153,7 @@ describe('PortfolioCalculator', () => { dividend: new Big('0'), dividendInBaseCurrency: new Big('0'), fee: new Big('3.2'), + feeInBaseCurrency: new Big('3.2'), firstBuyDate: '2021-11-22', grossPerformance: new Big('-12.6'), grossPerformancePercentage: new Big('-0.0440867739678096571'), diff --git a/apps/api/src/app/portfolio/calculator/twr/portfolio-calculator-baln-buy.spec.ts b/apps/api/src/app/portfolio/calculator/twr/portfolio-calculator-baln-buy.spec.ts index bab265887..6221e6240 100644 --- a/apps/api/src/app/portfolio/calculator/twr/portfolio-calculator-baln-buy.spec.ts +++ b/apps/api/src/app/portfolio/calculator/twr/portfolio-calculator-baln-buy.spec.ts @@ -138,6 +138,7 @@ describe('PortfolioCalculator', () => { dividend: new Big('0'), dividendInBaseCurrency: new Big('0'), fee: new Big('1.55'), + feeInBaseCurrency: new Big('1.55'), firstBuyDate: '2021-11-30', grossPerformance: new Big('24.6'), grossPerformancePercentage: new Big('0.09004392386530014641'), diff --git a/apps/api/src/app/portfolio/calculator/twr/portfolio-calculator-btcusd-buy-and-sell-partially.spec.ts b/apps/api/src/app/portfolio/calculator/twr/portfolio-calculator-btcusd-buy-and-sell-partially.spec.ts index eba5d4674..85d39220a 100644 --- a/apps/api/src/app/portfolio/calculator/twr/portfolio-calculator-btcusd-buy-and-sell-partially.spec.ts +++ b/apps/api/src/app/portfolio/calculator/twr/portfolio-calculator-btcusd-buy-and-sell-partially.spec.ts @@ -166,6 +166,7 @@ describe('PortfolioCalculator', () => { dividend: new Big('0'), dividendInBaseCurrency: new Big('0'), fee: new Big('0'), + feeInBaseCurrency: new Big('0'), firstBuyDate: '2015-01-01', grossPerformance: new Big('27172.74'), grossPerformancePercentage: new Big('42.41978276196153750666'), diff --git a/apps/api/src/app/portfolio/calculator/twr/portfolio-calculator-fee.spec.ts b/apps/api/src/app/portfolio/calculator/twr/portfolio-calculator-fee.spec.ts index 88d7adb71..7e9bbc26d 100644 --- a/apps/api/src/app/portfolio/calculator/twr/portfolio-calculator-fee.spec.ts +++ b/apps/api/src/app/portfolio/calculator/twr/portfolio-calculator-fee.spec.ts @@ -123,6 +123,7 @@ describe('PortfolioCalculator', () => { dividend: new Big('0'), dividendInBaseCurrency: new Big('0'), fee: new Big('49'), + feeInBaseCurrency: new Big('49'), firstBuyDate: '2021-09-01', grossPerformance: null, grossPerformancePercentage: null, diff --git a/apps/api/src/app/portfolio/calculator/twr/portfolio-calculator-googl-buy.spec.ts b/apps/api/src/app/portfolio/calculator/twr/portfolio-calculator-googl-buy.spec.ts index 690f1eb51..31182c8cb 100644 --- a/apps/api/src/app/portfolio/calculator/twr/portfolio-calculator-googl-buy.spec.ts +++ b/apps/api/src/app/portfolio/calculator/twr/portfolio-calculator-googl-buy.spec.ts @@ -151,6 +151,7 @@ describe('PortfolioCalculator', () => { dividend: new Big('0'), dividendInBaseCurrency: new Big('0'), fee: new Big('1'), + feeInBaseCurrency: new Big('0.9238'), firstBuyDate: '2023-01-03', grossPerformance: new Big('27.33'), grossPerformancePercentage: new Big('0.3066651705565529623'), @@ -177,7 +178,7 @@ describe('PortfolioCalculator', () => { valueInBaseCurrency: new Big('103.10483') } ], - totalFeesWithCurrencyEffect: new Big('1'), + totalFeesWithCurrencyEffect: new Big('0.9238'), totalInterestWithCurrencyEffect: new Big('0'), totalInvestment: new Big('89.12'), totalInvestmentWithCurrencyEffect: new Big('82.329056'), diff --git a/apps/api/src/app/portfolio/calculator/twr/portfolio-calculator-item.spec.ts b/apps/api/src/app/portfolio/calculator/twr/portfolio-calculator-item.spec.ts index 422d119b2..985dc5feb 100644 --- a/apps/api/src/app/portfolio/calculator/twr/portfolio-calculator-item.spec.ts +++ b/apps/api/src/app/portfolio/calculator/twr/portfolio-calculator-item.spec.ts @@ -123,6 +123,7 @@ describe('PortfolioCalculator', () => { dividend: new Big('0'), dividendInBaseCurrency: new Big('0'), fee: new Big('0'), + feeInBaseCurrency: new Big('0'), firstBuyDate: '2022-01-01', grossPerformance: null, grossPerformancePercentage: null, diff --git a/apps/api/src/app/portfolio/calculator/twr/portfolio-calculator-novn-buy-and-sell-partially.spec.ts b/apps/api/src/app/portfolio/calculator/twr/portfolio-calculator-novn-buy-and-sell-partially.spec.ts index f65d2ba61..72cb16a5f 100644 --- a/apps/api/src/app/portfolio/calculator/twr/portfolio-calculator-novn-buy-and-sell-partially.spec.ts +++ b/apps/api/src/app/portfolio/calculator/twr/portfolio-calculator-novn-buy-and-sell-partially.spec.ts @@ -153,6 +153,7 @@ describe('PortfolioCalculator', () => { dividend: new Big('0'), dividendInBaseCurrency: new Big('0'), fee: new Big('4.25'), + feeInBaseCurrency: new Big('4.25'), firstBuyDate: '2022-03-07', grossPerformance: new Big('21.93'), grossPerformancePercentage: new Big('0.15113417083448194384'), diff --git a/apps/api/src/app/portfolio/calculator/twr/portfolio-calculator-novn-buy-and-sell.spec.ts b/apps/api/src/app/portfolio/calculator/twr/portfolio-calculator-novn-buy-and-sell.spec.ts index 902f710ee..59cc13f0e 100644 --- a/apps/api/src/app/portfolio/calculator/twr/portfolio-calculator-novn-buy-and-sell.spec.ts +++ b/apps/api/src/app/portfolio/calculator/twr/portfolio-calculator-novn-buy-and-sell.spec.ts @@ -183,6 +183,7 @@ describe('PortfolioCalculator', () => { dividend: new Big('0'), dividendInBaseCurrency: new Big('0'), fee: new Big('0'), + feeInBaseCurrency: new Big('0'), firstBuyDate: '2022-03-07', grossPerformance: new Big('19.86'), grossPerformancePercentage: new Big('0.13100263852242744063'), 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 f8b62a940..e9f1f3fda 100644 --- a/apps/api/src/app/portfolio/calculator/twr/portfolio-calculator.ts +++ b/apps/api/src/app/portfolio/calculator/twr/portfolio-calculator.ts @@ -34,9 +34,9 @@ export class TWRPortfolioCalculator extends PortfolioCalculator { let totalTimeWeightedInvestmentWithCurrencyEffect = new Big(0); for (const currentPosition of positions) { - if (currentPosition.fee) { + if (currentPosition.feeInBaseCurrency) { totalFeesWithCurrencyEffect = totalFeesWithCurrencyEffect.plus( - currentPosition.fee + currentPosition.feeInBaseCurrency ); } diff --git a/libs/common/src/lib/models/timeline-position.ts b/libs/common/src/lib/models/timeline-position.ts index 412449590..545891464 100644 --- a/libs/common/src/lib/models/timeline-position.ts +++ b/libs/common/src/lib/models/timeline-position.ts @@ -24,6 +24,10 @@ export class TimelinePosition { @Type(() => Big) fee: Big; + @Transform(transformToBig, { toClassOnly: true }) + @Type(() => Big) + feeInBaseCurrency: Big; + firstBuyDate: string; @Transform(transformToBig, { toClassOnly: true })