Open Source Wealth Management Software. Angular + NestJS + Prisma + Nx + TypeScript 🤍
TypeScript angularetffinancefintechghostfoliohacktoberfestinvestingnestjsosspersonal-financeportfolioprismasoftwarestocktrackertradingtypescriptwealth-managementweb
Fix rounding of value per account and platform (#7594)
Thomas Kaul7b74cf5cea8a
diff --git a/CHANGELOG.md b/CHANGELOG.mdindex c571fdae70..d98e934248 100644--- a/CHANGELOG.md+++ b/CHANGELOG.md@@ -9,6 +9,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0### Fixed+- Fixed the allocation in the accounts tab of the holding detail dialog caused by floating-point rounding- Fixed the account aggregations in impersonation mode to be based on the impersonated user- Fixed the base currency of the activities in impersonation mode to be based on the impersonated user- Fixed the base currency of the dividends in impersonation mode to be based on the impersonated userdiff --git a/apps/api/src/app/portfolio/portfolio.service.spec.ts b/apps/api/src/app/portfolio/portfolio.service.spec.tsindex eed3a27cb7..c5e0a83329 100644--- a/apps/api/src/app/portfolio/portfolio.service.spec.ts+++ b/apps/api/src/app/portfolio/portfolio.service.spec.ts@@ -510,5 +510,43 @@ describe('PortfolioService', () => {expect(accounts[UNKNOWN_KEY]).toBeUndefined();expect(platforms[UNKNOWN_KEY]).toBeUndefined();});++ it('should not accumulate rounding errors of activities cancelling each other out', async () => {+ const { accounts, platforms } = await getValueOfAccountsAndPlatforms({+ activities: [+ {+ account,+ accountId: account.id,+ assetProfile: { symbol: 'AAPL' },+ quantity: 0.1,+ type: 'BUY'+ },+ {+ account,+ accountId: account.id,+ assetProfile: { symbol: 'AAPL' },+ quantity: 0.2,+ type: 'BUY'+ },+ {+ account,+ accountId: account.id,+ assetProfile: { symbol: 'AAPL' },+ quantity: 0.3,+ type: 'SELL'+ }+ ],+ filters: [],+ portfolioItemsNow: {+ AAPL: { marketPriceInBaseCurrency: 1234.5678 }+ },+ userCurrency: 'USD',+ userId: userDummyData.id+ });++ // 100 (balance) + 0 (activities)+ expect(accounts[account.id].valueInBaseCurrency).toBe(100);+ expect(platforms[account.platformId].valueInBaseCurrency).toBe(100);+ });});});diff --git a/apps/api/src/app/portfolio/portfolio.service.ts b/apps/api/src/app/portfolio/portfolio.service.tsindex 88f6750080..2c77017844 100644--- a/apps/api/src/app/portfolio/portfolio.service.ts+++ b/apps/api/src/app/portfolio/portfolio.service.ts@@ -2270,38 +2270,54 @@ export class PortfolioService {}}- for (const { account, assetProfile, quantity, type } of ordersByAccount) {- const currentValueOfSymbolInBaseCurrency =- getFactor(type) *- quantity *- (portfolioItemsNow[assetProfile.symbol]?.marketPriceInBaseCurrency ??- 0);-- if (accounts[account?.id || UNKNOWN_KEY]?.valueInBaseCurrency) {- accounts[account?.id || UNKNOWN_KEY].valueInBaseCurrency +=- currentValueOfSymbolInBaseCurrency;- } else {- accounts[account?.id || UNKNOWN_KEY] = {- balance: 0,- currency: account?.currency,- name: account?.name,- valueInBaseCurrency: currentValueOfSymbolInBaseCurrency- };- }+ if (ordersByAccount.length === 0) {+ continue;+ }- if (- platforms[account?.platformId || UNKNOWN_KEY]?.valueInBaseCurrency- ) {- platforms[account?.platformId || UNKNOWN_KEY].valueInBaseCurrency +=- currentValueOfSymbolInBaseCurrency;- } else {- platforms[account?.platformId || UNKNOWN_KEY] = {- balance: 0,- currency: account?.currency,- name: account?.platform?.name,- valueInBaseCurrency: currentValueOfSymbolInBaseCurrency- };- }+ let valueOfAccountInBaseCurrency = new Big(0);++ for (const { assetProfile, quantity, type } of ordersByAccount) {+ valueOfAccountInBaseCurrency = valueOfAccountInBaseCurrency.plus(+ new Big(quantity)+ .mul(getFactor(type))+ .mul(+ portfolioItemsNow[assetProfile.symbol]+ ?.marketPriceInBaseCurrency ?? 0+ )+ );+ }++ const currentAccountId = account?.id || UNKNOWN_KEY;+ const currentPlatformId = account?.platformId || UNKNOWN_KEY;++ if (accounts[currentAccountId]) {+ accounts[currentAccountId].valueInBaseCurrency = new Big(+ accounts[currentAccountId].valueInBaseCurrency+ )+ .plus(valueOfAccountInBaseCurrency)+ .toNumber();+ } else {+ accounts[currentAccountId] = {+ balance: 0,+ currency: account?.currency,+ name: account?.name,+ valueInBaseCurrency: valueOfAccountInBaseCurrency.toNumber()+ };+ }++ if (platforms[currentPlatformId]) {+ platforms[currentPlatformId].valueInBaseCurrency = new Big(+ platforms[currentPlatformId].valueInBaseCurrency+ )+ .plus(valueOfAccountInBaseCurrency)+ .toNumber();+ } else {+ platforms[currentPlatformId] = {+ balance: 0,+ currency: account?.currency,+ name: account?.platform?.name,+ valueInBaseCurrency: valueOfAccountInBaseCurrency.toNumber()+ };}}