Skip to content

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

Feature/add regional market cluster risk rule for Europe (#4271)

Shaunak Das06298089db57
diff --git a/CHANGELOG.md b/CHANGELOG.md
index 4de4ab2ccb..eb4468a9de 100644
--- a/CHANGELOG.md
+++ b/CHANGELOG.md
@@ -9,6 +9,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
### Added
+- Added a new static portfolio analysis rule: _Regional Market Cluster Risk_ (Europe)
- Added a link to _Duck.ai_ to the _Copy AI prompt to clipboard_ action on the analysis page (experimental)
- Extracted the tags selector to a reusable component used in the create or update activity dialog and holding detail dialog
diff --git a/apps/api/src/app/portfolio/portfolio.service.ts b/apps/api/src/app/portfolio/portfolio.service.ts
index a14f97d267..bbe2a6ca5d 100644
--- a/apps/api/src/app/portfolio/portfolio.service.ts
+++ b/apps/api/src/app/portfolio/portfolio.service.ts
@@ -15,6 +15,7 @@ import { EconomicMarketClusterRiskDevelopedMarkets } from '@ghostfolio/api/model
import { EconomicMarketClusterRiskEmergingMarkets } from '@ghostfolio/api/models/rules/economic-market-cluster-risk/emerging-markets';
import { EmergencyFundSetup } from '@ghostfolio/api/models/rules/emergency-fund/emergency-fund-setup';
import { FeeRatioInitialInvestment } from '@ghostfolio/api/models/rules/fees/fee-ratio-initial-investment';
+import { RegionalMarketClusterRiskEurope } from '@ghostfolio/api/models/rules/regional-market-cluster-risk/europe';
import { RegionalMarketClusterRiskNorthAmerica } from '@ghostfolio/api/models/rules/regional-market-cluster-risk/north-america';
import { DataProviderService } from '@ghostfolio/api/services/data-provider/data-provider.service';
import { ExchangeRateDataService } from '@ghostfolio/api/services/exchange-rate-data/exchange-rate-data.service';
@@ -1278,6 +1279,11 @@ export class PortfolioService {
summary.ordersCount > 0
? await this.rulesService.evaluate(
[
+ new RegionalMarketClusterRiskEurope(
+ this.exchangeRateDataService,
+ marketsAdvancedTotalInBaseCurrency,
+ marketsAdvanced.europe.valueInBaseCurrency
+ ),
new RegionalMarketClusterRiskNorthAmerica(
this.exchangeRateDataService,
marketsAdvancedTotalInBaseCurrency,
diff --git a/apps/api/src/app/user/user.service.ts b/apps/api/src/app/user/user.service.ts
index 415cbc99df..44a29e7379 100644
--- a/apps/api/src/app/user/user.service.ts
+++ b/apps/api/src/app/user/user.service.ts
@@ -13,6 +13,7 @@ import { EconomicMarketClusterRiskDevelopedMarkets } from '@ghostfolio/api/model
import { EconomicMarketClusterRiskEmergingMarkets } from '@ghostfolio/api/models/rules/economic-market-cluster-risk/emerging-markets';
import { EmergencyFundSetup } from '@ghostfolio/api/models/rules/emergency-fund/emergency-fund-setup';
import { FeeRatioInitialInvestment } from '@ghostfolio/api/models/rules/fees/fee-ratio-initial-investment';
+import { RegionalMarketClusterRiskEurope } from '@ghostfolio/api/models/rules/regional-market-cluster-risk/europe';
import { RegionalMarketClusterRiskNorthAmerica } from '@ghostfolio/api/models/rules/regional-market-cluster-risk/north-america';
import { ConfigurationService } from '@ghostfolio/api/services/configuration/configuration.service';
import { I18nService } from '@ghostfolio/api/services/i18n/i18n.service';
@@ -270,6 +271,11 @@ export class UserService {
undefined,
undefined
).getSettings(user.Settings.settings),
+ RegionalMarketClusterRiskEurope: new RegionalMarketClusterRiskEurope(
+ undefined,
+ undefined,
+ undefined
+ ).getSettings(user.Settings.settings),
RegionalMarketClusterRiskNorthAmerica:
new RegionalMarketClusterRiskNorthAmerica(
undefined,
diff --git a/apps/api/src/models/rules/regional-market-cluster-risk/europe.ts b/apps/api/src/models/rules/regional-market-cluster-risk/europe.ts
new file mode 100644
index 0000000000..d33190ceb5
--- /dev/null
+++ b/apps/api/src/models/rules/regional-market-cluster-risk/europe.ts
@@ -0,0 +1,77 @@
+import { Rule } from '@ghostfolio/api/models/rule';
+import { ExchangeRateDataService } from '@ghostfolio/api/services/exchange-rate-data/exchange-rate-data.service';
+import { UserSettings } from '@ghostfolio/common/interfaces';
+
+import { Settings } from './interfaces/rule-settings.interface';
+
+export class RegionalMarketClusterRiskEurope extends Rule<Settings> {
+ private currentValueInBaseCurrency: number;
+ private europeValueInBaseCurrency: number;
+
+ public constructor(
+ protected exchangeRateDataService: ExchangeRateDataService,
+ currentValueInBaseCurrency: number,
+ europeValueInBaseCurrency: number
+ ) {
+ super(exchangeRateDataService, {
+ key: RegionalMarketClusterRiskEurope.name,
+ name: 'Europe'
+ });
+
+ this.currentValueInBaseCurrency = currentValueInBaseCurrency;
+ this.europeValueInBaseCurrency = europeValueInBaseCurrency;
+ }
+
+ public evaluate(ruleSettings: Settings) {
+ const europeMarketValueRatio = this.currentValueInBaseCurrency
+ ? this.europeValueInBaseCurrency / this.currentValueInBaseCurrency
+ : 0;
+
+ if (europeMarketValueRatio > ruleSettings.thresholdMax) {
+ return {
+ evaluation: `The Europe market contribution of your current investment (${(europeMarketValueRatio * 100).toPrecision(3)}%) exceeds ${(
+ ruleSettings.thresholdMax * 100
+ ).toPrecision(3)}%`,
+ value: false
+ };
+ } else if (europeMarketValueRatio < ruleSettings.thresholdMin) {
+ return {
+ evaluation: `The Europe market contribution of your current investment (${(europeMarketValueRatio * 100).toPrecision(3)}%) is below ${(
+ ruleSettings.thresholdMin * 100
+ ).toPrecision(3)}%`,
+ value: false
+ };
+ }
+
+ return {
+ evaluation: `The Europe market contribution of your current investment (${(europeMarketValueRatio * 100).toPrecision(3)}%) is within the range of ${(
+ ruleSettings.thresholdMin * 100
+ ).toPrecision(
+ 3
+ )}% and ${(ruleSettings.thresholdMax * 100).toPrecision(3)}%`,
+ value: true
+ };
+ }
+
+ public getConfiguration() {
+ return {
+ threshold: {
+ max: 1,
+ min: 0,
+ step: 0.01,
+ unit: '%'
+ },
+ thresholdMax: true,
+ thresholdMin: true
+ };
+ }
+
+ public getSettings({ baseCurrency, xRayRules }: UserSettings): Settings {
+ return {
+ baseCurrency,
+ isActive: xRayRules?.[this.getKey()]?.isActive ?? true,
+ thresholdMax: xRayRules?.[this.getKey()]?.thresholdMax ?? 0.15,
+ thresholdMin: xRayRules?.[this.getKey()]?.thresholdMin ?? 0.11
+ };
+ }
+}
diff --git a/apps/api/src/models/rules/regional-market-cluster-risk/interfaces/rule-settings.interface.ts b/apps/api/src/models/rules/regional-market-cluster-risk/interfaces/rule-settings.interface.ts
new file mode 100644
index 0000000000..8b9fddf3ab
--- /dev/null
+++ b/apps/api/src/models/rules/regional-market-cluster-risk/interfaces/rule-settings.interface.ts
@@ -0,0 +1,7 @@
+import { RuleSettings } from '@ghostfolio/api/models/interfaces/rule-settings.interface';
+
+export interface Settings extends RuleSettings {
+ baseCurrency: string;
+ thresholdMin: number;
+ thresholdMax: number;
+}
diff --git a/apps/api/src/models/rules/regional-market-cluster-risk/north-america.ts b/apps/api/src/models/rules/regional-market-cluster-risk/north-america.ts
index dc3f6d9794..4563b7c54d 100644
--- a/apps/api/src/models/rules/regional-market-cluster-risk/north-america.ts
+++ b/apps/api/src/models/rules/regional-market-cluster-risk/north-america.ts
@@ -1,8 +1,9 @@
-import { RuleSettings } from '@ghostfolio/api/models/interfaces/rule-settings.interface';
import { Rule } from '@ghostfolio/api/models/rule';
import { ExchangeRateDataService } from '@ghostfolio/api/services/exchange-rate-data/exchange-rate-data.service';
import { UserSettings } from '@ghostfolio/common/interfaces';
+import { Settings } from './interfaces/rule-settings.interface';
+
export class RegionalMarketClusterRiskNorthAmerica extends Rule<Settings> {
private currentValueInBaseCurrency: number;
private northAmericaValueInBaseCurrency: number;
@@ -74,9 +75,3 @@ export class RegionalMarketClusterRiskNorthAmerica extends Rule<Settings> {
};
}
}
-
-interface Settings extends RuleSettings {
- baseCurrency: string;
- thresholdMin: number;
- thresholdMax: number;
-}
diff --git a/libs/common/src/lib/interfaces/x-ray-rules-settings.interface.ts b/libs/common/src/lib/interfaces/x-ray-rules-settings.interface.ts
index 61f79d65f9..f1ca5c683b 100644
--- a/libs/common/src/lib/interfaces/x-ray-rules-settings.interface.ts
+++ b/libs/common/src/lib/interfaces/x-ray-rules-settings.interface.ts
@@ -9,6 +9,7 @@ export interface XRayRulesSettings {
EconomicMarketClusterRiskEmergingMarkets?: RuleSettings;
EmergencyFundSetup?: RuleSettings;
FeeRatioInitialInvestment?: RuleSettings;
+ RegionalMarketClusterRiskEurope?: RuleSettings;
RegionalMarketClusterRiskNorthAmerica?: RuleSettings;
}