Browse Source

Merge branch 'dockerpush' into main

pull/5027/head
dandevaud 3 months ago
committed by GitHub
parent
commit
932cf16244
No known key found for this signature in database GPG Key ID: B5690EEEBB952194
  1. 1
      .admin.cred
  2. 25
      .env.dev
  3. 3
      .env.example
  4. 47
      .github/workflows/docker-image-branch.yml
  5. 47
      .github/workflows/docker-image-dev.yml
  6. 5
      .github/workflows/docker-image.yml
  7. 47
      .github/workflows/main.yml
  8. 0
      .husky/pre-push
  9. 89
      CHANGELOG.md
  10. 1
      Dockerfile
  11. 11
      apps/api/project.json
  12. 1
      apps/api/src/app/account-balance/account-balance.service.ts
  13. 2
      apps/api/src/app/account/account.service.ts
  14. 46
      apps/api/src/app/admin/admin.controller.ts
  15. 2
      apps/api/src/app/admin/admin.module.ts
  16. 19
      apps/api/src/app/admin/admin.service.ts
  17. 16
      apps/api/src/app/admin/update-asset-profile.dto.ts
  18. 13
      apps/api/src/app/import/import.service.ts
  19. 114
      apps/api/src/app/order/order.service.ts
  20. 595
      apps/api/src/app/portfolio/calculator/constantPortfolioReturn/portfolio-calculator.ts
  21. 22
      apps/api/src/app/portfolio/calculator/portfolio-calculator.factory.ts
  22. 222
      apps/api/src/app/portfolio/calculator/portfolio-calculator.ts
  23. 4
      apps/api/src/app/portfolio/calculator/roai/portfolio-calculator-baln-buy-and-sell-in-two-activities.spec.ts
  24. 4
      apps/api/src/app/portfolio/calculator/roai/portfolio-calculator-baln-buy-and-sell.spec.ts
  25. 34
      apps/api/src/app/portfolio/calculator/roai/portfolio-calculator-googl-buy.spec.ts
  26. 4
      apps/api/src/app/portfolio/calculator/roai/portfolio-calculator-novn-buy-and-sell-partially.spec.ts
  27. 9
      apps/api/src/app/portfolio/calculator/roai/portfolio-calculator.ts
  28. 1
      apps/api/src/app/portfolio/interfaces/portfolio-holding-detail.interface.ts
  29. 5
      apps/api/src/app/portfolio/interfaces/portfolio-order-item.interface.ts
  30. 32
      apps/api/src/app/portfolio/portfolio.controller.ts
  31. 89
      apps/api/src/app/portfolio/portfolio.service.ts
  32. 82
      apps/api/src/app/tag/tag.service.ts
  33. 3
      apps/api/src/app/user/update-user-setting.dto.ts
  34. 6
      apps/api/src/app/user/user.controller.ts
  35. 25
      apps/api/src/helper/dateQueryHelper.ts
  36. 1
      apps/api/src/helper/portfolio.helper.ts
  37. 2
      apps/api/src/interceptors/performance-logging/performance-logging.interceptor.ts
  38. 7
      apps/api/src/services/data-provider/data-enhancer/trackinsight/trackinsight.service.ts
  39. 25
      apps/api/src/services/data-provider/data-provider.service.ts
  40. 32
      apps/api/src/services/data-provider/manual/manual.service.ts
  41. 9
      apps/api/src/services/market-data/market-data.service.ts
  42. 159
      apps/api/src/services/queues/data-gathering/data-gathering.processor.ts
  43. 49
      apps/api/src/services/queues/data-gathering/data-gathering.service.ts
  44. 12
      apps/api/src/services/symbol-profile/symbol-profile-overwrite.module.ts
  45. 69
      apps/api/src/services/symbol-profile/symbol-profile-overwrite.service.ts
  46. 56
      apps/api/src/services/symbol-profile/symbol-profile.service.ts
  47. 7
      apps/api/src/services/tag/tag.service.ts
  48. 6
      apps/client-e2e/project.json
  49. 50
      apps/client/project.json
  50. 11
      apps/client/src/app/components/admin-market-data/admin-market-data.component.ts
  51. 3
      apps/client/src/app/components/admin-market-data/admin-market-data.html
  52. 65
      apps/client/src/app/components/admin-market-data/asset-profile-dialog/asset-profile-dialog.component.ts
  53. 44
      apps/client/src/app/components/admin-market-data/asset-profile-dialog/asset-profile-dialog.html
  54. 4
      apps/client/src/app/components/admin-market-data/asset-profile-dialog/asset-profile-dialog.module.ts
  55. 13
      apps/client/src/app/components/admin-tag/admin-tag.component.html
  56. 8
      apps/client/src/app/components/admin-tag/admin-tag.component.ts
  57. 1
      apps/client/src/app/components/benchmark-comparator/benchmark-comparator.component.scss
  58. 10
      apps/client/src/app/components/header/header.component.ts
  59. 30
      apps/client/src/app/components/holding-detail-dialog/holding-detail-dialog.component.ts
  60. 54
      apps/client/src/app/components/holding-detail-dialog/holding-detail-dialog.html
  61. 3
      apps/client/src/app/components/toggle/toggle.component.ts
  62. 6
      apps/client/src/app/pages/portfolio/activities/create-or-update-activity-dialog/create-or-update-activity-dialog.component.ts
  63. 13
      apps/client/src/app/pages/portfolio/activities/create-or-update-activity-dialog/create-or-update-activity-dialog.html
  64. 48
      apps/client/src/app/pages/portfolio/allocations/allocations-page.component.ts
  65. 18
      apps/client/src/app/pages/portfolio/allocations/allocations-page.html
  66. 34
      apps/client/src/app/pages/portfolio/analysis/analysis-page.component.ts
  67. 2
      apps/client/src/app/pages/portfolio/analysis/analysis-page.html
  68. 19
      apps/client/src/app/services/admin.service.ts
  69. 11
      apps/client/src/app/services/data.service.ts
  70. 2
      apps/client/src/app/services/import-activities.service.ts
  71. 6
      apps/client/src/app/services/user/user.service.ts
  72. 6
      apps/ui-e2e/project.json
  73. 10
      libs/common/src/lib/calculation-helper.ts
  74. 46
      libs/common/src/lib/chunkhelper.ts
  75. 12
      libs/common/src/lib/config.ts
  76. 3
      libs/common/src/lib/interfaces/admin-market-data.interface.ts
  77. 3
      libs/common/src/lib/interfaces/enhanced-symbol-profile.interface.ts
  78. 2
      libs/common/src/lib/interfaces/historical-data-item.interface.ts
  79. 3
      libs/common/src/lib/interfaces/symbol-metrics.interface.ts
  80. 9
      libs/common/src/lib/types/date-range.type.ts
  81. 51
      libs/ui/src/lib/activities-table/activities-table.component.scss
  82. 38
      libs/ui/src/lib/activity-type/activity-type.component.html
  83. 15
      libs/ui/src/lib/assistant/assistant.component.ts
  84. 12
      libs/ui/src/lib/assistant/assistant.html
  85. 89
      libs/ui/src/lib/holdings-table/holdings-table.component.html
  86. 20
      libs/ui/src/lib/holdings-table/holdings-table.component.scss
  87. 15
      libs/ui/src/lib/holdings-table/holdings-table.component.ts
  88. 1
      libs/ui/src/lib/i18n.ts
  89. 185
      migrations.json
  90. 956
      package-lock.json
  91. 2
      package.json
  92. 22
      prisma/migrations/20231108082445_added_tags_to_holding/migration.sql
  93. 3
      prisma/schema.prisma
  94. 17248
      yarn-error.log

1
.admin.cred

@ -0,0 +1 @@
14d4daf73eefed7da7c32ec19bc37e678be0244fb46c8f4965bfe9ece7384706ed58222ad9b96323893c1d845bc33a308e7524c2c79636062cbb095e0780cb51

25
.env.dev

@ -1,25 +0,0 @@
COMPOSE_PROJECT_NAME=ghostfolio-development
# CACHE
REDIS_HOST=localhost
REDIS_PORT=6379
REDIS_PASSWORD=<INSERT_REDIS_PASSWORD>
# POSTGRES
POSTGRES_DB=ghostfolio-db
POSTGRES_USER=user
POSTGRES_PASSWORD=<INSERT_POSTGRES_PASSWORD>
# VARIOUS
ACCESS_TOKEN_SALT=<INSERT_RANDOM_STRING>
DATABASE_URL=postgresql://${POSTGRES_USER}:${POSTGRES_PASSWORD}@localhost:5432/${POSTGRES_DB}?connect_timeout=300&sslmode=prefer
JWT_SECRET_KEY=<INSERT_RANDOM_STRING>
# DEVELOPMENT
# Nx 18 enables using plugins to infer targets by default
# This is disabled for existing workspaces to maintain compatibility
# For more info, see: https://nx.dev/concepts/inferred-tasks
NX_ADD_PLUGINS=false
NX_NATIVE_COMMAND_RUNNER=false

3
.env.example

@ -1,4 +1,4 @@
COMPOSE_PROJECT_NAME=ghostfolio COMPOSE_PROJECT_NAME=ghostfolio-development
# CACHE # CACHE
REDIS_HOST=redis REDIS_HOST=redis
@ -10,7 +10,6 @@ POSTGRES_DB=ghostfolio-db
POSTGRES_USER=user POSTGRES_USER=user
POSTGRES_PASSWORD=<INSERT_POSTGRES_PASSWORD> POSTGRES_PASSWORD=<INSERT_POSTGRES_PASSWORD>
# VARIOUS
ACCESS_TOKEN_SALT=<INSERT_RANDOM_STRING> ACCESS_TOKEN_SALT=<INSERT_RANDOM_STRING>
DATABASE_URL=postgresql://${POSTGRES_USER}:${POSTGRES_PASSWORD}@postgres:5432/${POSTGRES_DB}?connect_timeout=300&sslmode=prefer DATABASE_URL=postgresql://${POSTGRES_USER}:${POSTGRES_PASSWORD}@postgres:5432/${POSTGRES_DB}?connect_timeout=300&sslmode=prefer
JWT_SECRET_KEY=<INSERT_RANDOM_STRING> JWT_SECRET_KEY=<INSERT_RANDOM_STRING>

47
.github/workflows/docker-image-branch.yml

@ -0,0 +1,47 @@
name: Docker image CD - Branch
on:
push:
branches:
- '*'
jobs:
build_and_push:
runs-on: ubuntu-latest
steps:
- name: Checkout code
uses: actions/checkout@v3
- name: Docker metadata
id: meta
uses: docker/metadata-action@v4
with:
images: dandevaud/ghostfolio
tags: |
type=semver,pattern={{major}}
type=semver,pattern={{version}}
- name: Set up QEMU
uses: docker/setup-qemu-action@v2
- name: Set up Docker Buildx
id: buildx
uses: docker/setup-buildx-action@v2
- name: Login to DockerHub
if: github.event_name != 'pull_request'
uses: docker/login-action@v2
with:
username: ${{ secrets.DOCKER_HUB_USERNAME }}
password: ${{ secrets.DOCKER_HUB_ACCESS_TOKEN }}
- name: Build and push
uses: docker/build-push-action@v3
with:
context: .
platforms: linux/amd64,linux/arm/v7,linux/arm64
push: ${{ github.event_name != 'pull_request' }}
tags: dandevaud/ghostfolio:${{ github.ref_name }}
labels: ${{ steps.meta.output.labels }}
cache-from: type=gha
cache-to: type=gha,mode=max

47
.github/workflows/docker-image-dev.yml

@ -0,0 +1,47 @@
name: Docker image CD - DEV
on:
push:
branches:
- 'dockerpush'
jobs:
build_and_push:
runs-on: ubuntu-latest
steps:
- name: Checkout code
uses: actions/checkout@v3
- name: Docker metadata
id: meta
uses: docker/metadata-action@v4
with:
images: dandevaud/ghostfolio
tags: |
type=semver,pattern={{major}}
type=semver,pattern={{version}}
- name: Set up QEMU
uses: docker/setup-qemu-action@v2
- name: Set up Docker Buildx
id: buildx
uses: docker/setup-buildx-action@v2
- name: Login to DockerHub
if: github.event_name != 'pull_request'
uses: docker/login-action@v2
with:
username: ${{ secrets.DOCKER_HUB_USERNAME }}
password: ${{ secrets.DOCKER_HUB_ACCESS_TOKEN }}
- name: Build and push
uses: docker/build-push-action@v3
with:
context: .
platforms: linux/amd64,linux/arm/v7,linux/arm64
push: ${{ github.event_name != 'pull_request' }}
tags: dandevaud/ghostfolio:beta
labels: ${{ steps.meta.output.labels }}
cache-from: type=gha
cache-to: type=gha,mode=max

5
.github/workflows/docker-image.yml

@ -4,9 +4,6 @@ on:
push: push:
tags: tags:
- '*.*.*' - '*.*.*'
pull_request:
branches:
- 'main'
jobs: jobs:
build_and_push: build_and_push:
@ -19,7 +16,7 @@ jobs:
id: meta id: meta
uses: docker/metadata-action@v4 uses: docker/metadata-action@v4
with: with:
images: ghostfolio/ghostfolio images: dandevaud/ghostfolio
tags: | tags: |
type=semver,pattern={{major}} type=semver,pattern={{major}}
type=semver,pattern={{version}} type=semver,pattern={{version}}

47
.github/workflows/main.yml

@ -0,0 +1,47 @@
name: Docker image CD - DEV
on:
push:
branches:
- 'main'
jobs:
build_and_push:
runs-on: ubuntu-latest
steps:
- name: Checkout code
uses: actions/checkout@v3
- name: Docker metadata
id: meta
uses: docker/metadata-action@v4
with:
images: dandevaud/ghostfolio
tags: |
type=semver,pattern={{major}}
type=semver,pattern={{version}}
- name: Set up QEMU
uses: docker/setup-qemu-action@v2
- name: Set up Docker Buildx
id: buildx
uses: docker/setup-buildx-action@v2
- name: Login to DockerHub
if: github.event_name != 'pull_request'
uses: docker/login-action@v2
with:
username: ${{ secrets.DOCKER_HUB_USERNAME }}
password: ${{ secrets.DOCKER_HUB_ACCESS_TOKEN }}
- name: Build and push
uses: docker/build-push-action@v3
with:
context: .
platforms: linux/amd64,linux/arm/v7,linux/arm64
push: ${{ github.event_name != 'pull_request' }}
tags: dandevaud/ghostfolio:main
labels: ${{ steps.meta.output.labels }}
cache-from: type=gha
cache-to: type=gha,mode=max

0
.husky/pre-commit → .husky/pre-push

89
CHANGELOG.md

@ -27,6 +27,93 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
## 2.149.0 - 2025-03-30 ## 2.149.0 - 2025-03-30
### Changed
- Restructured the resources page
- Renamed the static portfolio analysis rule from _Allocation Cluster Risk_ to _Economic Market Cluster Risk_ (Developed Markets and Emerging Markets)
- Improved the language localization for German (`de`)
- Switched the `consistent-generic-constructors` rule from `warn` to `error` in the `eslint` configuration
- Switched the `consistent-indexed-object-style` rule from `warn` to `off` in the `eslint` configuration
- Switched the `consistent-type-assertions` rule from `warn` to `error` in the `eslint` configuration
- Switched the `prefer-optional-chain` rule from `warn` to `error` in the `eslint` configuration
- Upgraded `Nx` from version `20.0.3` to `20.0.6`
## 2.119.0 - 2024-10-26
### Changed
- Switched the `consistent-type-definitions` rule from `warn` to `error` in the `eslint` configuration
- Switched the `no-empty-function` rule from `warn` to `error` in the `eslint` configuration
- Switched the `prefer-function-type` rule from `warn` to `error` in the `eslint` configuration
- Upgraded `prisma` from version `5.20.0` to `5.21.1`
### Fixed
- Fixed an issue with the X-axis scale of the dividend timeline on the analysis page
- Fixed an issue with the X-axis scale of the investment timeline on the analysis page
- Fixed an issue with the X-axis scale of the portfolio evolution chart on the analysis page
- Fixed an issue in the calculation of the static portfolio analysis rule: _Allocation Cluster Risk_ (Developed Markets)
- Fixed an issue in the calculation of the static portfolio analysis rule: _Allocation Cluster Risk_ (Emerging Markets)
## 2.118.0 - 2024-10-23
### Added
- Added a new static portfolio analysis rule: _Allocation Cluster Risk_ (Developed Markets)
- Added a new static portfolio analysis rule: _Allocation Cluster Risk_ (Emerging Markets)
- Added support for mutual funds in the _EOD Historical Data_ service
### Changed
- Improved the font colors of the chart of the holdings tab on the home page (experimental)
- Optimized the dialog sizes for mobile (full screen)
- Optimized the git-hook via `husky` to lint only affected projects before a commit
- Upgraded `angular` from version `18.1.1` to `18.2.8`
- Upgraded `Nx` from version `19.5.6` to `20.0.3`
### Fixed
- Fixed the warning `export was not found` in connection with `GetValuesParams`
- Quoted the password for the _Redis_ service `healthcheck` in the `docker-compose` files (`docker-compose.yml` and `docker-compose.build.yml`)
## 2.117.0 - 2024-10-19
### Added
- Added the logotype to the footer
- Added the data providers management to the admin control panel
### Changed
- Improved the backgrounds of the chart of the holdings tab on the home page (experimental)
- Improved the language localization for German (`de`)
### Fixed
- Fixed an issue in the carousel component for the testimonial section on the landing page
## 2.116.0 - 2024-10-17
### Added
- Extended the content of the _Self-Hosting_ section by the benchmarks concept for _Compare with..._ on the Frequently Asked Questions (FAQ) page
- Extended the content of the _Self-Hosting_ section by the benchmarks concept for _Markets_ on the Frequently Asked Questions (FAQ) page
- Set the permissions (`chmod 0700`) on `entrypoint.sh` in the `Dockerfile`
### Changed
- Improved the empty state in the benchmarks of the markets overview
- Disabled the text hover effect in the chart of the holdings tab on the home page (experimental)
- Improved the usability to customize the rule thresholds in the _X-ray_ section by introducing units (experimental)
- Switched to adjusted market prices (splits and dividends) in the get historical functionality of the _EOD Historical Data_ service
- Improved the language localization for German (`de`)
### Fixed
- Fixed the usage of the environment variable `PROCESSOR_PORTFOLIO_SNAPSHOT_COMPUTATION_CONCURRENCY`
## 2.115.0 - 2024-10-14
### Added ### Added
- Added support for changing the asset profile identifier (`dataSource` and `symbol`) in the asset profile details dialog of the admin control panel (experimental) - Added support for changing the asset profile identifier (`dataSource` and `symbol`) in the asset profile details dialog of the admin control panel (experimental)
@ -1919,9 +2006,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
- Introduced the lazy-loaded activities table to the position detail dialog (experimental) - Introduced the lazy-loaded activities table to the position detail dialog (experimental)
- Improved the font weight in the value component - Improved the font weight in the value component
- Improved the language localization for Türkçe (`tr`) - Improved the language localization for Türkçe (`tr`)
- Upgraded `angular` from version `17.0.4` to `17.0.7`
- Upgraded to _Inter_ 4 font family - Upgraded to _Inter_ 4 font family
- Upgraded `Nx` from version `17.0.2` to `17.2.5`
### Fixed ### Fixed

1
Dockerfile

@ -61,6 +61,7 @@ RUN apt-get update && apt-get install -y --no-install-suggests \
COPY --chown=node:node --from=builder /ghostfolio/dist/apps /ghostfolio/apps COPY --chown=node:node --from=builder /ghostfolio/dist/apps /ghostfolio/apps
COPY --chown=node:node ./docker/entrypoint.sh /ghostfolio/entrypoint.sh COPY --chown=node:node ./docker/entrypoint.sh /ghostfolio/entrypoint.sh
RUN chmod 0700 /ghostfolio/entrypoint.sh
WORKDIR /ghostfolio/apps/api WORKDIR /ghostfolio/apps/api
EXPOSE ${PORT:-3333} EXPOSE ${PORT:-3333}
USER node USER node

11
apps/api/project.json

@ -4,6 +4,7 @@
"sourceRoot": "apps/api/src", "sourceRoot": "apps/api/src",
"projectType": "application", "projectType": "application",
"prefix": "api", "prefix": "api",
"tags": [],
"generators": {}, "generators": {},
"targets": { "targets": {
"build": { "build": {
@ -60,6 +61,13 @@
"buildTarget": "api:build" "buildTarget": "api:build"
} }
}, },
"profile": {
"executor": "@nx/js:node",
"options": {
"buildTarget": "api:build",
"runtimeArgs": ["--perf-basic-prof-only-functions"]
}
},
"lint": { "lint": {
"executor": "@nx/eslint:lint", "executor": "@nx/eslint:lint",
"options": { "options": {
@ -73,6 +81,5 @@
}, },
"outputs": ["{workspaceRoot}/coverage/apps/api"] "outputs": ["{workspaceRoot}/coverage/apps/api"]
} }
}, }
"tags": []
} }

1
apps/api/src/app/account-balance/account-balance.service.ts

@ -95,6 +95,7 @@ export class AccountBalanceService {
return accountBalance; return accountBalance;
} }
@LogPerformance
public async getAccountBalanceItems({ public async getAccountBalanceItems({
filters, filters,
userCurrency, userCurrency,

2
apps/api/src/app/account/account.service.ts

@ -1,5 +1,6 @@
import { AccountBalanceService } from '@ghostfolio/api/app/account-balance/account-balance.service'; import { AccountBalanceService } from '@ghostfolio/api/app/account-balance/account-balance.service';
import { PortfolioChangedEvent } from '@ghostfolio/api/events/portfolio-changed.event'; import { PortfolioChangedEvent } from '@ghostfolio/api/events/portfolio-changed.event';
import { LogPerformance } from '@ghostfolio/api/interceptors/performance-logging/performance-logging.interceptor';
import { ExchangeRateDataService } from '@ghostfolio/api/services/exchange-rate-data/exchange-rate-data.service'; import { ExchangeRateDataService } from '@ghostfolio/api/services/exchange-rate-data/exchange-rate-data.service';
import { PrismaService } from '@ghostfolio/api/services/prisma/prisma.service'; import { PrismaService } from '@ghostfolio/api/services/prisma/prisma.service';
import { DATE_FORMAT } from '@ghostfolio/common/helper'; import { DATE_FORMAT } from '@ghostfolio/common/helper';
@ -162,6 +163,7 @@ export class AccountService {
}); });
} }
@LogPerformance
public async getCashDetails({ public async getCashDetails({
currency, currency,
filters = [], filters = [],

46
apps/api/src/app/admin/admin.controller.ts

@ -105,6 +105,23 @@ export class AdminController {
this.dataGatheringService.gatherMax(); this.dataGatheringService.gatherMax();
} }
@HasPermission(permissions.accessAdminControl)
@Post('gather/missing')
@UseGuards(AuthGuard('jwt'), HasPermissionGuard)
public async gatherMissing(): Promise<void> {
const assetProfileIdentifiers =
await this.dataGatheringService.getAllActiveAssetProfileIdentifiers();
const promises = assetProfileIdentifiers.map(({ dataSource, symbol }) => {
return this.dataGatheringService.gatherSymbolMissingOnly({
dataSource,
symbol
});
});
await Promise.all(promises);
}
@HasPermission(permissions.accessAdminControl) @HasPermission(permissions.accessAdminControl)
@Post('gather/profile-data') @Post('gather/profile-data')
@UseGuards(AuthGuard('jwt'), HasPermissionGuard) @UseGuards(AuthGuard('jwt'), HasPermissionGuard)
@ -158,7 +175,22 @@ export class AdminController {
@Param('dataSource') dataSource: DataSource, @Param('dataSource') dataSource: DataSource,
@Param('symbol') symbol: string @Param('symbol') symbol: string
): Promise<void> { ): Promise<void> {
this.dataGatheringService.gatherSymbol({ dataSource, symbol }); await this.dataGatheringService.gatherSymbol({ dataSource, symbol });
return;
}
@Post('gatherMissing/:dataSource/:symbol')
@UseGuards(AuthGuard('jwt'), HasPermissionGuard)
@HasPermission(permissions.accessAdminControl)
public async gatherSymbolMissingOnly(
@Param('dataSource') dataSource: DataSource,
@Param('symbol') symbol: string
): Promise<void> {
await this.dataGatheringService.gatherSymbolMissingOnly({
dataSource,
symbol
});
return; return;
} }
@ -340,7 +372,17 @@ export class AdminController {
): Promise<EnhancedSymbolProfile> { ): Promise<EnhancedSymbolProfile> {
return this.adminService.patchAssetProfileData( return this.adminService.patchAssetProfileData(
{ dataSource, symbol }, { dataSource, symbol },
assetProfile {
...assetProfile,
tags: {
connect: assetProfile.tags?.map(({ id }) => {
return { id };
}),
disconnect: assetProfile.tagsDisconnected?.map(({ id }) => ({
id
}))
}
}
); );
} }

2
apps/api/src/app/admin/admin.module.ts

@ -10,6 +10,7 @@ import { MarketDataModule } from '@ghostfolio/api/services/market-data/market-da
import { PrismaModule } from '@ghostfolio/api/services/prisma/prisma.module'; import { PrismaModule } from '@ghostfolio/api/services/prisma/prisma.module';
import { PropertyModule } from '@ghostfolio/api/services/property/property.module'; import { PropertyModule } from '@ghostfolio/api/services/property/property.module';
import { DataGatheringModule } from '@ghostfolio/api/services/queues/data-gathering/data-gathering.module'; import { DataGatheringModule } from '@ghostfolio/api/services/queues/data-gathering/data-gathering.module';
import { SymbolProfileOverwriteModule } from '@ghostfolio/api/services/symbol-profile/symbol-profile-overwrite.module';
import { SymbolProfileModule } from '@ghostfolio/api/services/symbol-profile/symbol-profile.module'; import { SymbolProfileModule } from '@ghostfolio/api/services/symbol-profile/symbol-profile.module';
import { Module } from '@nestjs/common'; import { Module } from '@nestjs/common';
@ -33,6 +34,7 @@ import { QueueModule } from './queue/queue.module';
QueueModule, QueueModule,
SubscriptionModule, SubscriptionModule,
SymbolProfileModule, SymbolProfileModule,
SymbolProfileOverwriteModule,
TransformDataSourceInRequestModule TransformDataSourceInRequestModule
], ],
controllers: [AdminController], controllers: [AdminController],

19
apps/api/src/app/admin/admin.service.ts

@ -41,11 +41,11 @@ import {
import { import {
AssetClass, AssetClass,
AssetSubClass, AssetSubClass,
DataSource,
Prisma, Prisma,
PrismaClient, PrismaClient,
Property, Property,
SymbolProfile SymbolProfile,
DataSource
} from '@prisma/client'; } from '@prisma/client';
import { differenceInDays } from 'date-fns'; import { differenceInDays } from 'date-fns';
import { StatusCodes, getReasonPhrase } from 'http-status-codes'; import { StatusCodes, getReasonPhrase } from 'http-status-codes';
@ -259,7 +259,8 @@ export class AdminService {
scraperConfiguration: true, scraperConfiguration: true,
sectors: true, sectors: true,
symbol: true, symbol: true,
SymbolProfileOverrides: true SymbolProfileOverrides: true,
tags: true
} }
}), }),
this.prismaService.symbolProfile.count({ where }) this.prismaService.symbolProfile.count({ where })
@ -314,7 +315,8 @@ export class AdminService {
Order, Order,
sectors, sectors,
symbol, symbol,
SymbolProfileOverrides SymbolProfileOverrides,
tags
}) => { }) => {
let countriesCount = countries ? Object.keys(countries).length : 0; let countriesCount = countries ? Object.keys(countries).length : 0;
@ -374,7 +376,9 @@ export class AdminService {
sectorsCount, sectorsCount,
activitiesCount: _count.Order, activitiesCount: _count.Order,
date: Order?.[0]?.date, date: Order?.[0]?.date,
isUsedByUsersWithSubscription: await isUsedByUsersWithSubscription isUsedByUsersWithSubscription:
await isUsedByUsersWithSubscription,
tags
}; };
} }
) )
@ -482,6 +486,7 @@ export class AdminService {
holdings, holdings,
isActive, isActive,
name, name,
tags,
scraperConfiguration, scraperConfiguration,
sectors, sectors,
symbol: newSymbol, symbol: newSymbol,
@ -563,6 +568,7 @@ export class AdminService {
sectors, sectors,
symbol, symbol,
symbolMapping, symbolMapping,
tags,
...(dataSource === 'MANUAL' ...(dataSource === 'MANUAL'
? { assetClass, assetSubClass, name, url } ? { assetClass, assetSubClass, name, url }
: { : {
@ -749,7 +755,8 @@ export class AdminService {
date: dateOfFirstActivity, date: dateOfFirstActivity,
id: undefined, id: undefined,
name: symbol, name: symbol,
sectorsCount: 0 sectorsCount: 0,
tags: []
}; };
} }
); );

16
apps/api/src/app/admin/update-asset-profile.dto.ts

@ -1,6 +1,12 @@
import { IsCurrencyCode } from '@ghostfolio/api/validators/is-currency-code'; import { IsCurrencyCode } from '@ghostfolio/api/validators/is-currency-code';
import { AssetClass, AssetSubClass, DataSource, Prisma } from '@prisma/client'; import {
AssetClass,
AssetSubClass,
DataSource,
Prisma,
Tag
} from '@prisma/client';
import { import {
IsArray, IsArray,
IsBoolean, IsBoolean,
@ -44,6 +50,14 @@ export class UpdateAssetProfileDto {
@IsString() @IsString()
name?: string; name?: string;
@IsArray()
@IsOptional()
tags?: Tag[];
@IsArray()
@IsOptional()
tagsDisconnected?: Tag[];
@IsObject() @IsObject()
@IsOptional() @IsOptional()
scraperConfiguration?: Prisma.InputJsonObject; scraperConfiguration?: Prisma.InputJsonObject;

13
apps/api/src/app/import/import.service.ts

@ -627,18 +627,17 @@ export class ImportService {
)?.[symbol] )?.[symbol]
}; };
if (type === 'BUY' || type === 'DIVIDEND' || type === 'SELL') { if (
type === 'BUY' ||
type === 'DIVIDEND' ||
type === 'SELL' ||
type === 'STAKE'
) {
if (!assetProfile?.name) { if (!assetProfile?.name) {
throw new Error( throw new Error(
`activities.${index}.symbol ("${symbol}") is not valid for the specified data source ("${dataSource}")` `activities.${index}.symbol ("${symbol}") is not valid for the specified data source ("${dataSource}")`
); );
} }
if (assetProfile.currency !== currency) {
throw new Error(
`activities.${index}.currency ("${currency}") does not match with currency of ${assetProfile.symbol} ("${assetProfile.currency}")`
);
}
} }
assetProfiles[getAssetProfileIdentifier({ dataSource, symbol })] = assetProfiles[getAssetProfileIdentifier({ dataSource, symbol })] =

114
apps/api/src/app/order/order.service.ts

@ -50,33 +50,55 @@ export class OrderService {
public async assignTags({ public async assignTags({
dataSource, dataSource,
symbol, symbol,
tags, userId,
userId tags
}: { tags: Tag[]; userId: string } & AssetProfileIdentifier) { }: { tags: Tag[]; userId: string } & AssetProfileIdentifier) {
const orders = await this.prismaService.order.findMany({ const promis = await this.symbolProfileService.getSymbolProfiles([
where: { {
userId, dataSource,
SymbolProfile: { symbol
dataSource, }
symbol ]);
} const symbolProfile: EnhancedSymbolProfile = promis[0];
const result = await this.symbolProfileService.updateSymbolProfile(
{ dataSource, symbol },
{
assetClass: symbolProfile.assetClass,
assetSubClass: symbolProfile.assetSubClass,
countries: symbolProfile.countries.reduce(
(all, v) => [...all, { code: v.code, weight: v.weight }],
[]
),
currency: symbolProfile.currency,
dataSource,
holdings: symbolProfile.holdings.reduce(
(all, v) => [
...all,
{ name: v.name, weight: v.allocationInPercentage }
],
[]
),
name: symbolProfile.name,
sectors: symbolProfile.sectors.reduce(
(all, v) => [...all, { name: v.name, weight: v.weight }],
[]
),
symbol,
tags: {
connectOrCreate: tags.map(({ id, name }) => {
return {
create: {
id,
name
},
where: {
id
}
};
})
},
url: symbolProfile.url
} }
});
await Promise.all(
orders.map(({ id }) =>
this.prismaService.order.update({
data: {
tags: {
// The set operation replaces all existing connections with the provided ones
set: tags.map((tag) => {
return { id: tag.id };
})
}
},
where: { id }
})
)
); );
this.eventEmitter.emit( this.eventEmitter.emit(
@ -85,6 +107,8 @@ export class OrderService {
userId userId
}) })
); );
return result;
} }
public async createOrder( public async createOrder(
@ -302,6 +326,7 @@ export class OrderService {
}); });
} }
@LogPerformance
public async getOrders({ public async getOrders({
endDate, endDate,
filters, filters,
@ -456,13 +481,34 @@ export class OrderService {
} }
if (filtersByTag?.length > 0) { if (filtersByTag?.length > 0) {
where.tags = { where.AND = [
some: { {
OR: filtersByTag.map(({ id }) => { OR: [
return { id }; {
}) tags: {
some: {
OR: filtersByTag.map(({ id }) => {
return {
id: id
};
})
}
}
},
{
SymbolProfile: {
tags: {
some: {
OR: filtersByTag.map(({ id }) => {
return { id };
})
}
}
}
}
]
} }
}; ];
} }
if (sortColumn) { if (sortColumn) {
@ -494,7 +540,11 @@ export class OrderService {
} }
}, },
// eslint-disable-next-line @typescript-eslint/naming-convention // eslint-disable-next-line @typescript-eslint/naming-convention
SymbolProfile: true, SymbolProfile: {
include: {
tags: true
}
},
tags: true tags: true
} }
}), }),

595
apps/api/src/app/portfolio/calculator/constantPortfolioReturn/portfolio-calculator.ts

@ -0,0 +1,595 @@
import { Activity } from '@ghostfolio/api/app/order/interfaces/activities.interface';
import { OrderService } from '@ghostfolio/api/app/order/order.service';
import { RedisCacheService } from '@ghostfolio/api/app/redis-cache/redis-cache.service';
import { getFactor } from '@ghostfolio/api/helper/portfolio.helper';
import { LogPerformance } from '@ghostfolio/api/interceptors/performance-logging/performance-logging.interceptor';
import { ConfigurationService } from '@ghostfolio/api/services/configuration/configuration.service';
import { ExchangeRateDataService } from '@ghostfolio/api/services/exchange-rate-data/exchange-rate-data.service';
import { IDataGatheringItem } from '@ghostfolio/api/services/interfaces/interfaces';
import { PortfolioSnapshotService } from '@ghostfolio/api/services/queues/portfolio-snapshot/portfolio-snapshot.service';
import { DATE_FORMAT, parseDate, resetHours } from '@ghostfolio/common/helper';
import { Filter, HistoricalDataItem } from '@ghostfolio/common/interfaces';
import { Inject, Logger } from '@nestjs/common';
import { Big } from 'big.js';
import {
addDays,
eachDayOfInterval,
endOfDay,
format,
isAfter,
isBefore,
subDays
} from 'date-fns';
import { CurrentRateService } from '../../current-rate.service';
import { DateQuery } from '../../interfaces/date-query.interface';
import { PortfolioOrder } from '../../interfaces/portfolio-order.interface';
import { RoaiPortfolioCalculator } from '../roai/portfolio-calculator';
export class CPRPortfolioCalculator extends RoaiPortfolioCalculator {
private holdings: { [date: string]: { [symbol: string]: Big } } = {};
private holdingCurrencies: { [symbol: string]: string } = {};
constructor(
{
accountBalanceItems,
activities,
configurationService,
currency,
currentRateService,
exchangeRateDataService,
portfolioSnapshotService,
redisCacheService,
userId,
filters
}: {
accountBalanceItems: HistoricalDataItem[];
activities: Activity[];
configurationService: ConfigurationService;
currency: string;
currentRateService: CurrentRateService;
exchangeRateDataService: ExchangeRateDataService;
portfolioSnapshotService: PortfolioSnapshotService;
redisCacheService: RedisCacheService;
filters: Filter[];
userId: string;
},
@Inject()
private orderService: OrderService
) {
super({
accountBalanceItems,
activities,
configurationService,
currency,
filters,
currentRateService,
exchangeRateDataService,
portfolioSnapshotService,
redisCacheService,
userId
});
}
@LogPerformance
public async getPerformanceWithTimeWeightedReturn({
start,
end
}: {
start: Date;
end: Date;
}): Promise<{ chart: HistoricalDataItem[] }> {
const item = await super.getPerformance({
end,
start
});
const itemResult = item.chart;
const dates = itemResult.map((item) => parseDate(item.date));
const timeWeighted = await this.getTimeWeightedChartData({
dates
});
item.chart = itemResult.map((itemInt) => {
const timeWeightedItem = timeWeighted.find(
(timeWeightedItem) => timeWeightedItem.date === itemInt.date
);
if (timeWeightedItem) {
itemInt.timeWeightedPerformance =
timeWeightedItem.netPerformanceInPercentage;
itemInt.timeWeightedPerformanceWithCurrencyEffect =
timeWeightedItem.netPerformanceInPercentageWithCurrencyEffect;
}
return itemInt;
});
return item;
}
@LogPerformance
public async getUnfilteredNetWorth(currency: string): Promise<Big> {
const activities = await this.orderService.getOrders({
userId: this.userId,
userCurrency: currency,
types: ['BUY', 'SELL', 'STAKE'],
withExcludedAccounts: true
});
const orders = this.activitiesToPortfolioOrder(activities.activities);
const start = orders.reduce(
(date, order) =>
parseDate(date.date).getTime() < parseDate(order.date).getTime()
? date
: order,
{ date: orders[0].date }
).date;
const end = new Date(Date.now());
const holdings = await this.getHoldings(orders, parseDate(start), end);
const marketMap = await this.currentRateService.getValues({
dataGatheringItems: this.mapToDataGatheringItems(orders),
dateQuery: { in: [end] }
});
const endString = format(end, DATE_FORMAT);
const exchangeRates = await Promise.all(
Object.keys(holdings[endString]).map(async (holding) => {
const symbolCurrency = this.getCurrencyFromActivities(orders, holding);
const exchangeRate =
await this.exchangeRateDataService.toCurrencyAtDate(
1,
symbolCurrency,
this.currency,
end
);
return { symbolCurrency, exchangeRate };
})
);
const currencyRates = exchangeRates.reduce<{ [currency: string]: number }>(
(all, currency): { [currency: string]: number } => {
all[currency.symbolCurrency] ??= currency.exchangeRate;
return all;
},
{}
);
const totalInvestment = await Object.keys(holdings[endString]).reduce(
(sum, holding) => {
if (!holdings[endString][holding].toNumber()) {
return sum;
}
const symbol = marketMap.values.find((m) => m.symbol === holding);
if (symbol?.marketPrice === undefined) {
Logger.warn(
`Missing historical market data for ${holding} (${end})`,
'PortfolioCalculator'
);
return sum;
} else {
const symbolCurrency = this.getCurrency(holding);
const price = new Big(currencyRates[symbolCurrency]).mul(
symbol.marketPrice
);
return sum.plus(new Big(price).mul(holdings[endString][holding]));
}
},
new Big(0)
);
return totalInvestment;
}
@LogPerformance
protected async getTimeWeightedChartData({
dates
}: {
dates?: Date[];
}): Promise<HistoricalDataItem[]> {
dates = dates.sort((a, b) => a.getTime() - b.getTime());
const start = dates[0];
const end = dates[dates.length - 1];
const marketMapTask = this.computeMarketMap({
gte: start,
lt: addDays(end, 1)
});
const timelineHoldings = await this.getHoldings(
this.activities,
start,
end
);
const data: HistoricalDataItem[] = [];
const startString = format(start, DATE_FORMAT);
data.push({
date: startString,
netPerformanceInPercentage: 0,
netPerformanceInPercentageWithCurrencyEffect: 0,
investmentValueWithCurrencyEffect: 0,
netPerformance: 0,
netPerformanceWithCurrencyEffect: 0,
netWorth: 0,
totalAccountBalance: 0,
totalInvestment: 0,
totalInvestmentValueWithCurrencyEffect: 0,
value: 0,
valueWithCurrencyEffect: 0
});
this.marketMap = await marketMapTask;
let totalInvestment = Object.keys(timelineHoldings[startString]).reduce(
(sum, holding) => {
return sum.plus(
timelineHoldings[startString][holding].mul(
this.marketMap[startString][holding] ?? new Big(0)
)
);
},
new Big(0)
);
let previousNetPerformanceInPercentage = new Big(0);
let previousNetPerformanceInPercentageWithCurrencyEffect = new Big(0);
for (let i = 1; i < dates.length; i++) {
const date = format(dates[i], DATE_FORMAT);
const previousDate = format(dates[i - 1], DATE_FORMAT);
const holdings = timelineHoldings[previousDate];
let newTotalInvestment = new Big(0);
let netPerformanceInPercentage = new Big(0);
let netPerformanceInPercentageWithCurrencyEffect = new Big(0);
for (const holding of Object.keys(holdings)) {
({
netPerformanceInPercentage,
netPerformanceInPercentageWithCurrencyEffect,
newTotalInvestment
} = await this.handleSingleHolding(
previousDate,
holding,
date,
totalInvestment,
timelineHoldings,
netPerformanceInPercentage,
netPerformanceInPercentageWithCurrencyEffect,
newTotalInvestment
));
}
totalInvestment = newTotalInvestment;
previousNetPerformanceInPercentage = previousNetPerformanceInPercentage
.plus(1)
.mul(netPerformanceInPercentage.plus(1))
.minus(1);
previousNetPerformanceInPercentageWithCurrencyEffect =
previousNetPerformanceInPercentageWithCurrencyEffect
.plus(1)
.mul(netPerformanceInPercentageWithCurrencyEffect.plus(1))
.minus(1);
data.push({
date,
netPerformanceInPercentage: previousNetPerformanceInPercentage
.mul(100)
.toNumber(),
netPerformanceInPercentageWithCurrencyEffect:
previousNetPerformanceInPercentageWithCurrencyEffect
.mul(100)
.toNumber()
});
}
return data;
}
@LogPerformance
protected async handleSingleHolding(
previousDate: string,
holding: string,
date: string,
totalInvestment: Big,
timelineHoldings: { [date: string]: { [symbol: string]: Big } },
netPerformanceInPercentage: Big,
netPerformanceInPercentageWithCurrencyEffect: Big,
newTotalInvestment: Big
) {
const previousPrice =
Object.keys(this.marketMap).indexOf(previousDate) > 0
? this.marketMap[previousDate][holding]
: undefined;
const priceDictionary = this.marketMap[date];
let currentPrice =
priceDictionary !== undefined ? priceDictionary[holding] : previousPrice;
currentPrice ??= previousPrice;
const previousHolding = timelineHoldings[previousDate][holding];
const priceInBaseCurrency = currentPrice
? new Big(
await this.exchangeRateDataService.toCurrencyAtDate(
currentPrice?.toNumber() ?? 0,
this.getCurrency(holding),
this.currency,
parseDate(date)
)
)
: new Big(0);
if (previousHolding.eq(0)) {
return {
netPerformanceInPercentage: netPerformanceInPercentage,
netPerformanceInPercentageWithCurrencyEffect:
netPerformanceInPercentageWithCurrencyEffect,
newTotalInvestment: newTotalInvestment.plus(
timelineHoldings[date][holding].mul(priceInBaseCurrency)
)
};
}
if (previousPrice === undefined || currentPrice === undefined) {
Logger.warn(
`Missing historical market data for ${holding} (${previousPrice === undefined ? previousDate : date}})`,
'PortfolioCalculator'
);
return {
netPerformanceInPercentage: netPerformanceInPercentage,
netPerformanceInPercentageWithCurrencyEffect:
netPerformanceInPercentageWithCurrencyEffect,
newTotalInvestment: newTotalInvestment.plus(
timelineHoldings[date][holding].mul(priceInBaseCurrency)
)
};
}
const previousPriceInBaseCurrency = previousPrice
? new Big(
await this.exchangeRateDataService.toCurrencyAtDate(
previousPrice?.toNumber() ?? 0,
this.getCurrency(holding),
this.currency,
parseDate(previousDate)
)
)
: new Big(0);
const portfolioWeight = totalInvestment.toNumber()
? previousHolding.mul(previousPriceInBaseCurrency).div(totalInvestment)
: new Big(0);
netPerformanceInPercentage = netPerformanceInPercentage.plus(
currentPrice.div(previousPrice).minus(1).mul(portfolioWeight)
);
netPerformanceInPercentageWithCurrencyEffect =
netPerformanceInPercentageWithCurrencyEffect.plus(
priceInBaseCurrency
.div(previousPriceInBaseCurrency)
.minus(1)
.mul(portfolioWeight)
);
newTotalInvestment = newTotalInvestment.plus(
timelineHoldings[date][holding].mul(priceInBaseCurrency)
);
return {
netPerformanceInPercentage,
netPerformanceInPercentageWithCurrencyEffect,
newTotalInvestment
};
}
@LogPerformance
protected getCurrency(symbol: string) {
return this.getCurrencyFromActivities(this.activities, symbol);
}
@LogPerformance
protected getCurrencyFromActivities(
activities: PortfolioOrder[],
symbol: string
) {
if (!this.holdingCurrencies[symbol]) {
this.holdingCurrencies[symbol] = activities.find(
(a) => a.SymbolProfile.symbol === symbol
).SymbolProfile.currency;
}
return this.holdingCurrencies[symbol];
}
@LogPerformance
protected async getHoldings(
activities: PortfolioOrder[],
start: Date,
end: Date
) {
if (
this.holdings &&
Object.keys(this.holdings).some((h) =>
isAfter(parseDate(h), subDays(end, 1))
) &&
Object.keys(this.holdings).some((h) =>
isBefore(parseDate(h), addDays(start, 1))
)
) {
return this.holdings;
}
this.computeHoldings(activities, start, end);
return this.holdings;
}
@LogPerformance
protected async computeHoldings(
activities: PortfolioOrder[],
start: Date,
end: Date
) {
const investmentByDate = this.getInvestmentByDate(activities);
this.calculateHoldings(investmentByDate, start, end);
}
private calculateHoldings(
investmentByDate: { [date: string]: PortfolioOrder[] },
start: Date,
end: Date
) {
const transactionDates = Object.keys(investmentByDate).sort();
const dates = eachDayOfInterval({ start, end }, { step: 1 })
.map((date) => {
return resetHours(date);
})
.sort((a, b) => a.getTime() - b.getTime());
const currentHoldings: { [date: string]: { [symbol: string]: Big } } = {};
this.calculateInitialHoldings(investmentByDate, start, currentHoldings);
for (let i = 1; i < dates.length; i++) {
const dateString = format(dates[i], DATE_FORMAT);
const previousDateString = format(dates[i - 1], DATE_FORMAT);
if (transactionDates.some((d) => d === dateString)) {
const holdings = { ...currentHoldings[previousDateString] };
investmentByDate[dateString].forEach((trade) => {
holdings[trade.SymbolProfile.symbol] ??= new Big(0);
holdings[trade.SymbolProfile.symbol] = holdings[
trade.SymbolProfile.symbol
].plus(trade.quantity.mul(getFactor(trade.type)));
});
currentHoldings[dateString] = holdings;
} else {
currentHoldings[dateString] = currentHoldings[previousDateString];
}
}
this.holdings = currentHoldings;
}
@LogPerformance
protected calculateInitialHoldings(
investmentByDate: { [date: string]: PortfolioOrder[] },
start: Date,
currentHoldings: { [date: string]: { [symbol: string]: Big } }
) {
const preRangeTrades = Object.keys(investmentByDate)
.filter((date) => resetHours(new Date(date)) <= start)
.map((date) => investmentByDate[date])
.reduce((a, b) => a.concat(b), [])
.reduce((groupBySymbol, trade) => {
if (!groupBySymbol[trade.SymbolProfile.symbol]) {
groupBySymbol[trade.SymbolProfile.symbol] = [];
}
groupBySymbol[trade.SymbolProfile.symbol].push(trade);
return groupBySymbol;
}, {});
currentHoldings[format(start, DATE_FORMAT)] = {};
for (const symbol of Object.keys(preRangeTrades)) {
const trades: PortfolioOrder[] = preRangeTrades[symbol];
const startQuantity = trades.reduce((sum, trade) => {
return sum.plus(trade.quantity.mul(getFactor(trade.type)));
}, new Big(0));
currentHoldings[format(start, DATE_FORMAT)][symbol] = startQuantity;
}
}
@LogPerformance
protected getInvestmentByDate(activities: PortfolioOrder[]): {
[date: string]: PortfolioOrder[];
} {
return activities.reduce((groupedByDate, order) => {
if (!groupedByDate[order.date]) {
groupedByDate[order.date] = [];
}
groupedByDate[order.date].push(order);
return groupedByDate;
}, {});
}
@LogPerformance
protected mapToDataGatheringItems(
orders: PortfolioOrder[]
): IDataGatheringItem[] {
return orders
.map((activity) => {
return {
symbol: activity.SymbolProfile.symbol,
dataSource: activity.SymbolProfile.dataSource
};
})
.filter(
(gathering, i, arr) =>
arr.findIndex((t) => t.symbol === gathering.symbol) === i
);
}
@LogPerformance
protected async computeMarketMap(dateQuery: DateQuery): Promise<{
[date: string]: { [symbol: string]: Big };
}> {
const dataGatheringItems: IDataGatheringItem[] =
this.mapToDataGatheringItems(this.activities);
const { values: marketSymbols } = await this.currentRateService.getValues({
dataGatheringItems,
dateQuery
});
const marketSymbolMap: {
[date: string]: { [symbol: string]: Big };
} = {};
for (const marketSymbol of marketSymbols) {
const date = format(marketSymbol.date, DATE_FORMAT);
if (!marketSymbolMap[date]) {
marketSymbolMap[date] = {};
}
if (marketSymbol.marketPrice) {
marketSymbolMap[date][marketSymbol.symbol] = new Big(
marketSymbol.marketPrice
);
}
}
return marketSymbolMap;
}
@LogPerformance
protected activitiesToPortfolioOrder(
activities: Activity[]
): PortfolioOrder[] {
return activities
.map(
({
date,
fee,
quantity,
SymbolProfile,
tags = [],
type,
unitPrice
}) => {
if (isAfter(date, new Date(Date.now()))) {
// Adapt date to today if activity is in future (e.g. liability)
// to include it in the interval
date = endOfDay(new Date(Date.now()));
}
return {
SymbolProfile,
tags,
type,
date: format(date, DATE_FORMAT),
fee: new Big(fee),
quantity: new Big(quantity),
unitPrice: new Big(unitPrice)
};
}
)
.sort((a, b) => {
return a.date?.localeCompare(b.date);
});
}
}

22
apps/api/src/app/portfolio/calculator/portfolio-calculator.factory.ts

@ -1,6 +1,7 @@
import { Activity } from '@ghostfolio/api/app/order/interfaces/activities.interface'; import { Activity } from '@ghostfolio/api/app/order/interfaces/activities.interface';
import { CurrentRateService } from '@ghostfolio/api/app/portfolio/current-rate.service'; import { CurrentRateService } from '@ghostfolio/api/app/portfolio/current-rate.service';
import { RedisCacheService } from '@ghostfolio/api/app/redis-cache/redis-cache.service'; import { RedisCacheService } from '@ghostfolio/api/app/redis-cache/redis-cache.service';
import { LogPerformance } from '@ghostfolio/api/interceptors/performance-logging/performance-logging.interceptor';
import { ConfigurationService } from '@ghostfolio/api/services/configuration/configuration.service'; import { ConfigurationService } from '@ghostfolio/api/services/configuration/configuration.service';
import { ExchangeRateDataService } from '@ghostfolio/api/services/exchange-rate-data/exchange-rate-data.service'; import { ExchangeRateDataService } from '@ghostfolio/api/services/exchange-rate-data/exchange-rate-data.service';
import { PortfolioSnapshotService } from '@ghostfolio/api/services/queues/portfolio-snapshot/portfolio-snapshot.service'; import { PortfolioSnapshotService } from '@ghostfolio/api/services/queues/portfolio-snapshot/portfolio-snapshot.service';
@ -16,7 +17,8 @@ import { TwrPortfolioCalculator } from './twr/portfolio-calculator';
export enum PerformanceCalculationType { export enum PerformanceCalculationType {
MWR = 'MWR', // Money-Weighted Rate of Return MWR = 'MWR', // Money-Weighted Rate of Return
ROAI = 'ROAI', // Return on Average Investment ROAI = 'ROAI', // Return on Average Investment
TWR = 'TWR' // Time-Weighted Rate of Return TWR = 'TWR', // Time-Weighted Rate of Return
CPR = 'CPR' // Constant Portfolio Rate of Return
} }
@Injectable() @Injectable()
@ -29,6 +31,7 @@ export class PortfolioCalculatorFactory {
private readonly redisCacheService: RedisCacheService private readonly redisCacheService: RedisCacheService
) {} ) {}
@LogPerformance
public createCalculator({ public createCalculator({
accountBalanceItems = [], accountBalanceItems = [],
activities, activities,
@ -76,13 +79,26 @@ export class PortfolioCalculatorFactory {
accountBalanceItems, accountBalanceItems,
activities, activities,
currency, currency,
filters, currentRateService: this.currentRateService,
userId, userId,
configurationService: this.configurationService, configurationService: this.configurationService,
exchangeRateDataService: this.exchangeRateDataService,
portfolioSnapshotService: this.portfolioSnapshotService,
redisCacheService: this.redisCacheService,
filters
});
case PerformanceCalculationType.CPR:
return new RoaiPortfolioCalculator({
accountBalanceItems,
activities,
currency,
currentRateService: this.currentRateService, currentRateService: this.currentRateService,
userId,
configurationService: this.configurationService,
exchangeRateDataService: this.exchangeRateDataService, exchangeRateDataService: this.exchangeRateDataService,
portfolioSnapshotService: this.portfolioSnapshotService, portfolioSnapshotService: this.portfolioSnapshotService,
redisCacheService: this.redisCacheService redisCacheService: this.redisCacheService,
filters
}); });
default: default:
throw new Error('Invalid calculation type'); throw new Error('Invalid calculation type');

222
apps/api/src/app/portfolio/calculator/portfolio-calculator.ts

@ -57,12 +57,12 @@ export abstract class PortfolioCalculator {
protected accountBalanceItems: HistoricalDataItem[]; protected accountBalanceItems: HistoricalDataItem[];
protected activities: PortfolioOrder[]; protected activities: PortfolioOrder[];
private configurationService: ConfigurationService; protected configurationService: ConfigurationService;
private currency: string; protected currency: string;
private currentRateService: CurrentRateService; protected currentRateService: CurrentRateService;
private dataProviderInfos: DataProviderInfo[]; private dataProviderInfos: DataProviderInfo[];
private endDate: Date; private endDate: Date;
private exchangeRateDataService: ExchangeRateDataService; protected exchangeRateDataService: ExchangeRateDataService;
private filters: Filter[]; private filters: Filter[];
private portfolioSnapshotService: PortfolioSnapshotService; private portfolioSnapshotService: PortfolioSnapshotService;
private redisCacheService: RedisCacheService; private redisCacheService: RedisCacheService;
@ -70,7 +70,8 @@ export abstract class PortfolioCalculator {
private snapshotPromise: Promise<void>; private snapshotPromise: Promise<void>;
private startDate: Date; private startDate: Date;
private transactionPoints: TransactionPoint[]; private transactionPoints: TransactionPoint[];
private userId: string; protected userId: string;
protected marketMap: { [date: string]: { [symbol: string]: Big } } = {};
public constructor({ public constructor({
accountBalanceItems, accountBalanceItems,
@ -161,10 +162,6 @@ export abstract class PortfolioCalculator {
this.snapshotPromise = this.initialize(); this.snapshotPromise = this.initialize();
} }
protected abstract calculateOverallPerformance(
positions: TimelinePosition[]
): PortfolioSnapshot;
@LogPerformance @LogPerformance
public async computeSnapshot(): Promise<PortfolioSnapshot> { public async computeSnapshot(): Promise<PortfolioSnapshot> {
const lastTransactionPoint = this.transactionPoints.at(-1); const lastTransactionPoint = this.transactionPoints.at(-1);
@ -202,10 +199,7 @@ export abstract class PortfolioCalculator {
for (const { currency, dataSource, symbol } of transactionPoints[ for (const { currency, dataSource, symbol } of transactionPoints[
firstIndex - 1 firstIndex - 1
].items) { ].items) {
dataGatheringItems.push({ dataGatheringItems.push({ dataSource, symbol });
dataSource,
symbol
});
currencies[symbol] = currency; currencies[symbol] = currency;
} }
@ -234,17 +228,12 @@ export abstract class PortfolioCalculator {
values: marketSymbols values: marketSymbols
} = await this.currentRateService.getValues({ } = await this.currentRateService.getValues({
dataGatheringItems, dataGatheringItems,
dateQuery: { dateQuery: { gte: this.startDate, lt: this.endDate }
gte: this.startDate,
lt: this.endDate
}
}); });
this.dataProviderInfos = dataProviderInfos; this.dataProviderInfos = dataProviderInfos;
const marketSymbolMap: { const marketSymbolMap: { [date: string]: { [symbol: string]: Big } } = {};
[date: string]: { [symbol: string]: Big };
} = {};
for (const marketSymbol of marketSymbols) { for (const marketSymbol of marketSymbols) {
const date = format(marketSymbol.date, DATE_FORMAT); const date = format(marketSymbol.date, DATE_FORMAT);
@ -623,10 +612,12 @@ export abstract class PortfolioCalculator {
}; };
} }
@LogPerformance
public getDataProviderInfos() { public getDataProviderInfos() {
return this.dataProviderInfos; return this.dataProviderInfos;
} }
@LogPerformance
public async getDividendInBaseCurrency() { public async getDividendInBaseCurrency() {
await this.snapshotPromise; await this.snapshotPromise;
@ -637,18 +628,21 @@ export abstract class PortfolioCalculator {
); );
} }
@LogPerformance
public async getFeesInBaseCurrency() { public async getFeesInBaseCurrency() {
await this.snapshotPromise; await this.snapshotPromise;
return this.snapshot.totalFeesWithCurrencyEffect; return this.snapshot.totalFeesWithCurrencyEffect;
} }
@LogPerformance
public async getInterestInBaseCurrency() { public async getInterestInBaseCurrency() {
await this.snapshotPromise; await this.snapshotPromise;
return this.snapshot.totalInterestWithCurrencyEffect; return this.snapshot.totalInterestWithCurrencyEffect;
} }
@LogPerformance
public getInvestments(): { date: string; investment: Big }[] { public getInvestments(): { date: string; investment: Big }[] {
if (this.transactionPoints.length === 0) { if (this.transactionPoints.length === 0) {
return []; return [];
@ -666,6 +660,7 @@ export abstract class PortfolioCalculator {
}); });
} }
@LogPerformance
public getInvestmentsByGroup({ public getInvestmentsByGroup({
data, data,
groupBy groupBy
@ -689,12 +684,14 @@ export abstract class PortfolioCalculator {
})); }));
} }
@LogPerformance
public async getLiabilitiesInBaseCurrency() { public async getLiabilitiesInBaseCurrency() {
await this.snapshotPromise; await this.snapshotPromise;
return this.snapshot.totalLiabilitiesWithCurrencyEffect; return this.snapshot.totalLiabilitiesWithCurrencyEffect;
} }
@LogPerformance
public async getPerformance({ end, start }) { public async getPerformance({ end, start }) {
await this.snapshotPromise; await this.snapshotPromise;
@ -763,12 +760,6 @@ export abstract class PortfolioCalculator {
return { chart }; return { chart };
} }
public async getSnapshot() {
await this.snapshotPromise;
return this.snapshot;
}
public getStartDate() { public getStartDate() {
let firstAccountBalanceDate: Date; let firstAccountBalanceDate: Date;
let firstActivityDate: Date; let firstActivityDate: Date;
@ -794,24 +785,6 @@ export abstract class PortfolioCalculator {
return min([firstAccountBalanceDate, firstActivityDate]); return min([firstAccountBalanceDate, firstActivityDate]);
} }
protected abstract getSymbolMetrics({
chartDateMap,
dataSource,
end,
exchangeRates,
marketSymbolMap,
start,
symbol
}: {
chartDateMap: { [date: string]: boolean };
end: Date;
exchangeRates: { [dateString: string]: number };
marketSymbolMap: {
[date: string]: { [symbol: string]: Big };
};
start: Date;
} & AssetProfileIdentifier): SymbolMetrics;
public getTransactionPoints() { public getTransactionPoints() {
return this.transactionPoints; return this.transactionPoints;
} }
@ -822,76 +795,15 @@ export abstract class PortfolioCalculator {
return this.snapshot.totalValuablesWithCurrencyEffect; return this.snapshot.totalValuablesWithCurrencyEffect;
} }
private getChartDateMap({ @LogPerformance
endDate, public async getSnapshot() {
startDate, await this.snapshotPromise;
step
}: {
endDate: Date;
startDate: Date;
step: number;
}): { [date: string]: true } {
// Create a map of all relevant chart dates:
// 1. Add transaction point dates
const chartDateMap = this.transactionPoints.reduce((result, { date }) => {
result[date] = true;
return result;
}, {});
// 2. Add dates between transactions respecting the specified step size
for (const date of eachDayOfInterval(
{ end: endDate, start: startDate },
{ step }
)) {
chartDateMap[format(date, DATE_FORMAT)] = true;
}
if (step > 1) {
// Reduce the step size of last 90 days
for (const date of eachDayOfInterval(
{ end: endDate, start: subDays(endDate, 90) },
{ step: 3 }
)) {
chartDateMap[format(date, DATE_FORMAT)] = true;
}
// Reduce the step size of last 30 days
for (const date of eachDayOfInterval(
{ end: endDate, start: subDays(endDate, 30) },
{ step: 1 }
)) {
chartDateMap[format(date, DATE_FORMAT)] = true;
}
}
// Make sure the end date is present
chartDateMap[format(endDate, DATE_FORMAT)] = true;
// Make sure some key dates are present
for (const dateRange of ['1d', '1y', '5y', 'max', 'mtd', 'wtd', 'ytd']) {
const { endDate: dateRangeEnd, startDate: dateRangeStart } =
getIntervalFromDateRange(dateRange);
if (
!isBefore(dateRangeStart, startDate) &&
!isAfter(dateRangeStart, endDate)
) {
chartDateMap[format(dateRangeStart, DATE_FORMAT)] = true;
}
if (
!isBefore(dateRangeEnd, startDate) &&
!isAfter(dateRangeEnd, endDate)
) {
chartDateMap[format(dateRangeEnd, DATE_FORMAT)] = true;
}
}
return chartDateMap; return this.snapshot;
} }
@LogPerformance @LogPerformance
private computeTransactionPoints() { protected computeTransactionPoints() {
this.transactionPoints = []; this.transactionPoints = [];
const symbols: { [symbol: string]: TransactionPointSymbol } = {}; const symbols: { [symbol: string]: TransactionPointSymbol } = {};
@ -1030,7 +942,7 @@ export abstract class PortfolioCalculator {
} }
@LogPerformance @LogPerformance
private async initialize() { protected async initialize() {
const startTimeTotal = performance.now(); const startTimeTotal = performance.now();
let cachedPortfolioSnapshot: PortfolioSnapshot; let cachedPortfolioSnapshot: PortfolioSnapshot;
@ -1110,4 +1022,92 @@ export abstract class PortfolioCalculator {
await this.initialize(); await this.initialize();
} }
} }
private getChartDateMap({
endDate,
startDate,
step
}: {
endDate: Date;
startDate: Date;
step: number;
}): { [date: string]: true } {
// Create a map of all relevant chart dates:
// 1. Add transaction point dates
const chartDateMap = this.transactionPoints.reduce((result, { date }) => {
result[date] = true;
return result;
}, {});
// 2. Add dates between transactions respecting the specified step size
for (const date of eachDayOfInterval(
{ end: endDate, start: startDate },
{ step }
)) {
chartDateMap[format(date, DATE_FORMAT)] = true;
}
if (step > 1) {
// Reduce the step size of last 90 days
for (const date of eachDayOfInterval(
{ end: endDate, start: subDays(endDate, 90) },
{ step: 3 }
)) {
chartDateMap[format(date, DATE_FORMAT)] = true;
}
// Reduce the step size of last 30 days
for (const date of eachDayOfInterval(
{ end: endDate, start: subDays(endDate, 30) },
{ step: 1 }
)) {
chartDateMap[format(date, DATE_FORMAT)] = true;
}
}
// Make sure the end date is present
chartDateMap[format(endDate, DATE_FORMAT)] = true;
// Make sure some key dates are present
for (const dateRange of ['1d', '1y', '5y', 'max', 'mtd', 'wtd', 'ytd']) {
const { endDate: dateRangeEnd, startDate: dateRangeStart } =
getIntervalFromDateRange(dateRange);
if (
!isBefore(dateRangeStart, startDate) &&
!isAfter(dateRangeStart, endDate)
) {
chartDateMap[format(dateRangeStart, DATE_FORMAT)] = true;
}
if (
!isBefore(dateRangeEnd, startDate) &&
!isAfter(dateRangeEnd, endDate)
) {
chartDateMap[format(dateRangeEnd, DATE_FORMAT)] = true;
}
}
return chartDateMap;
}
protected abstract getSymbolMetrics({
chartDateMap,
dataSource,
end,
exchangeRates,
marketSymbolMap,
start,
symbol
}: {
chartDateMap: { [date: string]: boolean };
end: Date;
exchangeRates: { [dateString: string]: number };
marketSymbolMap: { [date: string]: { [symbol: string]: Big } };
start: Date;
} & AssetProfileIdentifier): SymbolMetrics;
protected abstract calculateOverallPerformance(
positions: TimelinePosition[]
): PortfolioSnapshot;
} }

4
apps/api/src/app/portfolio/calculator/roai/portfolio-calculator-baln-buy-and-sell-in-two-activities.spec.ts

@ -176,9 +176,7 @@ describe('PortfolioCalculator', () => {
netPerformancePercentageWithCurrencyEffectMap: { netPerformancePercentageWithCurrencyEffectMap: {
max: new Big('-0.0552834149755073478') max: new Big('-0.0552834149755073478')
}, },
netPerformanceWithCurrencyEffectMap: { netPerformanceWithCurrencyEffectMap: { max: new Big('-15.8') },
max: new Big('-15.8')
},
marketPrice: 148.9, marketPrice: 148.9,
marketPriceInBaseCurrency: 148.9, marketPriceInBaseCurrency: 148.9,
quantity: new Big('0'), quantity: new Big('0'),

4
apps/api/src/app/portfolio/calculator/roai/portfolio-calculator-baln-buy-and-sell.spec.ts

@ -163,9 +163,7 @@ describe('PortfolioCalculator', () => {
netPerformancePercentageWithCurrencyEffectMap: { netPerformancePercentageWithCurrencyEffectMap: {
max: new Big('-0.0552834149755073478') max: new Big('-0.0552834149755073478')
}, },
netPerformanceWithCurrencyEffectMap: { netPerformanceWithCurrencyEffectMap: { max: new Big('-15.8') },
max: new Big('-15.8')
},
marketPrice: 148.9, marketPrice: 148.9,
marketPriceInBaseCurrency: 148.9, marketPriceInBaseCurrency: 148.9,
quantity: new Big('0'), quantity: new Big('0'),

34
apps/api/src/app/portfolio/calculator/roai/portfolio-calculator-googl-buy.spec.ts

@ -161,9 +161,7 @@ describe('PortfolioCalculator', () => {
netPerformancePercentageWithCurrencyEffectMap: { netPerformancePercentageWithCurrencyEffectMap: {
max: new Big('0.24112962014285697628') max: new Big('0.24112962014285697628')
}, },
netPerformanceWithCurrencyEffectMap: { netPerformanceWithCurrencyEffectMap: { max: new Big('19.851974') },
max: new Big('19.851974')
},
marketPrice: 116.45, marketPrice: 116.45,
marketPriceInBaseCurrency: 103.10483, marketPriceInBaseCurrency: 103.10483,
quantity: new Big('1'), quantity: new Big('1'),
@ -199,30 +197,12 @@ describe('PortfolioCalculator', () => {
expect(investmentsByMonth).toEqual([ expect(investmentsByMonth).toEqual([
{ date: '2023-01-01', investment: 82.329056 }, { date: '2023-01-01', investment: 82.329056 },
{ { date: '2023-02-01', investment: 0 },
date: '2023-02-01', { date: '2023-03-01', investment: 0 },
investment: 0 { date: '2023-04-01', investment: 0 },
}, { date: '2023-05-01', investment: 0 },
{ { date: '2023-06-01', investment: 0 },
date: '2023-03-01', { date: '2023-07-01', investment: 0 }
investment: 0
},
{
date: '2023-04-01',
investment: 0
},
{
date: '2023-05-01',
investment: 0
},
{
date: '2023-06-01',
investment: 0
},
{
date: '2023-07-01',
investment: 0
}
]); ]);
}); });
}); });

4
apps/api/src/app/portfolio/calculator/roai/portfolio-calculator-novn-buy-and-sell-partially.spec.ts

@ -157,9 +157,7 @@ describe('PortfolioCalculator', () => {
netPerformancePercentageWithCurrencyEffectMap: { netPerformancePercentageWithCurrencyEffectMap: {
max: new Big('0.12348284960422163588') max: new Big('0.12348284960422163588')
}, },
netPerformanceWithCurrencyEffectMap: { netPerformanceWithCurrencyEffectMap: { max: new Big('17.68') },
max: new Big('17.68')
},
marketPrice: 87.8, marketPrice: 87.8,
marketPriceInBaseCurrency: 87.8, marketPriceInBaseCurrency: 87.8,
quantity: new Big('1'), quantity: new Big('1'),

9
apps/api/src/app/portfolio/calculator/roai/portfolio-calculator.ts

@ -221,7 +221,8 @@ export class RoaiPortfolioCalculator extends PortfolioCalculator {
totalLiabilities: new Big(0), totalLiabilities: new Big(0),
totalLiabilitiesInBaseCurrency: new Big(0), totalLiabilitiesInBaseCurrency: new Big(0),
totalValuables: new Big(0), totalValuables: new Big(0),
totalValuablesInBaseCurrency: new Big(0) totalValuablesInBaseCurrency: new Big(0),
unitPrices: {}
}; };
} }
@ -271,7 +272,8 @@ export class RoaiPortfolioCalculator extends PortfolioCalculator {
totalLiabilities: new Big(0), totalLiabilities: new Big(0),
totalLiabilitiesInBaseCurrency: new Big(0), totalLiabilitiesInBaseCurrency: new Big(0),
totalValuables: new Big(0), totalValuables: new Big(0),
totalValuablesInBaseCurrency: new Big(0) totalValuablesInBaseCurrency: new Big(0),
unitPrices: {}
}; };
} }
@ -963,7 +965,8 @@ export class RoaiPortfolioCalculator extends PortfolioCalculator {
timeWeightedInvestment: timeWeightedInvestment:
timeWeightedAverageInvestmentBetweenStartAndEndDate, timeWeightedAverageInvestmentBetweenStartAndEndDate,
timeWeightedInvestmentWithCurrencyEffect: timeWeightedInvestmentWithCurrencyEffect:
timeWeightedAverageInvestmentBetweenStartAndEndDateWithCurrencyEffect timeWeightedAverageInvestmentBetweenStartAndEndDateWithCurrencyEffect,
unitPrices: marketSymbolMap[endDateString]
}; };
} }
} }

1
apps/api/src/app/portfolio/interfaces/portfolio-holding-detail.interface.ts

@ -11,6 +11,7 @@ export interface PortfolioHoldingDetail {
averagePrice: number; averagePrice: number;
dataProviderInfo: DataProviderInfo; dataProviderInfo: DataProviderInfo;
dividendInBaseCurrency: number; dividendInBaseCurrency: number;
stakeRewards: number;
dividendYieldPercent: number; dividendYieldPercent: number;
dividendYieldPercentWithCurrencyEffect: number; dividendYieldPercentWithCurrencyEffect: number;
feeInBaseCurrency: number; feeInBaseCurrency: number;

5
apps/api/src/app/portfolio/interfaces/portfolio-order-item.interface.ts

@ -10,3 +10,8 @@ export interface PortfolioOrderItem extends PortfolioOrder {
unitPriceInBaseCurrency?: Big; unitPriceInBaseCurrency?: Big;
unitPriceInBaseCurrencyWithCurrencyEffect?: Big; unitPriceInBaseCurrencyWithCurrencyEffect?: Big;
} }
export interface WithCurrencyEffect<T> {
Value: T;
WithCurrencyEffect: T;
}

32
apps/api/src/app/portfolio/portfolio.controller.ts

@ -5,7 +5,10 @@ import {
hasNotDefinedValuesInObject, hasNotDefinedValuesInObject,
nullifyValuesInObject nullifyValuesInObject
} from '@ghostfolio/api/helper/object.helper'; } from '@ghostfolio/api/helper/object.helper';
import { PerformanceLoggingInterceptor } from '@ghostfolio/api/interceptors/performance-logging/performance-logging.interceptor'; import {
LogPerformance,
PerformanceLoggingInterceptor
} from '@ghostfolio/api/interceptors/performance-logging/performance-logging.interceptor';
import { RedactValuesInResponseInterceptor } from '@ghostfolio/api/interceptors/redact-values-in-response/redact-values-in-response.interceptor'; import { RedactValuesInResponseInterceptor } from '@ghostfolio/api/interceptors/redact-values-in-response/redact-values-in-response.interceptor';
import { TransformDataSourceInRequestInterceptor } from '@ghostfolio/api/interceptors/transform-data-source-in-request/transform-data-source-in-request.interceptor'; import { TransformDataSourceInRequestInterceptor } from '@ghostfolio/api/interceptors/transform-data-source-in-request/transform-data-source-in-request.interceptor';
import { TransformDataSourceInResponseInterceptor } from '@ghostfolio/api/interceptors/transform-data-source-in-response/transform-data-source-in-response.interceptor'; import { TransformDataSourceInResponseInterceptor } from '@ghostfolio/api/interceptors/transform-data-source-in-response/transform-data-source-in-response.interceptor';
@ -159,6 +162,23 @@ export class PortfolioController {
portfolioPosition.investment / totalInvestment; portfolioPosition.investment / totalInvestment;
portfolioPosition.valueInPercentage = portfolioPosition.valueInPercentage =
portfolioPosition.valueInBaseCurrency / totalValue; portfolioPosition.valueInBaseCurrency / totalValue;
portfolioPosition.assetClass = hasDetails
? portfolioPosition.assetClass
: undefined;
portfolioPosition.assetSubClass = hasDetails
? portfolioPosition.assetSubClass
: undefined;
portfolioPosition.countries = hasDetails
? portfolioPosition.countries
: [];
portfolioPosition.currency = hasDetails
? portfolioPosition.currency
: undefined;
portfolioPosition.markets = hasDetails
? portfolioPosition.markets
: undefined;
portfolioPosition.sectors = hasDetails ? portfolioPosition.sectors : [];
portfolioPosition.tags = hasDetails ? portfolioPosition.tags : [];
} }
for (const [name, { valueInBaseCurrency }] of Object.entries(accounts)) { for (const [name, { valueInBaseCurrency }] of Object.entries(accounts)) {
@ -225,6 +245,7 @@ export class PortfolioController {
currency: hasDetails ? portfolioPosition.currency : undefined, currency: hasDetails ? portfolioPosition.currency : undefined,
holdings: hasDetails ? portfolioPosition.holdings : [], holdings: hasDetails ? portfolioPosition.holdings : [],
markets: hasDetails ? portfolioPosition.markets : undefined, markets: hasDetails ? portfolioPosition.markets : undefined,
tags: hasDetails ? portfolioPosition.tags : [],
marketsAdvanced: hasDetails marketsAdvanced: hasDetails
? portfolioPosition.marketsAdvanced ? portfolioPosition.marketsAdvanced
: undefined, : undefined,
@ -476,6 +497,7 @@ export class PortfolioController {
@UseInterceptors(TransformDataSourceInRequestInterceptor) @UseInterceptors(TransformDataSourceInRequestInterceptor)
@UseInterceptors(TransformDataSourceInResponseInterceptor) @UseInterceptors(TransformDataSourceInResponseInterceptor)
@Version('2') @Version('2')
@LogPerformance
public async getPerformanceV2( public async getPerformanceV2(
@Headers(HEADER_KEY_IMPERSONATION.toLowerCase()) impersonationId: string, @Headers(HEADER_KEY_IMPERSONATION.toLowerCase()) impersonationId: string,
@Query('accounts') filterByAccounts?: string, @Query('accounts') filterByAccounts?: string,
@ -484,10 +506,9 @@ export class PortfolioController {
@Query('range') dateRange: DateRange = 'max', @Query('range') dateRange: DateRange = 'max',
@Query('symbol') filterBySymbol?: string, @Query('symbol') filterBySymbol?: string,
@Query('tags') filterByTags?: string, @Query('tags') filterByTags?: string,
@Query('withExcludedAccounts') withExcludedAccountsParam = 'false' @Query('withExcludedAccounts') withExcludedAccounts = false,
@Query('timeWeightedPerformance') calculateTimeWeightedPerformance = false
): Promise<PortfolioPerformanceResponse> { ): Promise<PortfolioPerformanceResponse> {
const withExcludedAccounts = withExcludedAccountsParam === 'true';
const filters = this.apiService.buildFiltersFromQueryParams({ const filters = this.apiService.buildFiltersFromQueryParams({
filterByAccounts, filterByAccounts,
filterByAssetClasses, filterByAssetClasses,
@ -501,7 +522,8 @@ export class PortfolioController {
filters, filters,
impersonationId, impersonationId,
withExcludedAccounts, withExcludedAccounts,
userId: this.request.user.id userId: this.request.user.id,
calculateTimeWeightedPerformance
}); });
if ( if (

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

@ -5,6 +5,7 @@ import { Activity } from '@ghostfolio/api/app/order/interfaces/activities.interf
import { OrderService } from '@ghostfolio/api/app/order/order.service'; import { OrderService } from '@ghostfolio/api/app/order/order.service';
import { UserService } from '@ghostfolio/api/app/user/user.service'; import { UserService } from '@ghostfolio/api/app/user/user.service';
import { getFactor } from '@ghostfolio/api/helper/portfolio.helper'; import { getFactor } from '@ghostfolio/api/helper/portfolio.helper';
import { LogPerformance } from '@ghostfolio/api/interceptors/performance-logging/performance-logging.interceptor';
import { AccountClusterRiskCurrentInvestment } from '@ghostfolio/api/models/rules/account-cluster-risk/current-investment'; import { AccountClusterRiskCurrentInvestment } from '@ghostfolio/api/models/rules/account-cluster-risk/current-investment';
import { AccountClusterRiskSingleAccount } from '@ghostfolio/api/models/rules/account-cluster-risk/single-account'; import { AccountClusterRiskSingleAccount } from '@ghostfolio/api/models/rules/account-cluster-risk/single-account';
import { AssetClassClusterRiskEquity } from '@ghostfolio/api/models/rules/asset-class-cluster-risk/equity'; import { AssetClassClusterRiskEquity } from '@ghostfolio/api/models/rules/asset-class-cluster-risk/equity';
@ -82,8 +83,9 @@ import {
parseISO, parseISO,
set set
} from 'date-fns'; } from 'date-fns';
import { isEmpty } from 'lodash'; import { isEmpty, uniqBy } from 'lodash';
import { CPRPortfolioCalculator } from './calculator/constantPortfolioReturn/portfolio-calculator';
import { PortfolioCalculator } from './calculator/portfolio-calculator'; import { PortfolioCalculator } from './calculator/portfolio-calculator';
import { import {
PerformanceCalculationType, PerformanceCalculationType,
@ -113,6 +115,7 @@ export class PortfolioService {
private readonly userService: UserService private readonly userService: UserService
) {} ) {}
@LogPerformance
public async getAccounts({ public async getAccounts({
filters, filters,
userId, userId,
@ -203,6 +206,7 @@ export class PortfolioService {
}); });
} }
@LogPerformance
public async getAccountsWithAggregations({ public async getAccountsWithAggregations({
filters, filters,
userId, userId,
@ -239,6 +243,7 @@ export class PortfolioService {
}; };
} }
@LogPerformance
public async getDividends({ public async getDividends({
activities, activities,
groupBy groupBy
@ -260,6 +265,7 @@ export class PortfolioService {
return dividends; return dividends;
} }
@LogPerformance
public async getInvestments({ public async getInvestments({
dateRange, dateRange,
filters, filters,
@ -336,6 +342,7 @@ export class PortfolioService {
}; };
} }
@LogPerformance
public async getDetails({ public async getDetails({
dateRange = 'max', dateRange = 'max',
filters, filters,
@ -484,13 +491,17 @@ export class PortfolioService {
})); }));
} }
const tagsInternal = tags.concat(
symbolProfiles.find((sp) => sp.symbol === symbol)?.tags ?? []
);
holdings[symbol] = { holdings[symbol] = {
currency, currency,
markets, markets,
marketsAdvanced, marketsAdvanced,
marketPrice, marketPrice,
symbol, symbol,
tags, tags: uniqBy(tagsInternal, 'id'),
transactionCount, transactionCount,
allocationInPercentage: filteredValueInBaseCurrency.eq(0) allocationInPercentage: filteredValueInBaseCurrency.eq(0)
? 0 ? 0
@ -627,6 +638,7 @@ export class PortfolioService {
}; };
} }
@LogPerformance
public async getPosition( public async getPosition(
aDataSource: DataSource, aDataSource: DataSource,
aImpersonationId: string, aImpersonationId: string,
@ -646,6 +658,7 @@ export class PortfolioService {
return { return {
averagePrice: undefined, averagePrice: undefined,
dataProviderInfo: undefined, dataProviderInfo: undefined,
stakeRewards: undefined,
dividendInBaseCurrency: undefined, dividendInBaseCurrency: undefined,
dividendYieldPercent: undefined, dividendYieldPercent: undefined,
dividendYieldPercentWithCurrencyEffect: undefined, dividendYieldPercentWithCurrencyEffect: undefined,
@ -736,6 +749,16 @@ export class PortfolioService {
) )
}); });
const stakeRewards = getSum(
activities
.filter(({ SymbolProfile, type }) => {
return symbol === SymbolProfile.symbol && type === 'STAKE';
})
.map(({ quantity }) => {
return new Big(quantity);
})
);
const historicalData = await this.dataProviderService.getHistorical( const historicalData = await this.dataProviderService.getHistorical(
[{ dataSource, symbol: aSymbol }], [{ dataSource, symbol: aSymbol }],
'day', 'day',
@ -806,6 +829,7 @@ export class PortfolioService {
transactionCount, transactionCount,
averagePrice: averagePrice.toNumber(), averagePrice: averagePrice.toNumber(),
dataProviderInfo: portfolioCalculator.getDataProviderInfos()?.[0], dataProviderInfo: portfolioCalculator.getDataProviderInfos()?.[0],
stakeRewards: stakeRewards.toNumber(),
dividendInBaseCurrency: dividendInBaseCurrency.toNumber(), dividendInBaseCurrency: dividendInBaseCurrency.toNumber(),
dividendYieldPercent: dividendYieldPercent.toNumber(), dividendYieldPercent: dividendYieldPercent.toNumber(),
dividendYieldPercentWithCurrencyEffect: dividendYieldPercentWithCurrencyEffect:
@ -823,7 +847,7 @@ export class PortfolioService {
grossPerformanceWithCurrencyEffect: grossPerformanceWithCurrencyEffect:
position.grossPerformanceWithCurrencyEffect?.toNumber(), position.grossPerformanceWithCurrencyEffect?.toNumber(),
historicalData: historicalDataArray, historicalData: historicalDataArray,
investment: position.investment?.toNumber(), investment: position.investmentWithCurrencyEffect?.toNumber(),
netPerformance: position.netPerformance?.toNumber(), netPerformance: position.netPerformance?.toNumber(),
netPerformancePercent: position.netPerformancePercentage?.toNumber(), netPerformancePercent: position.netPerformancePercentage?.toNumber(),
netPerformancePercentWithCurrencyEffect: netPerformancePercentWithCurrencyEffect:
@ -893,6 +917,7 @@ export class PortfolioService {
SymbolProfile, SymbolProfile,
averagePrice: 0, averagePrice: 0,
dataProviderInfo: undefined, dataProviderInfo: undefined,
stakeRewards: 0,
dividendInBaseCurrency: 0, dividendInBaseCurrency: 0,
dividendYieldPercent: 0, dividendYieldPercent: 0,
dividendYieldPercentWithCurrencyEffect: 0, dividendYieldPercentWithCurrencyEffect: 0,
@ -917,6 +942,7 @@ export class PortfolioService {
} }
} }
@LogPerformance
public async getPositions({ public async getPositions({
dateRange = 'max', dateRange = 'max',
filters, filters,
@ -1046,6 +1072,7 @@ export class PortfolioService {
dataProviderResponses[symbol]?.marketState ?? 'delayed', dataProviderResponses[symbol]?.marketState ?? 'delayed',
name: symbolProfileMap[symbol].name, name: symbolProfileMap[symbol].name,
netPerformance: netPerformance?.toNumber() ?? null, netPerformance: netPerformance?.toNumber() ?? null,
tags: symbolProfileMap[symbol].tags,
netPerformancePercentage: netPerformancePercentage:
netPerformancePercentage?.toNumber() ?? null, netPerformancePercentage?.toNumber() ?? null,
netPerformancePercentageWithCurrencyEffect: netPerformancePercentageWithCurrencyEffect:
@ -1065,17 +1092,20 @@ export class PortfolioService {
}; };
} }
@LogPerformance
public async getPerformance({ public async getPerformance({
dateRange = 'max', dateRange = 'max',
filters, filters,
impersonationId, impersonationId,
userId userId,
calculateTimeWeightedPerformance = false
}: { }: {
dateRange?: DateRange; dateRange?: DateRange;
filters?: Filter[]; filters?: Filter[];
impersonationId: string; impersonationId: string;
userId: string; userId: string;
withExcludedAccounts?: boolean; withExcludedAccounts?: boolean;
calculateTimeWeightedPerformance?: boolean;
}): Promise<PortfolioPerformanceResponse> { }): Promise<PortfolioPerformanceResponse> {
userId = await this.getUserId(impersonationId, userId); userId = await this.getUserId(impersonationId, userId);
const user = await this.userService.user({ id: userId }); const user = await this.userService.user({ id: userId });
@ -1120,15 +1150,14 @@ export class PortfolioService {
currency: userCurrency currency: userCurrency
}); });
const { errors, hasErrors, historicalData } =
await portfolioCalculator.getSnapshot();
const { endDate, startDate } = getIntervalFromDateRange(dateRange); const { endDate, startDate } = getIntervalFromDateRange(dateRange);
const range = { end: endDate, start: startDate };
const { chart } = await portfolioCalculator.getPerformance({ const { chart } = await (calculateTimeWeightedPerformance
end: endDate, ? (
start: startDate portfolioCalculator as CPRPortfolioCalculator
}); ).getPerformanceWithTimeWeightedReturn(range)
: portfolioCalculator.getPerformance(range));
const { const {
netPerformance, netPerformance,
@ -1150,9 +1179,8 @@ export class PortfolioService {
return { return {
chart, chart,
errors, hasErrors: false,
hasErrors, firstOrderDate: parseDate(chart[0]?.date),
firstOrderDate: parseDate(historicalData[0]?.date),
performance: { performance: {
netPerformance, netPerformance,
netPerformanceWithCurrencyEffect, netPerformanceWithCurrencyEffect,
@ -1166,6 +1194,7 @@ export class PortfolioService {
}; };
} }
@LogPerformance
public async getReport( public async getReport(
impersonationId: string impersonationId: string
): Promise<PortfolioReportResponse> { ): Promise<PortfolioReportResponse> {
@ -1320,6 +1349,7 @@ export class PortfolioService {
return { rules, statistics: this.getReportStatistics(rules) }; return { rules, statistics: this.getReportStatistics(rules) };
} }
@LogPerformance
public async updateTags({ public async updateTags({
dataSource, dataSource,
impersonationId, impersonationId,
@ -1334,7 +1364,6 @@ export class PortfolioService {
userId: string; userId: string;
}) { }) {
userId = await this.getUserId(impersonationId, userId); userId = await this.getUserId(impersonationId, userId);
await this.orderService.assignTags({ dataSource, symbol, tags, userId }); await this.orderService.assignTags({ dataSource, symbol, tags, userId });
} }
@ -1477,6 +1506,7 @@ export class PortfolioService {
return { markets, marketsAdvanced }; return { markets, marketsAdvanced };
} }
@LogPerformance
private async getCashPositions({ private async getCashPositions({
cashDetails, cashDetails,
userCurrency, userCurrency,
@ -1589,6 +1619,7 @@ export class PortfolioService {
return dividendsByGroup; return dividendsByGroup;
} }
@LogPerformance
private getEmergencyFundHoldingsValueInBaseCurrency({ private getEmergencyFundHoldingsValueInBaseCurrency({
holdings holdings
}: { }: {
@ -1736,6 +1767,7 @@ export class PortfolioService {
return { markets, marketsAdvanced }; return { markets, marketsAdvanced };
} }
@LogPerformance
private getReportStatistics( private getReportStatistics(
evaluatedRules: PortfolioReportResponse['rules'] evaluatedRules: PortfolioReportResponse['rules']
): PortfolioReportResponse['statistics'] { ): PortfolioReportResponse['statistics'] {
@ -1754,6 +1786,7 @@ export class PortfolioService {
return { rulesActiveCount, rulesFulfilledCount }; return { rulesActiveCount, rulesFulfilledCount };
} }
@LogPerformance
private getStreaks({ private getStreaks({
investments, investments,
savingsRate savingsRate
@ -1776,6 +1809,7 @@ export class PortfolioService {
return { currentStreak, longestStreak }; return { currentStreak, longestStreak };
} }
@LogPerformance
private async getSummary({ private async getSummary({
balanceInBaseCurrency, balanceInBaseCurrency,
emergencyFundHoldingsValueInBaseCurrency, emergencyFundHoldingsValueInBaseCurrency,
@ -1801,7 +1835,6 @@ export class PortfolioService {
userId, userId,
withExcludedAccounts: true withExcludedAccounts: true
}); });
const excludedActivities: Activity[] = []; const excludedActivities: Activity[] = [];
const nonExcludedActivities: Activity[] = []; const nonExcludedActivities: Activity[] = [];
@ -1864,7 +1897,9 @@ export class PortfolioService {
.plus(emergencyFundHoldingsValueInBaseCurrency) .plus(emergencyFundHoldingsValueInBaseCurrency)
.toNumber(); .toNumber();
const committedFunds = new Big(totalBuy).minus(totalSell); const committedFunds = new Big(totalBuy)
.minus(totalSell)
.minus(dividendInBaseCurrency);
const totalOfExcludedActivities = this.getSumOfActivityType({ const totalOfExcludedActivities = this.getSumOfActivityType({
userCurrency, userCurrency,
@ -1884,7 +1919,6 @@ export class PortfolioService {
currency: userCurrency, currency: userCurrency,
withExcludedAccounts: true withExcludedAccounts: true
}); });
const excludedBalanceInBaseCurrency = new Big( const excludedBalanceInBaseCurrency = new Big(
cashDetailsWithExcludedAccounts.balanceInBaseCurrency cashDetailsWithExcludedAccounts.balanceInBaseCurrency
).minus(balanceInBaseCurrency); ).minus(balanceInBaseCurrency);
@ -1893,12 +1927,17 @@ export class PortfolioService {
.plus(totalOfExcludedActivities) .plus(totalOfExcludedActivities)
.toNumber(); .toNumber();
const netWorth = new Big(balanceInBaseCurrency) const netWorth =
.plus(currentValueInBaseCurrency) portfolioCalculator instanceof CPRPortfolioCalculator
.plus(valuables) ? await (portfolioCalculator as CPRPortfolioCalculator)
.plus(excludedAccountsAndActivities) .getUnfilteredNetWorth(this.getUserCurrency())
.minus(liabilities) .then((value) => value.toNumber())
.toNumber(); : new Big(balanceInBaseCurrency)
.plus(currentValueInBaseCurrency)
.plus(valuables)
.plus(excludedAccountsAndActivities)
.minus(liabilities)
.toNumber();
const daysInMarket = differenceInDays(new Date(), firstOrderDate); const daysInMarket = differenceInDays(new Date(), firstOrderDate);
@ -1961,6 +2000,7 @@ export class PortfolioService {
}; };
} }
@LogPerformance
private getSumOfActivityType({ private getSumOfActivityType({
activities, activities,
activityType, activityType,
@ -2017,6 +2057,7 @@ export class PortfolioService {
return impersonationUserId || aUserId; return impersonationUserId || aUserId;
} }
@LogPerformance
private async getValueOfAccountsAndPlatforms({ private async getValueOfAccountsAndPlatforms({
activities, activities,
filters = [], filters = [],

82
apps/api/src/app/tag/tag.service.ts

@ -0,0 +1,82 @@
import { PrismaService } from '@ghostfolio/api/services/prisma/prisma.service';
import { Injectable } from '@nestjs/common';
import { Prisma, Tag } from '@prisma/client';
@Injectable()
export class TagService {
public constructor(private readonly prismaService: PrismaService) {}
public async createTag(data: Prisma.TagCreateInput) {
return this.prismaService.tag.create({
data
});
}
public async deleteTag(where: Prisma.TagWhereUniqueInput): Promise<Tag> {
return this.prismaService.tag.delete({ where });
}
public async getTag(
tagWhereUniqueInput: Prisma.TagWhereUniqueInput
): Promise<Tag> {
return this.prismaService.tag.findUnique({
where: tagWhereUniqueInput
});
}
public async getTags({
cursor,
orderBy,
skip,
take,
where
}: {
cursor?: Prisma.TagWhereUniqueInput;
orderBy?: Prisma.TagOrderByWithRelationInput;
skip?: number;
take?: number;
where?: Prisma.TagWhereInput;
} = {}) {
return this.prismaService.tag.findMany({
cursor,
orderBy,
skip,
take,
where
});
}
public async getTagsWithActivityCount() {
const tagsWithOrderCount = await this.prismaService.tag.findMany({
include: {
_count: {
select: { orders: true, symbolProfile: true }
}
}
});
return tagsWithOrderCount.map(({ _count, id, name, userId }) => {
return {
id,
name,
userId,
activityCount: _count.orders,
holdingCount: _count.symbolProfile
};
});
}
public async updateTag({
data,
where
}: {
data: Prisma.TagUpdateInput;
where: Prisma.TagWhereUniqueInput;
}): Promise<Tag> {
return this.prismaService.tag.update({
data,
where
});
}
}

3
apps/api/src/app/user/update-user-setting.dto.ts

@ -37,6 +37,9 @@ export class UpdateUserSettingDto {
@IsIn([ @IsIn([
'1d', '1d',
'1w',
'1m',
'3m',
'1y', '1y',
'5y', '5y',
'max', 'max',

6
apps/api/src/app/user/user.controller.ts

@ -183,6 +183,12 @@ export class UserController {
} }
} }
for (const key in data) {
if (data[key] !== false && data[key] !== null) {
userSettings[key] = data[key];
}
}
return this.userService.updateUserSetting({ return this.userService.updateUserSetting({
emitPortfolioChangedEvent, emitPortfolioChangedEvent,
userSettings, userSettings,

25
apps/api/src/helper/dateQueryHelper.ts

@ -0,0 +1,25 @@
import { resetHours } from '@ghostfolio/common/helper';
import { addDays } from 'date-fns';
import { DateQuery } from '../app/portfolio/interfaces/date-query.interface';
export class DateQueryHelper {
public handleDateQueryIn(dateQuery: DateQuery): {
query: DateQuery;
dates: Date[];
} {
let dates = [];
let query = dateQuery;
if (dateQuery.in?.length > 0) {
dates = dateQuery.in;
const end = Math.max(...dates.map((d) => d.getTime()));
const start = Math.min(...dates.map((d) => d.getTime()));
query = {
gte: resetHours(new Date(start)),
lt: resetHours(addDays(end, 1))
};
}
return { query, dates };
}
}

1
apps/api/src/helper/portfolio.helper.ts

@ -5,6 +5,7 @@ export function getFactor(activityType: ActivityType) {
switch (activityType) { switch (activityType) {
case 'BUY': case 'BUY':
case 'STAKE':
factor = 1; factor = 1;
break; break;
case 'SELL': case 'SELL':

2
apps/api/src/interceptors/performance-logging/performance-logging.interceptor.ts

@ -43,7 +43,7 @@ export function LogPerformance(
) { ) {
const originalMethod = descriptor.value; const originalMethod = descriptor.value;
descriptor.value = async function (...args: any[]) { descriptor.value = function (...args: any[]) {
const startTime = performance.now(); const startTime = performance.now();
const performanceLoggingService = new PerformanceLoggingService(); const performanceLoggingService = new PerformanceLoggingService();

7
apps/api/src/services/data-provider/data-enhancer/trackinsight/trackinsight.service.ts

@ -14,7 +14,6 @@ export class TrackinsightDataEnhancerService implements DataEnhancerInterface {
private static countriesMapping = { private static countriesMapping = {
'Russian Federation': 'Russia' 'Russian Federation': 'Russia'
}; };
private static holdingsWeightTreshold = 0.85;
private static sectorsMapping = { private static sectorsMapping = {
'Consumer Discretionary': 'Consumer Cyclical', 'Consumer Discretionary': 'Consumer Cyclical',
'Consumer Defensive': 'Consumer Staples', 'Consumer Defensive': 'Consumer Staples',
@ -94,10 +93,8 @@ export class TrackinsightDataEnhancerService implements DataEnhancerInterface {
return {}; return {};
}); });
if ( if (holdings?.weight < 1 - Math.min(holdings?.count * 0.000015, 0.95)) {
holdings?.weight < TrackinsightDataEnhancerService.holdingsWeightTreshold // Skip if data is inaccurate, dependent on holdings count there might be rounding issues
) {
// Skip if data is inaccurate
return response; return response;
} }

25
apps/api/src/services/data-provider/data-provider.service.ts

@ -1,4 +1,5 @@
import { RedisCacheService } from '@ghostfolio/api/app/redis-cache/redis-cache.service'; import { RedisCacheService } from '@ghostfolio/api/app/redis-cache/redis-cache.service';
import { LogPerformance } from '@ghostfolio/api/interceptors/performance-logging/performance-logging.interceptor';
import { ConfigurationService } from '@ghostfolio/api/services/configuration/configuration.service'; import { ConfigurationService } from '@ghostfolio/api/services/configuration/configuration.service';
import { DataProviderInterface } from '@ghostfolio/api/services/data-provider/interfaces/data-provider.interface'; import { DataProviderInterface } from '@ghostfolio/api/services/data-provider/interfaces/data-provider.interface';
import { import {
@ -386,6 +387,7 @@ export class DataProviderService {
return result; return result;
} }
@LogPerformance
public async getQuotes({ public async getQuotes({
items, items,
requestTimeout, requestTimeout,
@ -515,6 +517,8 @@ export class DataProviderService {
} }
response[symbol] = dataProviderResponse; response[symbol] = dataProviderResponse;
const quotesCacheTTL =
this.getAppropriateCacheTTL(dataProviderResponse);
this.redisCacheService.set( this.redisCacheService.set(
this.redisCacheService.getQuoteKey({ this.redisCacheService.getQuoteKey({
@ -522,7 +526,7 @@ export class DataProviderService {
dataSource: DataSource[dataSource] dataSource: DataSource[dataSource]
}), }),
JSON.stringify(response[symbol]), JSON.stringify(response[symbol]),
this.configurationService.get('CACHE_QUOTES_TTL') quotesCacheTTL
); );
for (const { for (const {
@ -605,6 +609,25 @@ export class DataProviderService {
return response; return response;
} }
private getAppropriateCacheTTL(dataProviderResponse: IDataProviderResponse) {
let quotesCacheTTL = this.configurationService.get('CACHE_QUOTES_TTL');
if (dataProviderResponse.dataSource === 'MANUAL') {
quotesCacheTTL = 14400; // 4h Cache for Manual Service
} else if (dataProviderResponse.marketState === 'closed') {
const date = new Date();
const dayOfWeek = date.getDay();
if (dayOfWeek === 0 || dayOfWeek === 6) {
quotesCacheTTL = 14400;
} else if (date.getHours() > 16) {
quotesCacheTTL = 14400;
} else {
quotesCacheTTL = 900;
}
}
return quotesCacheTTL;
}
public async search({ public async search({
includeIndices = false, includeIndices = false,
query, query,

32
apps/api/src/services/data-provider/manual/manual.service.ts

@ -13,6 +13,7 @@ import {
} from '@ghostfolio/api/services/interfaces/interfaces'; } from '@ghostfolio/api/services/interfaces/interfaces';
import { PrismaService } from '@ghostfolio/api/services/prisma/prisma.service'; import { PrismaService } from '@ghostfolio/api/services/prisma/prisma.service';
import { SymbolProfileService } from '@ghostfolio/api/services/symbol-profile/symbol-profile.service'; import { SymbolProfileService } from '@ghostfolio/api/services/symbol-profile/symbol-profile.service';
import { BatchPrismaClient } from '@ghostfolio/common/chunkhelper';
import { import {
DATE_FORMAT, DATE_FORMAT,
extractNumberFromString, extractNumberFromString,
@ -146,18 +147,25 @@ export class ManualService implements DataProviderInterface {
}) })
); );
const marketData = await this.prismaService.marketData.findMany({ const batch = new BatchPrismaClient(this.prismaService);
distinct: ['symbol'],
orderBy: { const marketData = await batch
date: 'desc' .over(symbols)
}, .with((prisma, _symbols) =>
take: symbols.length, prisma.marketData.findMany({
where: { distinct: ['symbol'],
symbol: { orderBy: {
in: symbols date: 'desc'
} },
} take: symbols.length,
}); where: {
symbol: {
in: _symbols
}
}
})
)
.then((_result) => _result.flat());
const symbolProfilesWithScraperConfigurationAndInstantMode = const symbolProfilesWithScraperConfigurationAndInstantMode =
symbolProfiles.filter(({ scraperConfiguration }) => { symbolProfiles.filter(({ scraperConfiguration }) => {

9
apps/api/src/services/market-data/market-data.service.ts

@ -12,11 +12,14 @@ import {
MarketDataState, MarketDataState,
Prisma Prisma
} from '@prisma/client'; } from '@prisma/client';
import AwaitLock from 'await-lock';
@Injectable() @Injectable()
export class MarketDataService { export class MarketDataService {
public constructor(private readonly prismaService: PrismaService) {} public constructor(private readonly prismaService: PrismaService) {}
lock = new AwaitLock();
public async deleteMany({ dataSource, symbol }: AssetProfileIdentifier) { public async deleteMany({ dataSource, symbol }: AssetProfileIdentifier) {
return this.prismaService.marketData.deleteMany({ return this.prismaService.marketData.deleteMany({
where: { where: {
@ -133,7 +136,6 @@ export class MarketDataService {
where: Prisma.MarketDataWhereUniqueInput; where: Prisma.MarketDataWhereUniqueInput;
}): Promise<MarketData> { }): Promise<MarketData> {
const { data, where } = params; const { data, where } = params;
return this.prismaService.marketData.upsert({ return this.prismaService.marketData.upsert({
where, where,
create: { create: {
@ -157,7 +159,7 @@ export class MarketDataService {
data: Prisma.MarketDataUpdateInput[]; data: Prisma.MarketDataUpdateInput[];
}): Promise<MarketData[]> { }): Promise<MarketData[]> {
const upsertPromises = data.map( const upsertPromises = data.map(
({ dataSource, date, marketPrice, symbol, state }) => { async ({ dataSource, date, marketPrice, symbol, state }) => {
return this.prismaService.marketData.upsert({ return this.prismaService.marketData.upsert({
create: { create: {
dataSource: dataSource as DataSource, dataSource: dataSource as DataSource,
@ -180,7 +182,6 @@ export class MarketDataService {
}); });
} }
); );
return await Promise.all(upsertPromises);
return this.prismaService.$transaction(upsertPromises);
} }
} }

159
apps/api/src/services/queues/data-gathering/data-gathering.processor.ts

@ -1,20 +1,25 @@
import { DataProviderService } from '@ghostfolio/api/services/data-provider/data-provider.service'; import { DataProviderService } from '@ghostfolio/api/services/data-provider/data-provider.service';
import { IDataGatheringItem } from '@ghostfolio/api/services/interfaces/interfaces'; import {
IDataGatheringItem,
IDataProviderHistoricalResponse
} from '@ghostfolio/api/services/interfaces/interfaces';
import { MarketDataService } from '@ghostfolio/api/services/market-data/market-data.service'; import { MarketDataService } from '@ghostfolio/api/services/market-data/market-data.service';
import { import {
DATA_GATHERING_QUEUE, DATA_GATHERING_QUEUE,
DEFAULT_PROCESSOR_GATHER_ASSET_PROFILE_CONCURRENCY, DEFAULT_PROCESSOR_GATHER_ASSET_PROFILE_CONCURRENCY,
DEFAULT_PROCESSOR_GATHER_HISTORICAL_MARKET_DATA_CONCURRENCY, DEFAULT_PROCESSOR_GATHER_HISTORICAL_MARKET_DATA_CONCURRENCY,
GATHER_ASSET_PROFILE_PROCESS, GATHER_ASSET_PROFILE_PROCESS,
GATHER_HISTORICAL_MARKET_DATA_PROCESS_JOB_NAME GATHER_HISTORICAL_MARKET_DATA_PROCESS_JOB_NAME,
GATHER_MISSING_HISTORICAL_MARKET_DATA_PROCESS_JOB_NAME
} from '@ghostfolio/common/config'; } from '@ghostfolio/common/config';
import { DATE_FORMAT, getStartOfUtcDate } from '@ghostfolio/common/helper'; import { DATE_FORMAT, getStartOfUtcDate } from '@ghostfolio/common/helper';
import { AssetProfileIdentifier } from '@ghostfolio/common/interfaces'; import { AssetProfileIdentifier } from '@ghostfolio/common/interfaces';
import { Process, Processor } from '@nestjs/bull'; import { Process, Processor } from '@nestjs/bull';
import { Injectable, Logger } from '@nestjs/common'; import { Injectable, Logger } from '@nestjs/common';
import { Prisma } from '@prisma/client'; import { DataSource, Prisma } from '@prisma/client';
import { Job } from 'bull'; import { Job } from 'bull';
import { isNumber } from 'class-validator';
import { import {
addDays, addDays,
format, format,
@ -22,7 +27,9 @@ import {
getMonth, getMonth,
getYear, getYear,
isBefore, isBefore,
parseISO parseISO,
eachDayOfInterval,
isEqual
} from 'date-fns'; } from 'date-fns';
import { DataGatheringService } from './data-gathering.service'; import { DataGatheringService } from './data-gathering.service';
@ -150,4 +157,148 @@ export class DataGatheringProcessor {
throw new Error(error); throw new Error(error);
} }
} }
@Process({
concurrency: parseInt(
process.env.PROCESSOR_CONCURRENCY_GATHER_HISTORICAL_MARKET_DATA ??
DEFAULT_PROCESSOR_GATHER_HISTORICAL_MARKET_DATA_CONCURRENCY.toString(),
10
),
name: GATHER_MISSING_HISTORICAL_MARKET_DATA_PROCESS_JOB_NAME
})
public async gatherMissingHistoricalMarketData(job: Job<IDataGatheringItem>) {
try {
const { dataSource, date, symbol } = job.data;
Logger.log(
`Historical market data gathering for missing values has been started for ${symbol} (${dataSource}) at ${format(
date,
DATE_FORMAT
)}`,
`DataGatheringProcessor (${GATHER_HISTORICAL_MARKET_DATA_PROCESS_JOB_NAME})`
);
const entries = await this.marketDataService.marketDataItems({
where: {
AND: {
symbol: {
equals: symbol
},
dataSource: {
equals: dataSource
}
}
},
orderBy: {
date: 'asc'
},
take: 1
});
const firstEntry = entries[0];
const marketData = await this.marketDataService
.getRange({
assetProfileIdentifiers: [{ dataSource, symbol }],
dateQuery: {
gte: addDays(firstEntry.date, -10)
}
})
.then((md) => md.map((m) => m.date));
let dates = eachDayOfInterval(
{
start: firstEntry.date,
end: new Date()
},
{
step: 1
}
);
dates = dates.filter((d) => !marketData.some((md) => isEqual(md, d)));
const historicalData = await this.dataProviderService.getHistoricalRaw({
assetProfileIdentifiers: [{ dataSource, symbol }],
from: firstEntry.date,
to: new Date()
});
const data: Prisma.MarketDataUpdateInput[] =
this.mapToMarketUpsertDataInputs(
dates,
historicalData,
symbol,
dataSource
);
await this.marketDataService.updateMany({ data });
Logger.log(
`Historical market data gathering for missing values has been completed for ${symbol} (${dataSource}) at ${format(
date,
DATE_FORMAT
)}`,
`DataGatheringProcessor (${GATHER_HISTORICAL_MARKET_DATA_PROCESS_JOB_NAME})`
);
} catch (error) {
Logger.error(
error,
`DataGatheringProcessor (${GATHER_HISTORICAL_MARKET_DATA_PROCESS_JOB_NAME})`
);
throw new Error(error);
}
}
private mapToMarketUpsertDataInputs(
missingMarketData: Date[],
historicalData: Record<
string,
Record<string, IDataProviderHistoricalResponse>
>,
symbol: string,
dataSource: DataSource
): Prisma.MarketDataUpdateInput[] {
return missingMarketData.map((date) => {
if (
isNumber(
historicalData[symbol]?.[format(date, DATE_FORMAT)]?.marketPrice
)
) {
return {
date,
symbol,
dataSource,
marketPrice:
historicalData[symbol]?.[format(date, DATE_FORMAT)]?.marketPrice
};
} else {
let earlierDate = date;
let index = 0;
while (
!isNumber(
historicalData[symbol]?.[format(earlierDate, DATE_FORMAT)]
?.marketPrice
)
) {
earlierDate = addDays(earlierDate, -1);
index++;
if (index > 10) {
break;
}
}
if (
isNumber(
historicalData[symbol]?.[format(earlierDate, DATE_FORMAT)]
?.marketPrice
)
) {
return {
date,
symbol,
dataSource,
marketPrice:
historicalData[symbol]?.[format(earlierDate, DATE_FORMAT)]
?.marketPrice
};
}
}
});
}
} }

49
apps/api/src/services/queues/data-gathering/data-gathering.service.ts

@ -13,6 +13,8 @@ import {
DATA_GATHERING_QUEUE_PRIORITY_MEDIUM, DATA_GATHERING_QUEUE_PRIORITY_MEDIUM,
GATHER_HISTORICAL_MARKET_DATA_PROCESS_JOB_NAME, GATHER_HISTORICAL_MARKET_DATA_PROCESS_JOB_NAME,
GATHER_HISTORICAL_MARKET_DATA_PROCESS_JOB_OPTIONS, GATHER_HISTORICAL_MARKET_DATA_PROCESS_JOB_OPTIONS,
GATHER_MISSING_HISTORICAL_MARKET_DATA_PROCESS_JOB_NAME,
GATHER_MISSING_HISTORICAL_MARKET_DATA_PROCESS_JOB_OPTIONS,
PROPERTY_BENCHMARKS PROPERTY_BENCHMARKS
} from '@ghostfolio/common/config'; } from '@ghostfolio/common/config';
import { import {
@ -111,6 +113,24 @@ export class DataGatheringService {
}); });
} }
public async gatherSymbolMissingOnly({
dataSource,
symbol
}: AssetProfileIdentifier) {
const dataGatheringItems = (await this.getSymbolsMax()).filter(
(dataGatheringItem) => {
return (
dataGatheringItem.dataSource === dataSource &&
dataGatheringItem.symbol === symbol
);
}
);
await this.gatherMissingDataSymbols({
dataGatheringItems,
priority: DATA_GATHERING_QUEUE_PRIORITY_HIGH
});
}
public async gatherSymbolForDate({ public async gatherSymbolForDate({
dataSource, dataSource,
date, date,
@ -297,6 +317,35 @@ export class DataGatheringService {
); );
} }
public async gatherMissingDataSymbols({
dataGatheringItems,
priority
}: {
dataGatheringItems: IDataGatheringItem[];
priority: number;
}) {
await this.addJobsToQueue(
dataGatheringItems.map(({ dataSource, date, symbol }) => {
return {
data: {
dataSource,
date,
symbol
},
name: GATHER_MISSING_HISTORICAL_MARKET_DATA_PROCESS_JOB_NAME,
opts: {
...GATHER_MISSING_HISTORICAL_MARKET_DATA_PROCESS_JOB_OPTIONS,
priority,
jobId: `${getAssetProfileIdentifier({
dataSource,
symbol
})}-missing-${format(date, DATE_FORMAT)}`
}
};
})
);
}
public async getAllActiveAssetProfileIdentifiers(): Promise< public async getAllActiveAssetProfileIdentifiers(): Promise<
AssetProfileIdentifier[] AssetProfileIdentifier[]
> { > {

12
apps/api/src/services/symbol-profile/symbol-profile-overwrite.module.ts

@ -0,0 +1,12 @@
import { PrismaModule } from '@ghostfolio/api/services/prisma/prisma.module';
import { Module } from '@nestjs/common';
import { SymbolProfileOverwriteService } from './symbol-profile-overwrite.service';
@Module({
imports: [PrismaModule],
providers: [SymbolProfileOverwriteService],
exports: [SymbolProfileOverwriteService]
})
export class SymbolProfileOverwriteModule {}

69
apps/api/src/services/symbol-profile/symbol-profile-overwrite.service.ts

@ -0,0 +1,69 @@
import { PrismaService } from '@ghostfolio/api/services/prisma/prisma.service';
import { Injectable } from '@nestjs/common';
import { DataSource, Prisma, SymbolProfileOverrides } from '@prisma/client';
@Injectable()
export class SymbolProfileOverwriteService {
public constructor(private readonly prismaService: PrismaService) {}
public async add(
assetProfileOverwrite: Prisma.SymbolProfileOverridesCreateInput
): Promise<SymbolProfileOverrides | never> {
return this.prismaService.symbolProfileOverrides.create({
data: assetProfileOverwrite
});
}
public async delete(symbolProfileId: string) {
return this.prismaService.symbolProfileOverrides.delete({
where: { symbolProfileId: symbolProfileId }
});
}
public updateSymbolProfileOverrides({
assetClass,
assetSubClass,
name,
countries,
sectors,
url,
symbolProfileId
}: Prisma.SymbolProfileOverridesUpdateInput & { symbolProfileId: string }) {
return this.prismaService.symbolProfileOverrides.update({
data: {
assetClass,
assetSubClass,
name,
countries,
sectors,
url
},
where: { symbolProfileId: symbolProfileId }
});
}
public async GetSymbolProfileId(
Symbol: string,
datasource: DataSource
): Promise<string> {
const SymbolProfileId = await this.prismaService.symbolProfile
.findFirst({
where: {
symbol: Symbol,
dataSource: datasource
}
})
.then((s) => s.id);
const symbolProfileIdSaved = await this.prismaService.symbolProfileOverrides
.findFirst({
where: {
symbolProfileId: SymbolProfileId
}
})
.then((s) => s?.symbolProfileId);
return symbolProfileIdSaved;
}
}

56
apps/api/src/services/symbol-profile/symbol-profile.service.ts

@ -1,3 +1,4 @@
import { LogPerformance } from '@ghostfolio/api/interceptors/performance-logging/performance-logging.interceptor';
import { PrismaService } from '@ghostfolio/api/services/prisma/prisma.service'; import { PrismaService } from '@ghostfolio/api/services/prisma/prisma.service';
import { UNKNOWN_KEY } from '@ghostfolio/common/config'; import { UNKNOWN_KEY } from '@ghostfolio/common/config';
import { import {
@ -10,31 +11,19 @@ import { Country } from '@ghostfolio/common/interfaces/country.interface';
import { Sector } from '@ghostfolio/common/interfaces/sector.interface'; import { Sector } from '@ghostfolio/common/interfaces/sector.interface';
import { Injectable } from '@nestjs/common'; import { Injectable } from '@nestjs/common';
import { Prisma, SymbolProfile, SymbolProfileOverrides } from '@prisma/client'; import {
Prisma,
SymbolProfile,
SymbolProfileOverrides,
Tag
} from '@prisma/client';
import { continents, countries } from 'countries-list'; import { continents, countries } from 'countries-list';
@Injectable() @Injectable()
export class SymbolProfileService { export class SymbolProfileService {
public constructor(private readonly prismaService: PrismaService) {} public constructor(private readonly prismaService: PrismaService) {}
public async add( @LogPerformance
assetProfile: Prisma.SymbolProfileCreateInput
): Promise<SymbolProfile | never> {
return this.prismaService.symbolProfile.create({ data: assetProfile });
}
public async delete({ dataSource, symbol }: AssetProfileIdentifier) {
return this.prismaService.symbolProfile.delete({
where: { dataSource_symbol: { dataSource, symbol } }
});
}
public async deleteById(id: string) {
return this.prismaService.symbolProfile.delete({
where: { id }
});
}
public async getActiveSymbolProfilesByUserSubscription({ public async getActiveSymbolProfilesByUserSubscription({
withUserSubscription = false withUserSubscription = false
}: { }: {
@ -70,6 +59,7 @@ export class SymbolProfileService {
}); });
} }
@LogPerformance
public async getSymbolProfiles( public async getSymbolProfiles(
aAssetProfileIdentifiers: AssetProfileIdentifier[] aAssetProfileIdentifiers: AssetProfileIdentifier[]
): Promise<EnhancedSymbolProfile[]> { ): Promise<EnhancedSymbolProfile[]> {
@ -86,6 +76,7 @@ export class SymbolProfileService {
select: { date: true }, select: { date: true },
take: 1 take: 1
}, },
tags: true,
SymbolProfileOverrides: true SymbolProfileOverrides: true
}, },
where: { where: {
@ -102,6 +93,24 @@ export class SymbolProfileService {
}); });
} }
public async add(
assetProfile: Prisma.SymbolProfileCreateInput
): Promise<SymbolProfile | never> {
return this.prismaService.symbolProfile.create({ data: assetProfile });
}
public async delete({ dataSource, symbol }: AssetProfileIdentifier) {
return this.prismaService.symbolProfile.delete({
where: { dataSource_symbol: { dataSource, symbol } }
});
}
public async deleteById(id: string) {
return this.prismaService.symbolProfile.delete({
where: { id }
});
}
public async getSymbolProfilesByIds( public async getSymbolProfilesByIds(
symbolProfileIds: string[] symbolProfileIds: string[]
): Promise<EnhancedSymbolProfile[]> { ): Promise<EnhancedSymbolProfile[]> {
@ -111,7 +120,8 @@ export class SymbolProfileService {
_count: { _count: {
select: { Order: true } select: { Order: true }
}, },
SymbolProfileOverrides: true SymbolProfileOverrides: true,
tags: true
}, },
where: { where: {
id: { id: {
@ -155,6 +165,7 @@ export class SymbolProfileService {
holdings, holdings,
isActive, isActive,
name, name,
tags,
scraperConfiguration, scraperConfiguration,
sectors, sectors,
symbolMapping, symbolMapping,
@ -172,6 +183,7 @@ export class SymbolProfileService {
holdings, holdings,
isActive, isActive,
name, name,
tags,
scraperConfiguration, scraperConfiguration,
sectors, sectors,
symbolMapping, symbolMapping,
@ -188,6 +200,7 @@ export class SymbolProfileService {
Order?: { Order?: {
date: Date; date: Date;
}[]; }[];
tags?: Tag[];
SymbolProfileOverrides: SymbolProfileOverrides; SymbolProfileOverrides: SymbolProfileOverrides;
})[] })[]
): EnhancedSymbolProfile[] { ): EnhancedSymbolProfile[] {
@ -206,7 +219,8 @@ export class SymbolProfileService {
sectors: this.getSectors( sectors: this.getSectors(
symbolProfile?.sectors as unknown as Prisma.JsonArray symbolProfile?.sectors as unknown as Prisma.JsonArray
), ),
symbolMapping: this.getSymbolMapping(symbolProfile) symbolMapping: this.getSymbolMapping(symbolProfile),
tags: symbolProfile?.tags
}; };
item.activitiesCount = symbolProfile._count.Order; item.activitiesCount = symbolProfile._count.Order;

7
apps/api/src/services/tag/tag.service.ts

@ -56,7 +56,8 @@ export class TagService {
where: { where: {
userId userId
} }
} },
symbolProfile: {}
} }
} }
}, },
@ -75,11 +76,11 @@ export class TagService {
} }
}); });
return tags.map(({ _count, id, name, userId }) => ({ return tags.map(({ id, name, userId }) => ({
id, id,
name, name,
userId, userId,
isUsed: _count.orders > 0 isUsed: true
})); }));
} }

6
apps/client-e2e/project.json

@ -3,6 +3,8 @@
"$schema": "../../node_modules/nx/schemas/project-schema.json", "$schema": "../../node_modules/nx/schemas/project-schema.json",
"sourceRoot": "apps/client-e2e/src", "sourceRoot": "apps/client-e2e/src",
"projectType": "application", "projectType": "application",
"tags": [],
"implicitDependencies": ["client"],
"targets": { "targets": {
"e2e": { "e2e": {
"executor": "@nx/cypress:cypress", "executor": "@nx/cypress:cypress",
@ -17,7 +19,5 @@
} }
} }
} }
}, }
"tags": [],
"implicitDependencies": ["client"]
} }

50
apps/client/project.json

@ -2,13 +2,59 @@
"name": "client", "name": "client",
"$schema": "../../node_modules/nx/schemas/project-schema.json", "$schema": "../../node_modules/nx/schemas/project-schema.json",
"projectType": "application", "projectType": "application",
"sourceRoot": "apps/client/src",
"prefix": "gf",
"i18n": {
"locales": {
"ca": {
"baseHref": "/ca/",
"translation": "apps/client/src/locales/messages.ca.xlf"
},
"de": {
"baseHref": "/de/",
"translation": "apps/client/src/locales/messages.de.xlf"
},
"es": {
"baseHref": "/es/",
"translation": "apps/client/src/locales/messages.es.xlf"
},
"fr": {
"baseHref": "/fr/",
"translation": "apps/client/src/locales/messages.fr.xlf"
},
"it": {
"baseHref": "/it/",
"translation": "apps/client/src/locales/messages.it.xlf"
},
"nl": {
"baseHref": "/nl/",
"translation": "apps/client/src/locales/messages.nl.xlf"
},
"pl": {
"baseHref": "/pl/",
"translation": "apps/client/src/locales/messages.pl.xlf"
},
"pt": {
"baseHref": "/pt/",
"translation": "apps/client/src/locales/messages.pt.xlf"
},
"tr": {
"baseHref": "/tr/",
"translation": "apps/client/src/locales/messages.tr.xlf"
},
"zh": {
"baseHref": "/zh/",
"translation": "apps/client/src/locales/messages.zh.xlf"
}
},
"sourceLocale": "en"
},
"tags": [],
"generators": { "generators": {
"@schematics/angular:component": { "@schematics/angular:component": {
"style": "scss" "style": "scss"
} }
}, },
"sourceRoot": "apps/client/src",
"prefix": "gf",
"targets": { "targets": {
"build": { "build": {
"executor": "@nx/angular:webpack-browser", "executor": "@nx/angular:webpack-browser",

11
apps/client/src/app/components/admin-market-data/admin-market-data.component.ts

@ -260,6 +260,17 @@ export class AdminMarketDataComponent
}); });
} }
public onGatherMissing() {
this.adminService
.gatherMissingOnly()
.pipe(takeUntil(this.unsubscribeSubject))
.subscribe(() => {
setTimeout(() => {
window.location.reload();
}, 300);
});
}
public onGatherProfileData() { public onGatherProfileData() {
this.adminService this.adminService
.gatherProfileData() .gatherProfileData()

3
apps/client/src/app/components/admin-market-data/admin-market-data.html

@ -198,6 +198,9 @@
>Gather All Historical Market Data</ng-container >Gather All Historical Market Data</ng-container
> >
</button> </button>
<button mat-menu-item (click)="onGatherMissing()">
<ng-container i18n>Gather All Missing Data</ng-container>
</button>
<button mat-menu-item (click)="onGatherProfileData()"> <button mat-menu-item (click)="onGatherProfileData()">
<ng-container i18n>Gather Profile Data</ng-container> <ng-container i18n>Gather Profile Data</ng-container>
</button> </button>

65
apps/client/src/app/components/admin-market-data/asset-profile-dialog/asset-profile-dialog.component.ts

@ -15,6 +15,7 @@ import {
} from '@ghostfolio/common/interfaces'; } from '@ghostfolio/common/interfaces';
import { translate } from '@ghostfolio/ui/i18n'; import { translate } from '@ghostfolio/ui/i18n';
import { COMMA, ENTER } from '@angular/cdk/keycodes';
import { HttpErrorResponse } from '@angular/common/http'; import { HttpErrorResponse } from '@angular/common/http';
import { import {
ChangeDetectionStrategy, ChangeDetectionStrategy,
@ -35,13 +36,15 @@ import {
Validators Validators
} from '@angular/forms'; } from '@angular/forms';
import { MatCheckboxChange } from '@angular/material/checkbox'; import { MatCheckboxChange } from '@angular/material/checkbox';
import { MatAutocompleteSelectedEvent } from '@angular/material/autocomplete';
import { MAT_DIALOG_DATA, MatDialogRef } from '@angular/material/dialog'; import { MAT_DIALOG_DATA, MatDialogRef } from '@angular/material/dialog';
import { MatSnackBar } from '@angular/material/snack-bar'; import { MatSnackBar } from '@angular/material/snack-bar';
import { import {
AssetClass, AssetClass,
AssetSubClass, AssetSubClass,
MarketData, MarketData,
SymbolProfile SymbolProfile,
Tag
} from '@prisma/client'; } from '@prisma/client';
import { format } from 'date-fns'; import { format } from 'date-fns';
import { StatusCodes } from 'http-status-codes'; import { StatusCodes } from 'http-status-codes';
@ -60,6 +63,8 @@ import { AssetProfileDialogParams } from './interfaces/interfaces';
standalone: false standalone: false
}) })
export class AssetProfileDialog implements OnDestroy, OnInit { export class AssetProfileDialog implements OnDestroy, OnInit {
@ViewChild('tagInput') tagInput: ElementRef<HTMLInputElement>;
public separatorKeysCodes: number[] = [ENTER, COMMA];
private static readonly HISTORICAL_DATA_TEMPLATE = `date;marketPrice\n${format( private static readonly HISTORICAL_DATA_TEMPLATE = `date;marketPrice\n${format(
new Date(), new Date(),
DATE_FORMAT DATE_FORMAT
@ -91,6 +96,8 @@ export class AssetProfileDialog implements OnDestroy, OnInit {
}), }),
isActive: [true], isActive: [true],
name: ['', Validators.required], name: ['', Validators.required],
tags: new FormControl<Tag[]>(undefined),
tagsDisconnected: new FormControl<Tag[]>(undefined),
scraperConfiguration: this.formBuilder.group({ scraperConfiguration: this.formBuilder.group({
defaultMarketPrice: null, defaultMarketPrice: null,
headers: JSON.stringify({}), headers: JSON.stringify({}),
@ -149,6 +156,8 @@ export class AssetProfileDialog implements OnDestroy, OnInit {
[name: string]: { name: string; value: number }; [name: string]: { name: string; value: number };
}; };
public HoldingTags: { id: string; name: string; userId: string }[];
public user: User; public user: User;
private unsubscribeSubject = new Subject<void>(); private unsubscribeSubject = new Subject<void>();
@ -188,6 +197,18 @@ export class AssetProfileDialog implements OnDestroy, OnInit {
} }
public initialize() { public initialize() {
this.dataService
.fetchTags()
.pipe(takeUntil(this.unsubscribeSubject))
.subscribe((tags) => {
this.HoldingTags = tags.map(({ id, name, userId }) => {
return { id, name, userId };
});
this.dataService.updateInfo();
this.changeDetectorRef.markForCheck();
});
this.historicalDataItems = undefined; this.historicalDataItems = undefined;
this.userService.stateChanged this.userService.stateChanged
@ -247,6 +268,8 @@ export class AssetProfileDialog implements OnDestroy, OnInit {
assetClass: this.assetProfile.assetClass ?? null, assetClass: this.assetProfile.assetClass ?? null,
assetSubClass: this.assetProfile.assetSubClass ?? null, assetSubClass: this.assetProfile.assetSubClass ?? null,
comment: this.assetProfile?.comment ?? '', comment: this.assetProfile?.comment ?? '',
tags: this.assetProfile?.tags ?? [],
tagsDisconnected: [],
countries: JSON.stringify( countries: JSON.stringify(
this.assetProfile?.countries?.map(({ code, weight }) => { this.assetProfile?.countries?.map(({ code, weight }) => {
return { code, weight }; return { code, weight };
@ -272,7 +295,7 @@ export class AssetProfileDialog implements OnDestroy, OnInit {
}, },
sectors: JSON.stringify(this.assetProfile?.sectors ?? []), sectors: JSON.stringify(this.assetProfile?.sectors ?? []),
symbolMapping: JSON.stringify(this.assetProfile?.symbolMapping ?? {}), symbolMapping: JSON.stringify(this.assetProfile?.symbolMapping ?? {}),
url: this.assetProfile?.url ?? '' url: this.assetProfile?.url
}); });
this.assetProfileForm.markAsPristine(); this.assetProfileForm.markAsPristine();
@ -316,6 +339,16 @@ export class AssetProfileDialog implements OnDestroy, OnInit {
.subscribe(); .subscribe();
} }
public onGatherSymbolMissingOnly({
dataSource,
symbol
}: AssetProfileIdentifier) {
this.adminService
.gatherSymbolMissingOnly({ dataSource, symbol })
.pipe(takeUntil(this.unsubscribeSubject))
.subscribe();
}
public onMarketDataChanged(withRefresh: boolean = false) { public onMarketDataChanged(withRefresh: boolean = false) {
if (withRefresh) { if (withRefresh) {
this.initialize(); this.initialize();
@ -397,10 +430,12 @@ export class AssetProfileDialog implements OnDestroy, OnInit {
assetClass: this.assetProfileForm.get('assetClass').value, assetClass: this.assetProfileForm.get('assetClass').value,
assetSubClass: this.assetProfileForm.get('assetSubClass').value, assetSubClass: this.assetProfileForm.get('assetSubClass').value,
comment: this.assetProfileForm.get('comment').value || null, comment: this.assetProfileForm.get('comment').value || null,
tags: this.assetProfileForm.get('tags').value,
tagsDisconnected: this.assetProfileForm.get('tagsDisconnected').value,
currency: this.assetProfileForm.get('currency').value, currency: this.assetProfileForm.get('currency').value,
isActive: this.assetProfileForm.get('isActive').value, isActive: this.assetProfileForm.get('isActive').value,
name: this.assetProfileForm.get('name').value, name: this.assetProfileForm.get('name').value,
url: this.assetProfileForm.get('url').value || null url: this.assetProfileForm.get('url').value
}; };
try { try {
@ -569,6 +604,30 @@ export class AssetProfileDialog implements OnDestroy, OnInit {
}); });
} }
public onRemoveTag(aTag: Tag) {
this.assetProfileForm.controls['tags'].setValue(
this.assetProfileForm.controls['tags'].value.filter(({ id }) => {
return id !== aTag.id;
})
);
this.assetProfileForm.controls['tagsDisconnected'].setValue([
...(this.assetProfileForm.controls['tagsDisconnected'].value ?? []),
aTag
]);
this.assetProfileForm.markAsDirty();
}
public onAddTag(event: MatAutocompleteSelectedEvent) {
this.assetProfileForm.controls['tags'].setValue([
...(this.assetProfileForm.controls['tags'].value ?? []),
this.HoldingTags.find(({ id }) => {
return id === event.option.value;
})
]);
this.tagInput.nativeElement.value = '';
this.assetProfileForm.markAsDirty();
}
public ngOnDestroy() { public ngOnDestroy() {
this.unsubscribeSubject.next(); this.unsubscribeSubject.next();
this.unsubscribeSubject.complete(); this.unsubscribeSubject.complete();

44
apps/client/src/app/components/admin-market-data/asset-profile-dialog/asset-profile-dialog.html

@ -26,6 +26,19 @@
> >
<ng-container i18n>Gather Historical Market Dataa</ng-container> <ng-container i18n>Gather Historical Market Dataa</ng-container>
</button> </button>
<button
mat-menu-item
type="button"
[disabled]="assetProfileForm.dirty"
(click)="
onGatherSymbolMissingOnly({
dataSource: data.dataSource,
symbol: data.symbol
})
"
>
<ng-container i18n>Gather Missing Historical Data</ng-container>
</button>
<button <button
mat-menu-item mat-menu-item
type="button" type="button"
@ -309,6 +322,37 @@
</mat-select> </mat-select>
</mat-form-field> </mat-form-field>
</div> </div>
<div class="mt-3">
<mat-form-field appearance="outline" class="w-100">
<mat-label i18n>Tags</mat-label>
<mat-chip-grid #tagsChipList>
<mat-chip-row
*ngFor="let tag of assetProfileForm.controls['tags']?.value"
matChipRemove
[removable]="true"
(removed)="onRemoveTag(tag)"
>
{{ tag.name }}
<ion-icon class="ml-2" matPrefix name="close-outline"></ion-icon>
</mat-chip-row>
<input
#tagInput
name="close-outline"
[matAutocomplete]="autocompleteTags"
[matChipInputFor]="tagsChipList"
[matChipInputSeparatorKeyCodes]="separatorKeysCodes"
/>
</mat-chip-grid>
<mat-autocomplete
#autocompleteTags="matAutocomplete"
(optionSelected)="onAddTag($event)"
>
<mat-option *ngFor="let tag of HoldingTags" [value]="tag.id">
{{ tag.name }}
</mat-option>
</mat-autocomplete>
</mat-form-field>
</div>
<div class="d-flex my-3"> <div class="d-flex my-3">
<div class="w-50"> <div class="w-50">
<mat-checkbox <mat-checkbox

4
apps/client/src/app/components/admin-market-data/asset-profile-dialog/asset-profile-dialog.module.ts

@ -11,8 +11,10 @@ import { TextFieldModule } from '@angular/cdk/text-field';
import { CommonModule } from '@angular/common'; import { CommonModule } from '@angular/common';
import { CUSTOM_ELEMENTS_SCHEMA, NgModule } from '@angular/core'; import { CUSTOM_ELEMENTS_SCHEMA, NgModule } from '@angular/core';
import { FormsModule, ReactiveFormsModule } from '@angular/forms'; import { FormsModule, ReactiveFormsModule } from '@angular/forms';
import { MatAutocompleteModule } from '@angular/material/autocomplete';
import { MatButtonModule } from '@angular/material/button'; import { MatButtonModule } from '@angular/material/button';
import { MatCheckboxModule } from '@angular/material/checkbox'; import { MatCheckboxModule } from '@angular/material/checkbox';
import { MatChipsModule } from '@angular/material/chips';
import { MatDialogModule } from '@angular/material/dialog'; import { MatDialogModule } from '@angular/material/dialog';
import { MatExpansionModule } from '@angular/material/expansion'; import { MatExpansionModule } from '@angular/material/expansion';
import { MatInputModule } from '@angular/material/input'; import { MatInputModule } from '@angular/material/input';
@ -32,6 +34,8 @@ import { AssetProfileDialog } from './asset-profile-dialog.component';
GfHistoricalMarketDataEditorComponent, GfHistoricalMarketDataEditorComponent,
GfLineChartComponent, GfLineChartComponent,
GfPortfolioProportionChartComponent, GfPortfolioProportionChartComponent,
MatAutocompleteModule,
MatChipsModule,
GfSymbolAutocompleteComponent, GfSymbolAutocompleteComponent,
GfValueComponent, GfValueComponent,
MatButtonModule, MatButtonModule,

13
apps/client/src/app/components/admin-tag/admin-tag.component.html

@ -61,6 +61,19 @@
{{ element.activityCount }} {{ element.activityCount }}
</td> </td>
</ng-container> </ng-container>
<ng-container matColumnDef="holdings">
<th
*matHeaderCellDef
class="px-1"
mat-header-cell
mat-sort-header="holdingCount"
>
<ng-container i18n>Holdings</ng-container>
</th>
<td *matCellDef="let element" class="px-1" mat-cell>
{{ element.holdingCount }}
</td>
</ng-container>
<ng-container matColumnDef="actions" stickyEnd> <ng-container matColumnDef="actions" stickyEnd>
<th <th

8
apps/client/src/app/components/admin-tag/admin-tag.component.ts

@ -36,7 +36,13 @@ export class AdminTagComponent implements OnInit, OnDestroy {
public dataSource = new MatTableDataSource<Tag>(); public dataSource = new MatTableDataSource<Tag>();
public deviceType: string; public deviceType: string;
public displayedColumns = ['name', 'userId', 'activities', 'actions']; public displayedColumns = [
'name',
'userId',
'activities',
'holdings',
'actions'
];
public tags: Tag[]; public tags: Tag[];
private unsubscribeSubject = new Subject<void>(); private unsubscribeSubject = new Subject<void>();

1
apps/client/src/app/components/benchmark-comparator/benchmark-comparator.component.scss

@ -7,5 +7,6 @@
ngx-skeleton-loader { ngx-skeleton-loader {
height: 100%; height: 100%;
} }
margin-bottom: 0.5rem;
} }
} }

10
apps/client/src/app/components/header/header.component.ts

@ -177,15 +177,19 @@ export class HeaderComponent implements OnChanges {
for (const filter of filters) { for (const filter of filters) {
if (filter.type === 'ACCOUNT') { if (filter.type === 'ACCOUNT') {
userSetting['filters.accounts'] = filter.id ? [filter.id] : null; userSetting['filters.accounts'] = filter.id?.length
? [filter.id]
: null;
} else if (filter.type === 'ASSET_CLASS') { } else if (filter.type === 'ASSET_CLASS') {
userSetting['filters.assetClasses'] = filter.id ? [filter.id] : null; userSetting['filters.assetClasses'] = filter.id?.length
? [filter.id]
: null;
} else if (filter.type === 'DATA_SOURCE') { } else if (filter.type === 'DATA_SOURCE') {
userSetting['filters.dataSource'] = filter.id ? filter.id : null; userSetting['filters.dataSource'] = filter.id ? filter.id : null;
} else if (filter.type === 'SYMBOL') { } else if (filter.type === 'SYMBOL') {
userSetting['filters.symbol'] = filter.id ? filter.id : null; userSetting['filters.symbol'] = filter.id ? filter.id : null;
} else if (filter.type === 'TAG') { } else if (filter.type === 'TAG') {
userSetting['filters.tags'] = filter.id ? [filter.id] : null; userSetting['filters.tags'] = filter.id?.length ? [filter.id] : null;
} }
} }

30
apps/client/src/app/components/holding-detail-dialog/holding-detail-dialog.component.ts

@ -94,6 +94,7 @@ export class GfHoldingDetailDialogComponent implements OnDestroy, OnInit {
public dataProviderInfo: DataProviderInfo; public dataProviderInfo: DataProviderInfo;
public dataSource: MatTableDataSource<Activity>; public dataSource: MatTableDataSource<Activity>;
public dividendInBaseCurrency: number; public dividendInBaseCurrency: number;
public stakeRewards: number;
public dividendInBaseCurrencyPrecision = 2; public dividendInBaseCurrencyPrecision = 2;
public dividendYieldPercentWithCurrencyEffect: number; public dividendYieldPercentWithCurrencyEffect: number;
public feeInBaseCurrency: number; public feeInBaseCurrency: number;
@ -115,6 +116,7 @@ export class GfHoldingDetailDialogComponent implements OnDestroy, OnInit {
public netPerformanceWithCurrencyEffectPrecision = 2; public netPerformanceWithCurrencyEffectPrecision = 2;
public quantity: number; public quantity: number;
public quantityPrecision = 2; public quantityPrecision = 2;
public stakePrecision = 2;
public reportDataGlitchMail: string; public reportDataGlitchMail: string;
public sectors: { public sectors: {
[name: string]: { name: string; value: number }; [name: string]: { name: string; value: number };
@ -228,10 +230,7 @@ export class GfHoldingDetailDialogComponent implements OnDestroy, OnInit {
averagePrice, averagePrice,
dataProviderInfo, dataProviderInfo,
dividendInBaseCurrency, dividendInBaseCurrency,
dividendYieldPercentWithCurrencyEffect, stakeRewards,
feeInBaseCurrency,
firstBuyDate,
historicalData,
investment, investment,
marketPrice, marketPrice,
maxPrice, maxPrice,
@ -244,13 +243,18 @@ export class GfHoldingDetailDialogComponent implements OnDestroy, OnInit {
SymbolProfile, SymbolProfile,
tags, tags,
transactionCount, transactionCount,
value value,
dividendYieldPercentWithCurrencyEffect,
feeInBaseCurrency,
firstBuyDate,
historicalData
}) => { }) => {
this.averagePrice = averagePrice; this.averagePrice = averagePrice;
this.benchmarkDataItems = []; this.benchmarkDataItems = [];
this.countries = {}; this.countries = {};
this.dataProviderInfo = dataProviderInfo; this.dataProviderInfo = dataProviderInfo;
this.dividendInBaseCurrency = dividendInBaseCurrency; this.dividendInBaseCurrency = dividendInBaseCurrency;
this.stakeRewards = stakeRewards;
if ( if (
this.data.deviceType === 'mobile' && this.data.deviceType === 'mobile' &&
@ -381,7 +385,7 @@ export class GfHoldingDetailDialogComponent implements OnDestroy, OnInit {
} }
} }
if (isToday(parseISO(this.firstBuyDate))) { if (this.firstBuyDate && isToday(parseISO(this.firstBuyDate))) {
// Add average price // Add average price
this.historicalDataItems.push({ this.historicalDataItems.push({
date: this.firstBuyDate, date: this.firstBuyDate,
@ -421,6 +425,7 @@ export class GfHoldingDetailDialogComponent implements OnDestroy, OnInit {
if ( if (
this.benchmarkDataItems[0]?.value === undefined && this.benchmarkDataItems[0]?.value === undefined &&
this.firstBuyDate &&
isSameMonth(parseISO(this.firstBuyDate), new Date()) isSameMonth(parseISO(this.firstBuyDate), new Date())
) { ) {
this.benchmarkDataItems[0].value = this.averagePrice; this.benchmarkDataItems[0].value = this.averagePrice;
@ -439,6 +444,19 @@ export class GfHoldingDetailDialogComponent implements OnDestroy, OnInit {
this.fetchMarketData(); this.fetchMarketData();
} }
if (Number.isInteger(this.quantity)) {
this.quantityPrecision = 0;
} else if (this.SymbolProfile?.assetSubClass === 'CRYPTOCURRENCY') {
if (this.quantity < 1) {
this.quantityPrecision = 7;
} else if (this.quantity < 1000) {
this.quantityPrecision = 5;
} else if (this.quantity > 10000000) {
this.quantityPrecision = 0;
}
this.stakePrecision = this.quantityPrecision;
}
this.changeDetectorRef.markForCheck(); this.changeDetectorRef.markForCheck();
} }
); );

54
apps/client/src/app/components/holding-detail-dialog/holding-detail-dialog.html

@ -152,30 +152,35 @@
>Investment</gf-value >Investment</gf-value
> >
</div> </div>
@if (dividendInBaseCurrency && user?.settings?.isExperimentalFeatures) { <div
<div class="col-6 mb-3"> *ngIf="dividendInBaseCurrency > 0 || !stakeRewards"
<gf-value class="col-6 mb-3"
i18n >
size="medium" <gf-value
[isCurrency]="true" i18n
[locale]="data.locale" size="medium"
[precision]="dividendInBaseCurrencyPrecision" [isCurrency]="true"
[unit]="data.baseCurrency" [locale]="data.locale"
[value]="dividendInBaseCurrency" [precision]="dividendInBaseCurrencyPrecision"
>Dividend</gf-value [unit]="data.baseCurrency"
> [value]="dividendInBaseCurrency"
</div> >Dividend</gf-value
<div class="col-6 mb-3"> >
<gf-value </div>
i18n
size="medium" <div
[isPercent]="true" *ngIf="stakeRewards > 0 && dividendInBaseCurrency == 0"
[locale]="data.locale" class="col-6 mb-3"
[value]="dividendYieldPercentWithCurrencyEffect" >
>Dividend Yield</gf-value <gf-value
> i18n
</div> size="medium"
} [locale]="data.locale"
[precision]="stakePrecision"
[value]="stakeRewards"
>Stake Rewards
</gf-value>
</div>
<div class="col-6 mb-3"> <div class="col-6 mb-3">
<gf-value <gf-value
i18n i18n
@ -210,7 +215,6 @@
} }
</gf-value> </gf-value>
</div> </div>
<div class="col-6 mb-3"></div>
<div class="col-6 mb-3"> <div class="col-6 mb-3">
<gf-value i18n size="medium" [hidden]="!assetClass" [value]="assetClass" <gf-value i18n size="medium" [hidden]="!assetClass" [value]="assetClass"
>Asset Class</gf-value >Asset Class</gf-value

3
apps/client/src/app/components/toggle/toggle.component.ts

@ -20,6 +20,9 @@ import { FormControl } from '@angular/forms';
export class ToggleComponent implements OnChanges { export class ToggleComponent implements OnChanges {
public static DEFAULT_DATE_RANGE_OPTIONS: ToggleOption[] = [ public static DEFAULT_DATE_RANGE_OPTIONS: ToggleOption[] = [
{ label: $localize`Today`, value: '1d' }, { label: $localize`Today`, value: '1d' },
{ label: $localize`1W`, value: '1w' },
{ label: $localize`1M`, value: '1m' },
{ label: $localize`3M`, value: '3m' },
{ label: $localize`YTD`, value: 'ytd' }, { label: $localize`YTD`, value: 'ytd' },
{ label: $localize`1Y`, value: '1y' }, { label: $localize`1Y`, value: '1y' },
{ label: $localize`5Y`, value: '5y' }, { label: $localize`5Y`, value: '5y' },

6
apps/client/src/app/pages/portfolio/activities/create-or-update-activity-dialog/create-or-update-activity-dialog.component.ts

@ -208,6 +208,10 @@ export class CreateOrUpdateActivityDialog implements OnDestroy {
this.activityForm.get('quantity').value * this.activityForm.get('quantity').value *
this.activityForm.get('unitPrice').value + this.activityForm.get('unitPrice').value +
(this.activityForm.get('fee').value ?? 0); (this.activityForm.get('fee').value ?? 0);
} else if (this.activityForm.get('type').value === 'STAKE') {
this.total =
this.activityForm.get('quantity').value *
(this.currentMarketPrice ?? 0);
} else { } else {
this.total = this.total =
this.activityForm.get('quantity').value * this.activityForm.get('quantity').value *
@ -261,7 +265,7 @@ export class CreateOrUpdateActivityDialog implements OnDestroy {
if (this.activityForm.get('searchSymbol').invalid) { if (this.activityForm.get('searchSymbol').invalid) {
this.data.activity.SymbolProfile = null; this.data.activity.SymbolProfile = null;
} else if ( } else if (
['BUY', 'DIVIDEND', 'SELL'].includes( ['BUY', 'DIVIDEND', 'SELL', 'STAKE'].includes(
this.activityForm.get('type').value this.activityForm.get('type').value
) )
) { ) {

13
apps/client/src/app/pages/portfolio/activities/create-or-update-activity-dialog/create-or-update-activity-dialog.html

@ -49,6 +49,14 @@
>Revenue for lending out money</small >Revenue for lending out money</small
> >
</mat-option> </mat-option>
<mat-option class="line-height-1" value="STAKE">
<span
><b>{{ typesTranslationMap['STAKE'] }}</b></span
><br />
<small class="text-muted text-nowrap" i18n
>Stake rewards, stock dividends, free/gifted stocks</small
>
</mat-option>
<mat-option value="LIABILITY"> <mat-option value="LIABILITY">
<span <span
><b>{{ typesTranslationMap['LIABILITY'] }}</b></span ><b>{{ typesTranslationMap['LIABILITY'] }}</b></span
@ -193,7 +201,10 @@
class="mb-3" class="mb-3"
[ngClass]="{ 'd-none': activityForm.get('type')?.value === 'FEE' }" [ngClass]="{ 'd-none': activityForm.get('type')?.value === 'FEE' }"
> >
<div class="align-items-start d-flex"> <div
*ngIf="activityForm.controls['type']?.value !== 'STAKE'"
class="align-items-start d-flex"
>
<mat-form-field appearance="outline" class="w-100"> <mat-form-field appearance="outline" class="w-100">
<mat-label> <mat-label>
@switch (activityForm.get('type')?.value) { @switch (activityForm.get('type')?.value) {

48
apps/client/src/app/pages/portfolio/allocations/allocations-page.component.ts

@ -7,6 +7,7 @@ import { MAX_TOP_HOLDINGS, UNKNOWN_KEY } from '@ghostfolio/common/config';
import { prettifySymbol } from '@ghostfolio/common/helper'; import { prettifySymbol } from '@ghostfolio/common/helper';
import { import {
AssetProfileIdentifier, AssetProfileIdentifier,
Holding,
HoldingWithParents, HoldingWithParents,
PortfolioDetails, PortfolioDetails,
PortfolioPosition, PortfolioPosition,
@ -91,6 +92,10 @@ export class AllocationsPageComponent implements OnDestroy, OnInit {
public topHoldingsMap: { public topHoldingsMap: {
[name: string]: { name: string; value: number }; [name: string]: { name: string; value: number };
}; };
public tagHoldings: Holding[];
public tagHoldingsMap: {
[name: string]: { name: string; value: number };
};
public totalValueInEtf = 0; public totalValueInEtf = 0;
public UNKNOWN_KEY = UNKNOWN_KEY; public UNKNOWN_KEY = UNKNOWN_KEY;
public user: User; public user: User;
@ -202,6 +207,9 @@ export class AllocationsPageComponent implements OnDestroy, OnInit {
private fetchPortfolioDetails() { private fetchPortfolioDetails() {
return this.dataService.fetchPortfolioDetails({ return this.dataService.fetchPortfolioDetails({
filters: this.userService.getFilters(), filters: this.userService.getFilters(),
parameters: {
isAllocation: true
},
withMarkets: true withMarkets: true
}); });
} }
@ -280,6 +288,7 @@ export class AllocationsPageComponent implements OnDestroy, OnInit {
} }
}; };
this.topHoldingsMap = {}; this.topHoldingsMap = {};
this.tagHoldingsMap = {};
} }
private initializeAllocationsData() { private initializeAllocationsData() {
@ -338,6 +347,22 @@ export class AllocationsPageComponent implements OnDestroy, OnInit {
name: position.name name: position.name
}; };
if (position.tags.length > 0) {
for (const tag of position.tags) {
const { name } = tag;
if (this.tagHoldingsMap[name]?.value) {
this.tagHoldingsMap[name].value +=
position.valueInBaseCurrency ?? 0;
} else {
this.tagHoldingsMap[name] = {
name,
value: position.valueInBaseCurrency ?? 0
};
}
}
}
if (position.assetClass !== AssetClass.LIQUIDITY) { if (position.assetClass !== AssetClass.LIQUIDITY) {
// Prepare analysis data by continents, countries, holdings and sectors except for liquidity // Prepare analysis data by continents, countries, holdings and sectors except for liquidity
@ -478,6 +503,29 @@ export class AllocationsPageComponent implements OnDestroy, OnInit {
}; };
} }
this.tagHoldings = Object.values(this.tagHoldingsMap)
.map(({ name, value }) => {
if (this.hasImpersonationId || this.user.settings.isRestrictedView) {
return {
name,
allocationInPercentage: value,
valueInBaseCurrency: null
};
}
return {
name,
allocationInPercentage:
this.portfolioDetails.summary.currentValueInBaseCurrency > 0
? value / this.portfolioDetails.summary.currentValueInBaseCurrency
: 0,
valueInBaseCurrency: value
};
})
.sort((a, b) => {
return b.allocationInPercentage - a.allocationInPercentage;
});
this.topHoldings = Object.values(this.topHoldingsMap) this.topHoldings = Object.values(this.topHoldingsMap)
.map(({ name, value }) => { .map(({ name, value }) => {
if (this.hasImpersonationId || this.user.settings.isRestrictedView) { if (this.hasImpersonationId || this.user.settings.isRestrictedView) {

18
apps/client/src/app/pages/portfolio/allocations/allocations-page.html

@ -327,6 +327,24 @@
'd-none': !user?.settings?.isExperimentalFeatures 'd-none': !user?.settings?.isExperimentalFeatures
}" }"
> >
<mat-card appearance="outlined" class="mb-3">
<mat-card-header class="overflow-hidden w-100">
<mat-card-title class="align-items-center d-flex text-truncate">
<span i18n>By Tag Holding</span>
@if (user?.subscription?.type === 'Basic') {
<gf-premium-indicator class="ml-1" />
}
</mat-card-title>
</mat-card-header>
<mat-card-content>
<gf-top-holdings
[baseCurrency]="user?.settings?.baseCurrency"
[locale]="user?.settings?.locale"
[pageSize]="10"
[topHoldings]="tagHoldings"
/>
</mat-card-content>
</mat-card>
<mat-card appearance="outlined" class="mb-3"> <mat-card appearance="outlined" class="mb-3">
<mat-card-header class="overflow-hidden w-100"> <mat-card-header class="overflow-hidden w-100">
<mat-card-title class="align-items-center d-flex text-truncate"> <mat-card-title class="align-items-center d-flex text-truncate">

34
apps/client/src/app/pages/portfolio/analysis/analysis-page.component.ts

@ -12,7 +12,11 @@ import {
User User
} from '@ghostfolio/common/interfaces'; } from '@ghostfolio/common/interfaces';
import { hasPermission, permissions } from '@ghostfolio/common/permissions'; import { hasPermission, permissions } from '@ghostfolio/common/permissions';
import type { AiPromptMode, GroupBy } from '@ghostfolio/common/types'; import type {
AiPromptMode,
DateRange,
GroupBy
} from '@ghostfolio/common/types';
import { translate } from '@ghostfolio/ui/i18n'; import { translate } from '@ghostfolio/ui/i18n';
import { Clipboard } from '@angular/cdk/clipboard'; import { Clipboard } from '@angular/cdk/clipboard';
@ -46,6 +50,13 @@ export class AnalysisPageComponent implements OnDestroy, OnInit {
public benchmarks: Partial<SymbolProfile>[]; public benchmarks: Partial<SymbolProfile>[];
public bottom3: PortfolioPosition[]; public bottom3: PortfolioPosition[];
public dateRangeOptions = ToggleComponent.DEFAULT_DATE_RANGE_OPTIONS; public dateRangeOptions = ToggleComponent.DEFAULT_DATE_RANGE_OPTIONS;
public timeWeightedPerformanceOptions = [
{ label: $localize`No`, value: 'N' },
{ label: $localize`Both`, value: 'B' },
{ label: $localize`Only`, value: 'O' }
];
public selectedTimeWeightedPerformanceOption: string;
public daysInMarket: number;
public deviceType: string; public deviceType: string;
public dividendsByGroup: InvestmentItem[]; public dividendsByGroup: InvestmentItem[];
public dividendTimelineDataLabel = $localize`Dividend`; public dividendTimelineDataLabel = $localize`Dividend`;
@ -69,6 +80,8 @@ export class AnalysisPageComponent implements OnDestroy, OnInit {
public performance: PortfolioPerformance; public performance: PortfolioPerformance;
public performanceDataItems: HistoricalDataItem[]; public performanceDataItems: HistoricalDataItem[];
public performanceDataItemsInPercentage: HistoricalDataItem[]; public performanceDataItemsInPercentage: HistoricalDataItem[];
public performanceDataItemsTimeWeightedInPercentage: HistoricalDataItem[] =
[];
public portfolioEvolutionDataLabel = $localize`Investment`; public portfolioEvolutionDataLabel = $localize`Investment`;
public streaks: PortfolioInvestments['streaks']; public streaks: PortfolioInvestments['streaks'];
public top3: PortfolioPosition[]; public top3: PortfolioPosition[];
@ -148,6 +161,24 @@ export class AnalysisPageComponent implements OnDestroy, OnInit {
}); });
} }
public onChangeDateRange(dateRange: DateRange) {
this.dataService
.putUserSetting({ dateRange })
.pipe(takeUntil(this.unsubscribeSubject))
.subscribe(() => {
this.userService.remove();
this.userService
.get()
.pipe(takeUntil(this.unsubscribeSubject))
.subscribe((user) => {
this.user = user;
this.changeDetectorRef.markForCheck();
});
});
}
public onChangeGroupBy(aMode: GroupBy) { public onChangeGroupBy(aMode: GroupBy) {
this.mode = aMode; this.mode = aMode;
this.fetchDividendsAndInvestments(); this.fetchDividendsAndInvestments();
@ -267,6 +298,7 @@ export class AnalysisPageComponent implements OnDestroy, OnInit {
this.performance = performance; this.performance = performance;
this.performanceDataItems = []; this.performanceDataItems = [];
this.performanceDataItemsInPercentage = []; this.performanceDataItemsInPercentage = [];
this.performanceDataItemsTimeWeightedInPercentage = [];
for (const [ for (const [
index, index,

2
apps/client/src/app/pages/portfolio/analysis/analysis-page.html

@ -87,7 +87,7 @@
[performanceDataItems]="performanceDataItemsInPercentage" [performanceDataItems]="performanceDataItemsInPercentage"
[user]="user" [user]="user"
(benchmarkChanged)="onChangeBenchmark($event)" (benchmarkChanged)="onChangeBenchmark($event)"
/> ></gf-benchmark-comparator>
</div> </div>
</div> </div>

19
apps/client/src/app/services/admin.service.ts

@ -170,6 +170,10 @@ export class AdminService {
return this.http.post<void>('/api/v1/admin/gather/max', {}); return this.http.post<void>('/api/v1/admin/gather/max', {});
} }
public gatherMissingOnly() {
return this.http.post<void>('/api/v1/admin/gather/missing', {});
}
public gatherProfileData() { public gatherProfileData() {
return this.http.post<void>('/api/v1/admin/gather/profile-data', {}); return this.http.post<void>('/api/v1/admin/gather/profile-data', {});
} }
@ -189,6 +193,17 @@ export class AdminService {
return this.http.post<MarketData | void>(url, {}); return this.http.post<MarketData | void>(url, {});
} }
public gatherSymbolMissingOnly({
dataSource,
symbol
}: AssetProfileIdentifier & {
date?: Date;
}) {
const url = `/api/v1/admin/gatherMissing/${dataSource}/${symbol}`;
return this.http.post<MarketData | void>(url, {});
}
public fetchSymbolForDate({ public fetchSymbolForDate({
dataSource, dataSource,
dateString, dateString,
@ -218,6 +233,8 @@ export class AdminService {
sectors, sectors,
symbol: newSymbol, symbol: newSymbol,
symbolMapping, symbolMapping,
tags,
tagsDisconnected,
url url
}: UpdateAssetProfileDto }: UpdateAssetProfileDto
) { ) {
@ -236,6 +253,8 @@ export class AdminService {
sectors, sectors,
symbol: newSymbol, symbol: newSymbol,
symbolMapping, symbolMapping,
tags,
tagsDisconnected,
url url
} }
); );

11
apps/client/src/app/services/data.service.ts

@ -513,15 +513,18 @@ export class DataService {
public fetchPortfolioDetails({ public fetchPortfolioDetails({
filters, filters,
parameters,
withMarkets = false withMarkets = false
}: { }: {
filters?: Filter[]; filters?: Filter[];
parameters?: any;
withMarkets?: boolean; withMarkets?: boolean;
} = {}): Observable<PortfolioDetails> { } = {}): Observable<PortfolioDetails> {
let params = this.buildFiltersAsQueryParams({ filters }); let params = this.buildFiltersAsQueryParams({ filters });
if (withMarkets) { if (withMarkets) {
params = params.append('withMarkets', withMarkets); params = params.append('withMarkets', withMarkets);
params = parameters ? params.appendAll(parameters) : params;
} }
return this.http return this.http
@ -611,11 +614,13 @@ export class DataService {
filters, filters,
range, range,
withExcludedAccounts = false, withExcludedAccounts = false,
timeWeightedPerformance = false,
withItems = false withItems = false
}: { }: {
filters?: Filter[]; filters?: Filter[];
range: DateRange; range: DateRange;
withExcludedAccounts?: boolean; withExcludedAccounts?: boolean;
timeWeightedPerformance?: boolean;
withItems?: boolean; withItems?: boolean;
}): Observable<PortfolioPerformanceResponse> { }): Observable<PortfolioPerformanceResponse> {
let params = this.buildFiltersAsQueryParams({ filters }); let params = this.buildFiltersAsQueryParams({ filters });
@ -624,6 +629,12 @@ export class DataService {
if (withExcludedAccounts) { if (withExcludedAccounts) {
params = params.append('withExcludedAccounts', withExcludedAccounts); params = params.append('withExcludedAccounts', withExcludedAccounts);
} }
if (timeWeightedPerformance) {
params = params.append(
'timeWeightedPerformance',
timeWeightedPerformance
);
}
if (withItems) { if (withItems) {
params = params.append('withItems', withItems); params = params.append('withItems', withItems);

2
apps/client/src/app/services/import-activities.service.ts

@ -348,6 +348,8 @@ export class ImportActivitiesService {
return 'LIABILITY'; return 'LIABILITY';
case 'sell': case 'sell':
return 'SELL'; return 'SELL';
case 'stake':
return 'STAKE';
default: default:
break; break;
} }

6
apps/client/src/app/services/user/user.service.ts

@ -53,14 +53,14 @@ export class UserService extends ObservableStore<UserStoreState> {
if (user?.settings['filters.accounts']) { if (user?.settings['filters.accounts']) {
filters.push({ filters.push({
id: user.settings['filters.accounts'][0], id: user.settings['filters.accounts'].join(','),
type: 'ACCOUNT' type: 'ACCOUNT'
}); });
} }
if (user?.settings['filters.assetClasses']) { if (user?.settings['filters.assetClasses']) {
filters.push({ filters.push({
id: user.settings['filters.assetClasses'][0], id: user.settings['filters.assetClasses'].join(','),
type: 'ASSET_CLASS' type: 'ASSET_CLASS'
}); });
} }
@ -81,7 +81,7 @@ export class UserService extends ObservableStore<UserStoreState> {
if (user?.settings['filters.tags']) { if (user?.settings['filters.tags']) {
filters.push({ filters.push({
id: user.settings['filters.tags'][0], id: user.settings['filters.tags'].join(','),
type: 'TAG' type: 'TAG'
}); });
} }

6
apps/ui-e2e/project.json

@ -3,6 +3,8 @@
"$schema": "../../node_modules/nx/schemas/project-schema.json", "$schema": "../../node_modules/nx/schemas/project-schema.json",
"sourceRoot": "apps/ui-e2e/src", "sourceRoot": "apps/ui-e2e/src",
"projectType": "application", "projectType": "application",
"tags": [],
"implicitDependencies": ["ui"],
"targets": { "targets": {
"e2e": { "e2e": {
"executor": "@nx/cypress:cypress", "executor": "@nx/cypress:cypress",
@ -23,7 +25,5 @@
"lintFilePatterns": ["apps/ui-e2e/**/*.{js,ts}"] "lintFilePatterns": ["apps/ui-e2e/**/*.{js,ts}"]
} }
} }
}, }
"tags": [],
"implicitDependencies": ["ui"]
} }

10
libs/common/src/lib/calculation-helper.ts

@ -7,6 +7,7 @@ import {
startOfWeek, startOfWeek,
startOfYear, startOfYear,
subDays, subDays,
subMonths,
subYears subYears
} from 'date-fns'; } from 'date-fns';
import { isNumber } from 'lodash'; import { isNumber } from 'lodash';
@ -55,12 +56,21 @@ export function getIntervalFromDateRange(
subDays(startOfWeek(resetHours(new Date()), { weekStartsOn: 1 }), 1) subDays(startOfWeek(resetHours(new Date()), { weekStartsOn: 1 }), 1)
]); ]);
break; break;
case '1w':
startDate = max([startDate, subDays(resetHours(new Date()), 7)]);
break;
case 'ytd': case 'ytd':
startDate = max([ startDate = max([
startDate, startDate,
subDays(startOfYear(resetHours(new Date())), 1) subDays(startOfYear(resetHours(new Date())), 1)
]); ]);
break; break;
case '1m':
startDate = max([startDate, subMonths(resetHours(new Date()), 1)]);
break;
case '3m':
startDate = max([startDate, subMonths(resetHours(new Date()), 3)]);
break;
case '1y': case '1y':
startDate = max([startDate, subYears(resetHours(new Date()), 1)]); startDate = max([startDate, subYears(resetHours(new Date()), 1)]);
break; break;

46
libs/common/src/lib/chunkhelper.ts

@ -0,0 +1,46 @@
import { Prisma, PrismaClient } from '@prisma/client';
class Chunk<T> implements Iterable<T[] | undefined> {
protected constructor(
private readonly values: readonly T[],
private readonly size: number
) {}
*[Symbol.iterator]() {
const copy = [...this.values];
if (copy.length === 0) yield undefined;
while (copy.length) yield copy.splice(0, this.size);
}
map<U>(mapper: (items?: T[]) => U): U[] {
return Array.from(this).map((items) => mapper(items));
}
static of<U>(values: readonly U[]) {
return {
by: (size: number) => new Chunk(values, size)
};
}
}
export type Queryable<T, Result> = (
p: PrismaClient,
vs?: T[]
) => Prisma.PrismaPromise<Result>;
export class BatchPrismaClient {
constructor(
private readonly prisma: PrismaClient,
private readonly size = 32_000
) {}
over<T>(values: readonly T[]) {
return {
with: <Result>(queryable: Queryable<T, Result>) =>
this.prisma.$transaction(
Chunk.of(values)
.by(this.size)
.map((vs) => queryable(this.prisma, vs))
)
};
}
}

12
libs/common/src/lib/config.ts

@ -99,6 +99,18 @@ export const GATHER_HISTORICAL_MARKET_DATA_PROCESS_JOB_OPTIONS: JobOptions = {
removeOnComplete: true removeOnComplete: true
}; };
export const GATHER_MISSING_HISTORICAL_MARKET_DATA_PROCESS_JOB_NAME =
'GATHER_MISSING_HISTORICAL_MARKET_DATA';
export const GATHER_MISSING_HISTORICAL_MARKET_DATA_PROCESS_JOB_OPTIONS: JobOptions =
{
attempts: 12,
backoff: {
delay: ms('1 minute'),
type: 'exponential'
},
removeOnComplete: true
};
export const PORTFOLIO_SNAPSHOT_PROCESS_JOB_NAME = 'PORTFOLIO'; export const PORTFOLIO_SNAPSHOT_PROCESS_JOB_NAME = 'PORTFOLIO';
export const PORTFOLIO_SNAPSHOT_PROCESS_JOB_OPTIONS: JobOptions = { export const PORTFOLIO_SNAPSHOT_PROCESS_JOB_OPTIONS: JobOptions = {
removeOnComplete: true removeOnComplete: true

3
libs/common/src/lib/interfaces/admin-market-data.interface.ts

@ -1,4 +1,4 @@
import { AssetClass, AssetSubClass, DataSource } from '@prisma/client'; import { AssetClass, AssetSubClass, DataSource, Tag } from '@prisma/client';
export interface AdminMarketData { export interface AdminMarketData {
count: number; count: number;
@ -21,4 +21,5 @@ export interface AdminMarketDataItem {
name: string; name: string;
sectorsCount: number; sectorsCount: number;
symbol: string; symbol: string;
tags: Tag[];
} }

3
libs/common/src/lib/interfaces/enhanced-symbol-profile.interface.ts

@ -1,4 +1,4 @@
import { AssetClass, AssetSubClass, DataSource } from '@prisma/client'; import { AssetClass, AssetSubClass, DataSource, Tag } from '@prisma/client';
import { Country } from './country.interface'; import { Country } from './country.interface';
import { DataProviderInfo } from './data-provider-info.interface'; import { DataProviderInfo } from './data-provider-info.interface';
@ -31,5 +31,6 @@ export interface EnhancedSymbolProfile {
symbolMapping?: { [key: string]: string }; symbolMapping?: { [key: string]: string };
updatedAt: Date; updatedAt: Date;
url?: string; url?: string;
tags?: Tag[];
userId?: string; userId?: string;
} }

2
libs/common/src/lib/interfaces/historical-data-item.interface.ts

@ -16,5 +16,7 @@ export interface HistoricalDataItem {
totalInvestmentValueWithCurrencyEffect?: number; totalInvestmentValueWithCurrencyEffect?: number;
value?: number; value?: number;
valueInPercentage?: number; valueInPercentage?: number;
timeWeightedPerformance?: number;
timeWeightedPerformanceWithCurrencyEffect?: number;
valueWithCurrencyEffect?: number; valueWithCurrencyEffect?: number;
} }

3
libs/common/src/lib/interfaces/symbol-metrics.interface.ts

@ -49,6 +49,9 @@ export interface SymbolMetrics {
totalInterestInBaseCurrency: Big; totalInterestInBaseCurrency: Big;
totalInvestment: Big; totalInvestment: Big;
totalInvestmentWithCurrencyEffect: Big; totalInvestmentWithCurrencyEffect: Big;
unitPrices: {
[date: string]: Big;
};
totalLiabilities: Big; totalLiabilities: Big;
totalLiabilitiesInBaseCurrency: Big; totalLiabilitiesInBaseCurrency: Big;
totalValuables: Big; totalValuables: Big;

9
libs/common/src/lib/types/date-range.type.ts

@ -1,9 +1,12 @@
export type DateRange = export type DateRange =
| '1d' | '1d'
| 'wtd'
| '1w'
| 'mtd'
| '1m'
| '3m'
| 'ytd'
| '1y' | '1y'
| '5y' | '5y'
| 'max' | 'max'
| 'mtd'
| 'wtd'
| 'ytd'
| string; // '2024', '2023', '2022', etc. | string; // '2024', '2023', '2022', etc.

51
libs/ui/src/lib/activities-table/activities-table.component.scss

@ -1,3 +1,54 @@
:host { :host {
display: block; display: block;
.activities {
overflow-x: auto;
.mat-mdc-table {
th {
::ng-deep {
.mat-sort-header-container {
justify-content: inherit;
}
}
}
.mat-mdc-row {
.type-badge {
background-color: rgba(var(--palette-foreground-text), 0.05);
border-radius: 1rem;
line-height: 1em;
ion-icon {
font-size: 1rem;
}
&.buy {
color: var(--green);
}
&.dividend {
color: var(--blue);
}
&.stake {
color: var(--blue);
}
&.item {
color: var(--purple);
}
&.liability {
color: var(--red);
}
&.sell {
color: var(--orange);
}
}
}
}
}
}
:host-context(.is-dark-theme) {
.mat-mdc-table {
.type-badge {
background-color: rgba(
var(--palette-foreground-text-dark),
0.1
) !important;
}
}
} }

38
libs/ui/src/lib/activity-type/activity-type.component.html

@ -7,21 +7,31 @@
interest: activityType === 'INTEREST', interest: activityType === 'INTEREST',
item: activityType === 'ITEM', item: activityType === 'ITEM',
liability: activityType === 'LIABILITY', liability: activityType === 'LIABILITY',
sell: activityType === 'SELL' sell: activityType === 'SELL',
stake: activityType === 'STAKE'
}" }"
> >
@if (activityType === 'BUY') { <ion-icon
<ion-icon name="arrow-up-circle-outline" /> *ngIf="activityType === 'BUY'"
} @else if (activityType === 'DIVIDEND' || activityType === 'INTEREST') { name="arrow-up-circle-outline"
<ion-icon name="add-circle-outline" /> ></ion-icon>
} @else if (activityType === 'FEE') { <ion-icon
<ion-icon name="hammer-outline" /> *ngIf="
} @else if (activityType === 'ITEM') { activityType === 'DIVIDEND' ||
<ion-icon name="cube-outline" /> activityType === 'INTEREST' ||
} @else if (activityType === 'LIABILITY') { activityType === 'STAKE'
<ion-icon name="flame-outline" /> "
} @else if (activityType === 'SELL') { name="add-circle-outline"
<ion-icon name="arrow-down-circle-outline" /> ></ion-icon>
} <ion-icon *ngIf="activityType === 'FEE'" name="hammer-outline"></ion-icon>
<ion-icon *ngIf="activityType === 'ITEM'" name="cube-outline"></ion-icon>
<ion-icon
*ngIf="activityType === 'LIABILITY'"
name="flame-outline"
></ion-icon>
<ion-icon
*ngIf="activityType === 'SELL'"
name="arrow-down-circle-outline"
></ion-icon>
<span class="d-none d-lg-block mx-1">{{ activityTypeLabel }}</span> <span class="d-none d-lg-block mx-1">{{ activityTypeLabel }}</span>
</div> </div>

15
libs/ui/src/lib/assistant/assistant.component.ts

@ -222,10 +222,23 @@ export class GfAssistantComponent implements OnChanges, OnDestroy, OnInit {
label: $localize`Week to date` + ' (' + $localize`WTD` + ')', label: $localize`Week to date` + ' (' + $localize`WTD` + ')',
value: 'wtd' value: 'wtd'
}, },
{
label: '1 ' + $localize`Week` + ' (' + $localize`1W` + ')',
value: '1w'
},
{ {
label: $localize`Month to date` + ' (' + $localize`MTD` + ')', label: $localize`Month to date` + ' (' + $localize`MTD` + ')',
value: 'mtd' value: 'mtd'
}, },
{
label: '1 ' + $localize`Month` + ' (' + $localize`1M` + ')',
value: '1m'
},
{
label: '3 ' + $localize`Month` + ' (' + $localize`3M` + ')',
value: '3m'
},
{ {
label: $localize`Year to date` + ' (' + $localize`YTD` + ')', label: $localize`Year to date` + ' (' + $localize`YTD` + ')',
value: 'ytd' value: 'ytd'
@ -287,7 +300,7 @@ export class GfAssistantComponent implements OnChanges, OnDestroy, OnInit {
} }
} }
public hasFilter(aFormValue: { [key: string]: string }) { public hasFilter(aFormValue: { [key: string]: string[] }) {
return Object.values(aFormValue).some((value) => { return Object.values(aFormValue).some((value) => {
return !!value; return !!value;
}); });

12
libs/ui/src/lib/assistant/assistant.html

@ -104,8 +104,8 @@
<div class="p-3"> <div class="p-3">
<div class="mb-3"> <div class="mb-3">
<mat-form-field appearance="outline" class="w-100 without-hint"> <mat-form-field appearance="outline" class="w-100 without-hint">
<mat-label i18n>Account</mat-label> <mat-label i18n>Accounts</mat-label>
<mat-select formControlName="account"> <mat-select formControlName="account" multiple>
<mat-option [value]="null" /> <mat-option [value]="null" />
@for (account of accounts; track account.id) { @for (account of accounts; track account.id) {
<mat-option [value]="account.id"> <mat-option [value]="account.id">
@ -152,8 +152,8 @@
</div> </div>
<div class="mb-3"> <div class="mb-3">
<mat-form-field appearance="outline" class="w-100 without-hint"> <mat-form-field appearance="outline" class="w-100 without-hint">
<mat-label i18n>Tag</mat-label> <mat-label i18n>Tags</mat-label>
<mat-select formControlName="tag"> <mat-select formControlName="tag" multiple>
<mat-option [value]="null" /> <mat-option [value]="null" />
@for (tag of tags; track tag.id) { @for (tag of tags; track tag.id) {
<mat-option [value]="tag.id">{{ tag.label }}</mat-option> <mat-option [value]="tag.id">{{ tag.label }}</mat-option>
@ -163,8 +163,8 @@
</div> </div>
<div class="mb-3"> <div class="mb-3">
<mat-form-field appearance="outline" class="w-100 without-hint"> <mat-form-field appearance="outline" class="w-100 without-hint">
<mat-label i18n>Asset Class</mat-label> <mat-label i18n>Asset Classes</mat-label>
<mat-select formControlName="assetClass"> <mat-select formControlName="assetClass" multiple>
<mat-option [value]="null" /> <mat-option [value]="null" />
@for (assetClass of assetClasses; track assetClass.id) { @for (assetClass of assetClasses; track assetClass.id) {
<mat-option [value]="assetClass.id">{{ <mat-option [value]="assetClass.id">{{

89
libs/ui/src/lib/holdings-table/holdings-table.component.html

@ -16,18 +16,23 @@
[tooltip]="element.name" [tooltip]="element.name"
/> />
</td> </td>
<td *matFooterCellDef class="px-1" mat-footer-cell></td>
</ng-container> </ng-container>
<ng-container matColumnDef="nameWithSymbol"> <ng-container matColumnDef="nameWithSymbol">
<th <th
*matHeaderCellDef *matHeaderCellDef
class="px-1" class="px-1 name-col"
mat-header-cell mat-header-cell
mat-sort-header="symbol" mat-sort-header="symbol"
> >
<ng-container i18n>Name</ng-container> <ng-container i18n>Name</ng-container>
</th> </th>
<td *matCellDef="let element" class="line-height-1 px-1" mat-cell> <td
*matCellDef="let element"
class="line-height-1 px-1 name-col"
mat-cell
>
<div class="text-truncate"> <div class="text-truncate">
{{ element.name }} {{ element.name }}
@if (element.name === element.symbol) { @if (element.name === element.symbol) {
@ -38,6 +43,7 @@
<small class="text-muted">{{ element.symbol }}</small> <small class="text-muted">{{ element.symbol }}</small>
</div> </div>
</td> </td>
<td *matFooterCellDef class="px-1" mat-footer-cell>Total</td>
</ng-container> </ng-container>
<ng-container matColumnDef="dateOfFirstActivity"> <ng-container matColumnDef="dateOfFirstActivity">
@ -62,9 +68,40 @@
/> />
</div> </div>
</td> </td>
<td
*matFooterCellDef
class="d-none d-lg-table-cell justify-content-end px-1"
mat-footer-cell
></td>
</ng-container>
<ng-container
matColumnDef="marketPrice"
[stickyEnd]="dataSource?.sort?.active === 'marketPrice'"
>
<th
*matHeaderCellDef
class="justify-content-end px-1"
mat-header-cell
mat-sort-header
>
<ng-container i18n>Price</ng-container>
</th>
<td *matCellDef="let element" class="px-1" mat-cell>
<div class="d-flex justify-content-end">
<gf-value
[isCurrency]="true"
[locale]="locale"
[value]="isLoading ? undefined : element.marketPrice"
/>
</div>
</td>
<td *matFooterCellDef class="px-1" mat-footer-cell></td>
</ng-container> </ng-container>
<ng-container matColumnDef="valueInBaseCurrency"> <ng-container
matColumnDef="valueInBaseCurrency"
[stickyEnd]="dataSource?.sort?.active === 'valueInBaseCurrency'"
>
<th <th
*matHeaderCellDef *matHeaderCellDef
class="d-none d-lg-table-cell justify-content-end px-1" class="d-none d-lg-table-cell justify-content-end px-1"
@ -86,9 +123,20 @@
/> />
</div> </div>
</td> </td>
<td *matFooterCellDef class="d-none d-lg-table-cell px-1" mat-footer-cell>
<gf-value
[isCurrency]="true"
[locale]="locale"
[value]="totalValue"
></gf-value>
</td>
</ng-container> </ng-container>
<ng-container matColumnDef="allocationInPercentage"> <ng-container
matColumnDef="allocationInPercentage"
[stickyEnd]="dataSource?.sort?.active === 'allocationInPercentage'"
>
<th <th
*matHeaderCellDef *matHeaderCellDef
class="justify-content-end px-1" class="justify-content-end px-1"
@ -107,9 +155,15 @@
/> />
</div> </div>
</td> </td>
<td *matFooterCellDef class="px-1" mat-footer-cell></td>
</ng-container> </ng-container>
<ng-container matColumnDef="performance"> <ng-container
matColumnDef="performance"
[stickyEnd]="
dataSource?.sort?.active === 'netPerformanceWithCurrencyEffect'
"
>
<th <th
*matHeaderCellDef *matHeaderCellDef
class="justify-content-end px-1" class="justify-content-end px-1"
@ -130,6 +184,16 @@
/> />
</div> </div>
</td> </td>
<td *matFooterCellDef class="px-1" mat-footer-cell>
<div class="d-flex justify-content-end">
<gf-value
[colorizeSign]="true"
[isCurrency]="true"
[locale]="locale"
[value]="totalChange"
></gf-value>
</div>
</td>
</ng-container> </ng-container>
<ng-container matColumnDef="performanceInPercentage" stickyEnd> <ng-container matColumnDef="performanceInPercentage" stickyEnd>
@ -156,6 +220,16 @@
/> />
</div> </div>
</td> </td>
<td *matFooterCellDef class="px-1" mat-footer-cell>
<div class="d-flex justify-content-end">
<gf-value
[colorizeSign]="true"
[isPercent]="true"
[locale]="locale"
[value]="totalChangePercentage"
></gf-value>
</div>
</td>
</ng-container> </ng-container>
<tr *matHeaderRowDef="displayedColumns" mat-header-row></tr> <tr *matHeaderRowDef="displayedColumns" mat-header-row></tr>
@ -175,6 +249,11 @@
}) })
" "
></tr> ></tr>
<tr
*matFooterRowDef="displayedColumns"
mat-footer-row
[ngClass]="{ hidden: isLoading }"
></tr>
</table> </table>
</div> </div>

20
libs/ui/src/lib/holdings-table/holdings-table.component.scss

@ -1,6 +1,22 @@
:host { :host {
display: block; display: block;
.mat-column-nameWithSymbol {
word-wrap: break-word !important;
white-space: unset !important;
max-width: 250px;
width: 60%;
overflow-wrap: break-word;
word-wrap: break-word;
word-break: break-word;
-ms-hyphens: auto;
-moz-hyphens: auto;
-webkit-hyphens: auto;
hyphens: auto;
}
.gf-table { .gf-table {
th { th {
::ng-deep { ::ng-deep {
@ -11,3 +27,7 @@
} }
} }
} }
.mat-sort-header-sorted {
position: sticky !important;
}

15
libs/ui/src/lib/holdings-table/holdings-table.component.ts

@ -67,6 +67,10 @@ export class GfHoldingsTableComponent implements OnChanges, OnDestroy {
public isLoading = true; public isLoading = true;
public routeQueryParams: Subscription; public routeQueryParams: Subscription;
protected totalValue = 0;
protected totalChange = 0;
protected totalChangePercentage = 0;
private unsubscribeSubject = new Subject<void>(); private unsubscribeSubject = new Subject<void>();
public ngOnChanges() { public ngOnChanges() {
@ -77,6 +81,7 @@ export class GfHoldingsTableComponent implements OnChanges, OnDestroy {
} }
this.displayedColumns.push('allocationInPercentage'); this.displayedColumns.push('allocationInPercentage');
this.displayedColumns.push('marketPrice');
if (this.hasPermissionToShowValues) { if (this.hasPermissionToShowValues) {
this.displayedColumns.push('performance'); this.displayedColumns.push('performance');
@ -89,6 +94,16 @@ export class GfHoldingsTableComponent implements OnChanges, OnDestroy {
this.dataSource = new MatTableDataSource(this.holdings); this.dataSource = new MatTableDataSource(this.holdings);
this.dataSource.paginator = this.paginator; this.dataSource.paginator = this.paginator;
this.dataSource.sort = this.sort; this.dataSource.sort = this.sort;
this.totalValue = this.dataSource.data.reduce(
(sum, current) => (sum += current.valueInBaseCurrency),
0
);
this.totalChange = this.dataSource.data.reduce(
(sum, current) => (sum += current.netPerformanceWithCurrencyEffect),
0
);
this.totalChangePercentage =
this.totalChange / (this.totalValue - this.totalChange);
if (this.holdings) { if (this.holdings) {
this.isLoading = false; this.isLoading = false;

1
libs/ui/src/lib/i18n.ts

@ -39,6 +39,7 @@ const locales = {
ITEM: $localize`Valuable`, ITEM: $localize`Valuable`,
LIABILITY: $localize`Liability`, LIABILITY: $localize`Liability`,
SELL: $localize`Sell`, SELL: $localize`Sell`,
STAKE: $localize`Stake`,
// AssetClass (enum) // AssetClass (enum)
CASH: $localize`Cash`, CASH: $localize`Cash`,

185
migrations.json

@ -0,0 +1,185 @@
{
"migrations": [
{
"cli": "nx",
"version": "17.3.0-beta.6",
"description": "Updates the nx wrapper.",
"implementation": "./src/migrations/update-17-3-0/update-nxw",
"package": "nx",
"name": "17.3.0-update-nx-wrapper"
},
{
"cli": "nx",
"version": "18.0.0-beta.2",
"description": "Updates nx.json to disabled adding plugins when generating projects in an existing Nx workspace",
"implementation": "./src/migrations/update-18-0-0/disable-crystal-for-existing-workspaces",
"x-repair-skip": true,
"package": "nx",
"name": "18.0.0-disable-adding-plugins-for-existing-workspaces"
},
{
"version": "18.1.0-beta.3",
"description": "Moves affected.defaultBase to defaultBase in `nx.json`",
"implementation": "./src/migrations/update-17-2-0/move-default-base",
"package": "nx",
"name": "move-default-base-to-nx-json-root"
},
{
"cli": "nx",
"version": "18.1.0-beta.3",
"description": "Update to Cypress ^13.6.6 if the workspace is using Cypress v13 to ensure workspaces don't use v13.6.5 which has an issue when verifying Cypress.",
"implementation": "./src/migrations/update-18-1-0/update-cypress-version-13-6-6",
"package": "@nx/cypress",
"name": "update-cypress-version-13-6-6"
},
{
"cli": "nx",
"version": "17.2.6-beta.1",
"description": "Rename workspace rules from @nx/workspace/name to @nx/workspace-name",
"implementation": "./src/migrations/update-17-2-6-rename-workspace-rules/rename-workspace-rules",
"package": "@nx/eslint-plugin",
"name": "update-17-2-6-rename-workspace-rules"
},
{
"version": "17.1.0-beta.2",
"description": "Move jest executor options to nx.json targetDefaults",
"implementation": "./src/migrations/update-17-1-0/move-options-to-target-defaults",
"package": "@nx/jest",
"name": "move-options-to-target-defaults"
},
{
"cli": "nx",
"version": "17.1.0-beta.5",
"requires": {
"@angular/core": ">=17.0.0"
},
"description": "Update the @angular/cli package version to ~17.0.0.",
"factory": "./src/migrations/update-17-1-0/update-angular-cli",
"package": "@nx/angular",
"name": "update-angular-cli-version-17-0-0"
},
{
"cli": "nx",
"version": "17.1.0-beta.5",
"requires": {
"@angular/core": ">=17.0.0"
},
"description": "Rename 'browserTarget' to 'buildTarget'.",
"factory": "./src/migrations/update-17-1-0/browser-target-to-build-target",
"package": "@nx/angular",
"name": "rename-browser-target-to-build-target"
},
{
"cli": "nx",
"version": "17.1.0-beta.5",
"requires": {
"@angular/core": ">=17.0.0"
},
"description": "Replace usages of '@nguniversal/builders' with '@angular-devkit/build-angular'.",
"factory": "./src/migrations/update-17-1-0/replace-nguniversal-builders",
"package": "@nx/angular",
"name": "replace-nguniversal-builders"
},
{
"cli": "nx",
"version": "17.1.0-beta.5",
"requires": {
"@angular/core": ">=17.0.0"
},
"description": "Replace usages of '@nguniversal/' packages with '@angular/ssr'.",
"factory": "./src/migrations/update-17-1-0/replace-nguniversal-engines",
"package": "@nx/angular",
"name": "replace-nguniversal-engines"
},
{
"cli": "nx",
"version": "17.1.0-beta.5",
"requires": {
"@angular/core": ">=17.0.0"
},
"description": "Replace the deep imports from 'zone.js/dist/zone' and 'zone.js/dist/zone-testing' with 'zone.js' and 'zone.js/testing'.",
"factory": "./src/migrations/update-17-1-0/update-zone-js-deep-import",
"package": "@nx/angular",
"name": "update-zone-js-deep-import"
},
{
"cli": "nx",
"version": "17.2.0-beta.2",
"description": "Rename '@nx/angular:webpack-dev-server' executor to '@nx/angular:dev-server'",
"factory": "./src/migrations/update-17-2-0/rename-webpack-dev-server",
"package": "@nx/angular",
"name": "rename-webpack-dev-server-executor"
},
{
"cli": "nx",
"version": "17.3.0-beta.10",
"requires": {
"@angular/core": ">=17.1.0"
},
"description": "Update the @angular/cli package version to ~17.1.0.",
"factory": "./src/migrations/update-17-3-0/update-angular-cli",
"package": "@nx/angular",
"name": "update-angular-cli-version-17-1-0"
},
{
"cli": "nx",
"version": "17.3.0-beta.10",
"requires": {
"@angular/core": ">=17.1.0"
},
"description": "Add 'browser-sync' as dev dependency when '@angular-devkit/build-angular:ssr-dev-server' or '@nx/angular:module-federation-dev-ssr' is used.",
"factory": "./src/migrations/update-17-3-0/add-browser-sync-dependency",
"package": "@nx/angular",
"name": "add-browser-sync-dependency"
},
{
"cli": "nx",
"version": "17.3.0-beta.10",
"requires": {
"@angular/core": ">=17.1.0"
},
"description": "Add 'autoprefixer' as dev dependency when '@nx/angular:ng-packagr-lite' or '@nx/angular:package` is used.",
"factory": "./src/migrations/update-17-3-0/add-autoprefixer-dependency",
"package": "@nx/angular",
"name": "add-autoprefixer-dependency"
},
{
"cli": "nx",
"version": "18.0.0-beta.0",
"description": "Add NX_MF_DEV_SERVER_STATIC_REMOTES to inputs for task hashing when '@nx/angular:webpack-browser' is used for Module Federation.",
"factory": "./src/migrations/update-18-0-0/add-mf-env-var-to-target-defaults",
"package": "@nx/angular",
"name": "add-module-federation-env-var-to-target-defaults"
},
{
"cli": "nx",
"version": "18.1.0-beta.1",
"requires": {
"@angular/core": ">=17.2.0"
},
"description": "Update the @angular/cli package version to ~17.2.0.",
"factory": "./src/migrations/update-18-1-0/update-angular-cli",
"package": "@nx/angular",
"name": "update-angular-cli-version-17-2-0"
},
{
"cli": "nx",
"version": "18.1.1-beta.0",
"description": "Ensure targetDefaults inputs for task hashing when '@nx/angular:webpack-browser' is used are correct for Module Federation.",
"factory": "./src/migrations/update-18-1-1/fix-target-defaults-inputs",
"package": "@nx/angular",
"name": "fix-target-defaults-for-webpack-browser"
},
{
"cli": "nx",
"version": "18.2.0-beta.0",
"requires": {
"@angular/core": ">=17.3.0"
},
"description": "Update the @angular/cli package version to ~17.3.0.",
"factory": "./src/migrations/update-18-2-0/update-angular-cli",
"package": "@nx/angular",
"name": "update-angular-cli-version-17-3-0"
}
]
}

956
package-lock.json

File diff suppressed because it is too large

2
package.json

@ -52,6 +52,7 @@
"ts-node": "ts-node", "ts-node": "ts-node",
"update": "nx migrate latest", "update": "nx migrate latest",
"watch:server": "nx run api:copy-assets && nx run api:build --watch", "watch:server": "nx run api:copy-assets && nx run api:build --watch",
"profile:server": "nx run api:profile",
"watch:test": "nx test --watch", "watch:test": "nx test --watch",
"workspace-generator": "nx workspace-generator" "workspace-generator": "nx workspace-generator"
}, },
@ -92,6 +93,7 @@
"@simplewebauthn/server": "13.1.1", "@simplewebauthn/server": "13.1.1",
"@stripe/stripe-js": "5.4.0", "@stripe/stripe-js": "5.4.0",
"alphavantage": "2.2.0", "alphavantage": "2.2.0",
"await-lock": "^2.2.2",
"big.js": "6.2.2", "big.js": "6.2.2",
"bootstrap": "4.6.0", "bootstrap": "4.6.0",
"bull": "4.16.5", "bull": "4.16.5",

22
prisma/migrations/20231108082445_added_tags_to_holding/migration.sql

@ -0,0 +1,22 @@
-- AlterEnum
ALTER TYPE "Type" ADD VALUE IF NOT EXISTS 'STAKE';
-- CreateTable
CREATE TABLE IF NOT EXISTS "_SymbolProfileToTag" (
"A" TEXT NOT NULL,
"B" TEXT NOT NULL
);
-- CreateIndex
CREATE UNIQUE INDEX IF NOT EXISTS "_SymbolProfileToTag_AB_unique" ON "_SymbolProfileToTag"("A", "B");
-- CreateIndex
CREATE INDEX IF NOT EXISTS "_SymbolProfileToTag_B_index" ON "_SymbolProfileToTag"("B");
-- AddForeignKey
ALTER TABLE "_SymbolProfileToTag" DROP CONSTRAINT IF EXISTS "_SymbolProfileToTag_A_fkey" ;
ALTER TABLE "_SymbolProfileToTag" ADD CONSTRAINT "_SymbolProfileToTag_A_fkey" FOREIGN KEY ("A") REFERENCES "SymbolProfile"("id") ON DELETE CASCADE ON UPDATE CASCADE;
-- AddForeignKey
ALTER TABLE "_SymbolProfileToTag" DROP CONSTRAINT IF EXISTS "_SymbolProfileToTag_B_fkey" ;
ALTER TABLE "_SymbolProfileToTag" ADD CONSTRAINT "_SymbolProfileToTag_B_fkey" FOREIGN KEY ("B") REFERENCES "Tag"("id") ON DELETE CASCADE ON UPDATE CASCADE;

3
prisma/schema.prisma

@ -192,6 +192,7 @@ model SymbolProfile {
url String? url String?
userId String? userId String?
Order Order[] Order Order[]
tags Tag[]
SymbolProfileOverrides SymbolProfileOverrides? SymbolProfileOverrides SymbolProfileOverrides?
User User? @relation(fields: [userId], onDelete: Cascade, references: [id]) User User? @relation(fields: [userId], onDelete: Cascade, references: [id])
@ -237,6 +238,7 @@ model Tag {
orders Order[] orders Order[]
userId String? userId String?
User User? @relation(fields: [userId], onDelete: Cascade, references: [id]) User User? @relation(fields: [userId], onDelete: Cascade, references: [id])
symbolProfile SymbolProfile[]
@@unique([name, userId]) @@unique([name, userId])
@@index([name]) @@index([name])
@ -333,6 +335,7 @@ enum Type {
ITEM ITEM
LIABILITY LIABILITY
SELL SELL
STAKE
} }
enum ViewMode { enum ViewMode {

17248
yarn-error.log

File diff suppressed because it is too large
Loading…
Cancel
Save