Browse Source

feat: address review suggestions

pull/3949/head
vitalymatyushik 10 months ago
committed by Thomas Kaul
parent
commit
466028ae98
  1. 20
      apps/api/src/app/portfolio/portfolio.service.ts
  2. 8
      apps/api/src/app/user/user.service.ts
  3. 32
      apps/api/src/models/rules/allocation-cluster-risk/emerging-markets.ts
  4. 8
      apps/client/src/app/pages/portfolio/fire/fire-page.component.ts
  5. 19
      apps/client/src/app/pages/portfolio/fire/fire-page.html
  6. 2
      libs/common/src/lib/types/x-ray-rules-settings.type.ts

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

@ -1161,7 +1161,7 @@ export class PortfolioService {
const userId = await this.getUserId(impersonationId, this.request.user.id); const userId = await this.getUserId(impersonationId, this.request.user.id);
const userSettings = <UserSettings>this.request.user.Settings.settings; const userSettings = <UserSettings>this.request.user.Settings.settings;
const { accounts, holdings, summary, markets } = await this.getDetails({ const { accounts, holdings, markets, summary } = await this.getDetails({
impersonationId, impersonationId,
userId, userId,
withMarkets: true, withMarkets: true,
@ -1181,10 +1181,18 @@ export class PortfolioService {
new AccountClusterRiskSingleAccount( new AccountClusterRiskSingleAccount(
this.exchangeRateDataService, this.exchangeRateDataService,
accounts accounts
), )
],
userSettings
)
: undefined,
allocationClusterRisk:
summary.ordersCount > 0
? await this.rulesService.evaluate(
[
new AllocationClusterRiskEmergingMarkets( new AllocationClusterRiskEmergingMarkets(
this.exchangeRateDataService, this.exchangeRateDataService,
summary.committedFunds, summary.currentValueInBaseCurrency,
markets.emergingMarkets.valueInBaseCurrency markets.emergingMarkets.valueInBaseCurrency
) )
], ],
@ -1248,9 +1256,7 @@ export class PortfolioService {
await this.orderService.assignTags({ dataSource, symbol, tags, userId }); await this.orderService.assignTags({ dataSource, symbol, tags, userId });
} }
private getAggregatedMarkets(holdings: { private getAggregatedMarkets(holdings: Record<string, PortfolioPosition>): {
[symbol: string]: PortfolioPosition;
}): {
markets: PortfolioDetails['markets']; markets: PortfolioDetails['markets'];
marketsAdvanced: PortfolioDetails['marketsAdvanced']; marketsAdvanced: PortfolioDetails['marketsAdvanced'];
} { } {
@ -1909,7 +1915,7 @@ export class PortfolioService {
}: { }: {
activities: Activity[]; activities: Activity[];
filters?: Filter[]; filters?: Filter[];
portfolioItemsNow: { [p: string]: TimelinePosition }; portfolioItemsNow: Record<string, TimelinePosition>;
userCurrency: string; userCurrency: string;
userId: string; userId: string;
withExcludedAccounts?: boolean; withExcludedAccounts?: boolean;

8
apps/api/src/app/user/user.service.ts

@ -217,9 +217,11 @@ export class UserService {
{} {}
).getSettings(user.Settings.settings), ).getSettings(user.Settings.settings),
AllocationClusterRiskEmergingMarkets: AllocationClusterRiskEmergingMarkets:
new AllocationClusterRiskEmergingMarkets(undefined, 0, 0).getSettings( new AllocationClusterRiskEmergingMarkets(
user.Settings.settings undefined,
), undefined,
undefined
).getSettings(user.Settings.settings),
CurrencyClusterRiskBaseCurrencyCurrentInvestment: CurrencyClusterRiskBaseCurrencyCurrentInvestment:
new CurrencyClusterRiskBaseCurrencyCurrentInvestment( new CurrencyClusterRiskBaseCurrencyCurrentInvestment(
undefined, undefined,

32
apps/api/src/models/rules/allocation-cluster-risk/emerging-markets.ts

@ -4,12 +4,12 @@ import { ExchangeRateDataService } from '@ghostfolio/api/services/exchange-rate-
import { UserSettings } from '@ghostfolio/common/interfaces'; import { UserSettings } from '@ghostfolio/common/interfaces';
export class AllocationClusterRiskEmergingMarkets extends Rule<Settings> { export class AllocationClusterRiskEmergingMarkets extends Rule<Settings> {
private currentValueInBaseCurrency: number;
private emergingMarketsValueInBaseCurrency: number; private emergingMarketsValueInBaseCurrency: number;
private totalInvestment: number;
public constructor( public constructor(
protected exchangeRateDataService: ExchangeRateDataService, protected exchangeRateDataService: ExchangeRateDataService,
totalInvestment: number, currentValueInBaseCurrency: number,
emergingMarketsValueInBaseCurrency: number emergingMarketsValueInBaseCurrency: number
) { ) {
super(exchangeRateDataService, { super(exchangeRateDataService, {
@ -19,38 +19,41 @@ export class AllocationClusterRiskEmergingMarkets extends Rule<Settings> {
this.emergingMarketsValueInBaseCurrency = this.emergingMarketsValueInBaseCurrency =
emergingMarketsValueInBaseCurrency; emergingMarketsValueInBaseCurrency;
this.totalInvestment = totalInvestment; this.currentValueInBaseCurrency = currentValueInBaseCurrency;
} }
public evaluate(ruleSettings: Settings) { public evaluate(ruleSettings: Settings) {
const emergingMarketsValueRatio = this.totalInvestment const emergingMarketsValueRatio = this.currentValueInBaseCurrency
? this.emergingMarketsValueInBaseCurrency / this.totalInvestment ? this.emergingMarketsValueInBaseCurrency /
this.currentValueInBaseCurrency
: 0; : 0;
if (emergingMarketsValueRatio > ruleSettings.thresholdMax) { if (emergingMarketsValueRatio > ruleSettings.thresholdMax) {
return { return {
evaluation: `The emerging markets contribution exceed ${( evaluation: `The emerging markets contribution exceeds ${(
ruleSettings.thresholdMax * 100 ruleSettings.thresholdMax * 100
).toPrecision( ).toPrecision(
3 3
)}% of your initial investment (${(emergingMarketsValueRatio * 100).toPrecision(3)}%)`, )}% of your current investment (${(emergingMarketsValueRatio * 100).toPrecision(3)}%)`,
value: false value: false
}; };
} else if (emergingMarketsValueRatio < ruleSettings.thresholdMin) { } else if (emergingMarketsValueRatio < ruleSettings.thresholdMin) {
return { return {
evaluation: `The emerging markets contribution does not exceed ${( evaluation: `The emerging markets contribution is below ${(
ruleSettings.thresholdMin * 100 ruleSettings.thresholdMin * 100
).toPrecision( ).toPrecision(
3 3
)}% of your initial investment (${(emergingMarketsValueRatio * 100).toPrecision(3)}%)`, )}% of your current investment (${(emergingMarketsValueRatio * 100).toPrecision(3)}%)`,
value: false value: false
}; };
} }
return { return {
evaluation: `The emerging markets contribution does not exceed ${ evaluation: `The emerging markets contribution is within the range of ${(
ruleSettings.thresholdMax * 100 ruleSettings.thresholdMin * 100
}% of your initial investment (${(emergingMarketsValueRatio * 100).toPrecision(3)}%)`, ).toPrecision(
3
)}% and ${(ruleSettings.thresholdMax * 100).toPrecision(3)}% of your current investment (${(emergingMarketsValueRatio * 100).toPrecision(3)}%)`,
value: true value: true
}; };
} }
@ -60,10 +63,11 @@ export class AllocationClusterRiskEmergingMarkets extends Rule<Settings> {
threshold: { threshold: {
max: 1, max: 1,
min: 0, min: 0,
step: 0.0025, step: 0.01,
unit: '%' unit: '%'
}, },
thresholdMax: true thresholdMax: true,
thresholdMin: true
}; };
} }

8
apps/client/src/app/pages/portfolio/fire/fire-page.component.ts

@ -22,6 +22,7 @@ import { takeUntil } from 'rxjs/operators';
}) })
export class FirePageComponent implements OnDestroy, OnInit { export class FirePageComponent implements OnDestroy, OnInit {
public accountClusterRiskRules: PortfolioReportRule[]; public accountClusterRiskRules: PortfolioReportRule[];
public allocationClusterRiskRules: PortfolioReportRule[];
public currencyClusterRiskRules: PortfolioReportRule[]; public currencyClusterRiskRules: PortfolioReportRule[];
public deviceType: string; public deviceType: string;
public emergencyFundRules: PortfolioReportRule[]; public emergencyFundRules: PortfolioReportRule[];
@ -203,6 +204,13 @@ export class FirePageComponent implements OnDestroy, OnInit {
} }
) ?? null; ) ?? null;
this.allocationClusterRiskRules =
portfolioReport.rules['allocationClusterRisk']?.filter(
({ isActive }) => {
return isActive;
}
) ?? null;
this.currencyClusterRiskRules = this.currencyClusterRiskRules =
portfolioReport.rules['currencyClusterRisk']?.filter( portfolioReport.rules['currencyClusterRisk']?.filter(
({ isActive }) => { ({ isActive }) => {

19
apps/client/src/app/pages/portfolio/fire/fire-page.html

@ -174,6 +174,25 @@
(rulesUpdated)="onRulesUpdated($event)" (rulesUpdated)="onRulesUpdated($event)"
/> />
</div> </div>
<div class="mb-4">
<h4 class="align-items-center d-flex m-0">
<span i18n>Allocation Cluster Risks</span>
@if (user?.subscription?.type === 'Basic') {
<gf-premium-indicator class="ml-1" />
}
</h4>
<gf-rules
[hasPermissionToUpdateUserSettings]="
!hasImpersonationId &&
hasPermissionToUpdateUserSettings &&
user?.settings?.isExperimentalFeatures
"
[isLoading]="isLoadingPortfolioReport"
[rules]="allocationClusterRiskRules"
[settings]="user?.settings?.xRayRules"
(rulesUpdated)="onRulesUpdated($event)"
/>
</div>
<div class="mb-4"> <div class="mb-4">
<h4 class="align-items-center d-flex m-0"> <h4 class="align-items-center d-flex m-0">
<span i18n>Fees</span> <span i18n>Fees</span>

2
libs/common/src/lib/types/x-ray-rules-settings.type.ts

@ -1,7 +1,7 @@
export type XRayRulesSettings = { export type XRayRulesSettings = {
AccountClusterRiskCurrentInvestment?: RuleSettings; AccountClusterRiskCurrentInvestment?: RuleSettings;
AllocationClusterRiskEmergingMarkets?: RuleSettings;
AccountClusterRiskSingleAccount?: RuleSettings; AccountClusterRiskSingleAccount?: RuleSettings;
AllocationClusterRiskEmergingMarkets?: RuleSettings;
CurrencyClusterRiskBaseCurrencyCurrentInvestment?: RuleSettings; CurrencyClusterRiskBaseCurrencyCurrentInvestment?: RuleSettings;
CurrencyClusterRiskCurrentInvestment?: RuleSettings; CurrencyClusterRiskCurrentInvestment?: RuleSettings;
EmergencyFundSetup?: RuleSettings; EmergencyFundSetup?: RuleSettings;

Loading…
Cancel
Save