Browse Source

Bugfix/value of accounts filtered by asset class (#8113)

* Fix value of accounts filtered by asset class

* Update changelog
pull/8117/head
Thomas Kaul 4 days ago
committed by GitHub
parent
commit
52a1128d7b
No known key found for this signature in database GPG Key ID: B5690EEEBB952194
  1. 2
      CHANGELOG.md
  2. 22
      apps/api/src/app/activities/activities.service.ts
  3. 2
      apps/api/src/app/endpoints/mcp/mcp.controller.ts
  4. 83
      apps/api/src/app/portfolio/portfolio.service.spec.ts
  5. 23
      apps/api/src/app/portfolio/portfolio.service.ts

2
CHANGELOG.md

@ -17,6 +17,8 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
- Fixed the _Copy AI prompt to clipboard_ actions on the analysis page in impersonation mode to be based on the impersonated user (experimental) - Fixed the _Copy AI prompt to clipboard_ actions on the analysis page in impersonation mode to be based on the impersonated user (experimental)
- Fixed the start date of the date ranges for instances in time zones with a negative UTC offset - Fixed the start date of the date ranges for instances in time zones with a negative UTC offset
- Fixed the value of the accounts by excluding the cash balance when filtering by an asset class other than liquidity on the accounts page and the allocations page
- Fixed the value of the accounts by excluding the cash balance when filtering by an asset class other than liquidity in the server of the Model Context Protocol (MCP) (experimental)
## 3.83.0 - 2026-10-10 ## 3.83.0 - 2026-10-10

22
apps/api/src/app/activities/activities.service.ts

@ -83,7 +83,6 @@ export class ActivitiesService {
public areCashActivitiesExcludedByFilters(filters: Filter[] = []) { public areCashActivitiesExcludedByFilters(filters: Filter[] = []) {
const { const {
ASSET_CLASS: filtersByAssetClass = [],
DATA_SOURCE: [filterByDataSource] = [], DATA_SOURCE: [filterByDataSource] = [],
SYMBOL: [filterBySymbol] = [], SYMBOL: [filterBySymbol] = [],
TAG: filtersByTag = [] TAG: filtersByTag = []
@ -92,10 +91,7 @@ export class ActivitiesService {
}); });
const isFilteredByAssetClassOtherThanLiquidity = const isFilteredByAssetClassOtherThanLiquidity =
filtersByAssetClass.length > 0 && this.isFilteredByAssetClassOtherThanLiquidity(filters);
!filtersByAssetClass.some(({ id }) => {
return id === AssetClass.LIQUIDITY;
});
const isFilteredByAssetProfile = !!(filterByDataSource || filterBySymbol); const isFilteredByAssetProfile = !!(filterByDataSource || filterBySymbol);
const isFilteredByTag = filtersByTag.length > 0; const isFilteredByTag = filtersByTag.length > 0;
@ -875,6 +871,22 @@ export class ActivitiesService {
}); });
} }
public isFilteredByAssetClassOtherThanLiquidity(filters: Filter[] = []) {
const { ASSET_CLASS: filtersByAssetClass = [] } = groupBy(
filters,
({ type }) => {
return type;
}
);
return (
filtersByAssetClass.length > 0 &&
!filtersByAssetClass.some(({ id }) => {
return id === AssetClass.LIQUIDITY;
})
);
}
public async order( public async order(
orderWhereUniqueInput: Prisma.OrderWhereUniqueInput orderWhereUniqueInput: Prisma.OrderWhereUniqueInput
): Promise<Order | null> { ): Promise<Order | null> {

2
apps/api/src/app/endpoints/mcp/mcp.controller.ts

@ -38,7 +38,7 @@ export class GhostfolioMcpController {
', ' ', '
)}. If the access reads the monetary values, these columns are given in addition: ${PortfolioTableService.getAccountsTableValueColumnNames().join( )}. If the access reads the monetary values, these columns are given in addition: ${PortfolioTableService.getAccountsTableValueColumnNames().join(
', ' ', '
)}. The allocation in percentage is relative to the accounts of the result, hence the parameters change it. The parameters change the value in base currency as well. With the holding parameter, it is the value of the holding in the account without the cash balance. Without the holding parameter, it includes the full cash balance of the account, also with the assetClasses parameter. The balance is always the full cash balance of the account in the currency of the account.`, )}. The allocation in percentage is relative to the accounts of the result, hence the parameters change it. The parameters change the value in base currency as well. With the holding parameter or with asset classes which do not include LIQUIDITY, it is the value of the matching holdings in the account without the cash balance. Otherwise, it includes the full cash balance of the account. The balance is always the full cash balance of the account in the currency of the account.`,
name: 'get-accounts', name: 'get-accounts',
parameters: GET_ACCOUNTS_PARAMETERS parameters: GET_ACCOUNTS_PARAMETERS
}) })

83
apps/api/src/app/portfolio/portfolio.service.spec.ts

@ -1454,7 +1454,11 @@ describe('PortfolioService', () => {
getValueOfAccountsAndPlatforms: (aArgs: object) => Promise<{ getValueOfAccountsAndPlatforms: (aArgs: object) => Promise<{
accounts: Record< accounts: Record<
string, string,
{ quantity?: number; valueInBaseCurrency: number } {
balance: number;
quantity?: number;
valueInBaseCurrency: number;
}
>; >;
platforms: Record<string, { valueInBaseCurrency: number }>; platforms: Record<string, { valueInBaseCurrency: number }>;
}>; }>;
@ -1675,6 +1679,83 @@ describe('PortfolioService', () => {
expect(platforms[account.platformId].valueInBaseCurrency).toBe(10); expect(platforms[account.platformId].valueInBaseCurrency).toBe(10);
}); });
it('should exclude the cash balance if the activities are filtered by an asset class other than liquidity', async () => {
const { accounts, platforms } = await getValueOfAccountsAndPlatforms({
activities: [
{
account,
accountId: account.id,
assetProfile: { dataSource: DataSource.YAHOO, symbol: 'AAPL' },
quantity: 1,
type: 'BUY'
}
],
filters: [{ id: AssetClass.EQUITY, type: 'ASSET_CLASS' }],
portfolioItemsNow: {
'YAHOO-AAPL': { marketPriceInBaseCurrency: 10 }
},
userCurrency: 'USD',
userId: userDummyData.id
});
expect(accounts[account.id].balance).toBe(100);
// 1 * 10 (activity), without the balance of 100
expect(accounts[account.id].valueInBaseCurrency).toBe(10);
expect(platforms[account.platformId].valueInBaseCurrency).toBe(10);
});
it('should include the cash balance if the activities are filtered by asset classes including liquidity', async () => {
const { accounts, platforms } = await getValueOfAccountsAndPlatforms({
activities: [
{
account,
accountId: account.id,
assetProfile: { dataSource: DataSource.YAHOO, symbol: 'AAPL' },
quantity: 1,
type: 'BUY'
}
],
filters: [
{ id: AssetClass.EQUITY, type: 'ASSET_CLASS' },
{ id: AssetClass.LIQUIDITY, type: 'ASSET_CLASS' }
],
portfolioItemsNow: {
'YAHOO-AAPL': { marketPriceInBaseCurrency: 10 }
},
userCurrency: 'USD',
userId: userDummyData.id
});
// 100 (balance) + 1 * 10 (activity)
expect(accounts[account.id].valueInBaseCurrency).toBe(110);
expect(platforms[account.platformId].valueInBaseCurrency).toBe(110);
});
it('should include the cash balance if the activities are filtered by a tag', async () => {
const { accounts, platforms } = await getValueOfAccountsAndPlatforms({
activities: [
{
account,
accountId: account.id,
assetProfile: { dataSource: DataSource.YAHOO, symbol: 'AAPL' },
quantity: 1,
type: 'BUY'
}
],
filters: [{ id: randomUUID(), type: 'TAG' }],
portfolioItemsNow: {
'YAHOO-AAPL': { marketPriceInBaseCurrency: 10 }
},
userCurrency: 'USD',
userId: userDummyData.id
});
// 100 (balance) + 1 * 10 (activity)
expect(accounts[account.id].valueInBaseCurrency).toBe(110);
expect(platforms[account.platformId].valueInBaseCurrency).toBe(110);
});
it('should not accumulate rounding errors of the balances of accounts sharing a platform', async () => { it('should not accumulate rounding errors of the balances of accounts sharing a platform', async () => {
const platformId = randomUUID(); const platformId = randomUUID();

23
apps/api/src/app/portfolio/portfolio.service.ts

@ -2438,6 +2438,9 @@ export class PortfolioService {
return type; return type;
}); });
const isFilteredByAssetClassOtherThanLiquidity =
this.activitiesService.isFilteredByAssetClassOtherThanLiquidity(filters);
let currentAccounts: (AccountWithBalance & { let currentAccounts: (AccountWithBalance & {
Order?: Order[]; Order?: Order[];
platform?: Platform; platform?: Platform;
@ -2485,15 +2488,17 @@ export class PortfolioService {
}); });
if (account) { if (account) {
// The cash balance is not part of a holding and would distort the value // The cash balance is not part of a holding or of an asset class other
// and thus the allocation per account and platform // than liquidity and would distort the value and thus the allocation
const balanceInBaseCurrency = filterBySymbol // per account and platform
? 0 const balanceInBaseCurrency =
: this.exchangeRateDataService.toCurrency( filterBySymbol || isFilteredByAssetClassOtherThanLiquidity
account.balance, ? 0
account.currency, : this.exchangeRateDataService.toCurrency(
userCurrency account.balance,
); account.currency,
userCurrency
);
accounts[currentAccountId] = { accounts[currentAccountId] = {
balance: account.balance, balance: account.balance,

Loading…
Cancel
Save