Browse Source

Refactoring

pull/4240/head
Thomas Kaul 7 months ago
parent
commit
a8eb7a6643
  1. 36
      apps/api/src/app/portfolio/portfolio.service.ts
  2. 13
      apps/api/src/app/user/user.service.ts
  3. 17
      apps/api/src/models/rules/regional-market-cluster-risk/north-america.ts
  4. 2
      libs/common/src/lib/interfaces/x-ray-rules-settings.interface.ts

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

@ -1176,14 +1176,14 @@ export class PortfolioService {
withSummary: true withSummary: true
}); });
const marketsTotalInBaseCurrency = getSum( const marketsAdvancedTotalInBaseCurrency = getSum(
Object.values(markets).map(({ valueInBaseCurrency }) => { Object.values(marketsAdvanced).map(({ valueInBaseCurrency }) => {
return new Big(valueInBaseCurrency); return new Big(valueInBaseCurrency);
}) })
).toNumber(); ).toNumber();
const marketsAdvancedTotalInBaseCurrency = getSum( const marketsTotalInBaseCurrency = getSum(
Object.values(marketsAdvanced).map(({ valueInBaseCurrency }) => { Object.values(markets).map(({ valueInBaseCurrency }) => {
return new Big(valueInBaseCurrency); return new Big(valueInBaseCurrency);
}) })
).toNumber(); ).toNumber();
@ -1255,19 +1255,6 @@ export class PortfolioService {
userSettings userSettings
) )
: undefined, : undefined,
regionalMarketClusterRisk:
summary.ordersCount > 0
? await this.rulesService.evaluate(
[
new RegionalMarketClusterRiskNorthAmerica(
this.exchangeRateDataService,
marketsAdvancedTotalInBaseCurrency,
marketsAdvanced.northAmerica.valueInBaseCurrency
)
],
userSettings
)
: undefined,
emergencyFund: await this.rulesService.evaluate( emergencyFund: await this.rulesService.evaluate(
[ [
new EmergencyFundSetup( new EmergencyFundSetup(
@ -1286,7 +1273,20 @@ export class PortfolioService {
) )
], ],
userSettings userSettings
) ),
regionalMarketClusterRisk:
summary.ordersCount > 0
? await this.rulesService.evaluate(
[
new RegionalMarketClusterRiskNorthAmerica(
this.exchangeRateDataService,
marketsAdvancedTotalInBaseCurrency,
marketsAdvanced.northAmerica.valueInBaseCurrency
)
],
userSettings
)
: undefined
}; };
return { rules, statistics: this.getReportStatistics(rules) }; return { rules, statistics: this.getReportStatistics(rules) };

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

@ -261,11 +261,6 @@ export class UserService {
undefined, undefined,
undefined undefined
).getSettings(user.Settings.settings), ).getSettings(user.Settings.settings),
RegionalMarketClusterRisk: new RegionalMarketClusterRiskNorthAmerica(
undefined,
undefined,
undefined
).getSettings(user.Settings.settings),
EmergencyFundSetup: new EmergencyFundSetup( EmergencyFundSetup: new EmergencyFundSetup(
undefined, undefined,
undefined undefined
@ -274,7 +269,13 @@ export class UserService {
undefined, undefined,
undefined, undefined,
undefined undefined
).getSettings(user.Settings.settings) ).getSettings(user.Settings.settings),
RegionalMarketClusterRiskNorthAmerica:
new RegionalMarketClusterRiskNorthAmerica(
undefined,
undefined,
undefined
).getSettings(user.Settings.settings)
}; };
let currentPermissions = getPermissions(user.role); let currentPermissions = getPermissions(user.role);

17
apps/api/src/models/rules/regional-market-cluster-risk/north-america.ts

@ -5,7 +5,7 @@ import { UserSettings } from '@ghostfolio/common/interfaces';
export class RegionalMarketClusterRiskNorthAmerica extends Rule<Settings> { export class RegionalMarketClusterRiskNorthAmerica extends Rule<Settings> {
private currentValueInBaseCurrency: number; private currentValueInBaseCurrency: number;
private regionalMarketNorthAmericaValueInBaseCurrency: number; private northAmericaValueInBaseCurrency: number;
public constructor( public constructor(
protected exchangeRateDataService: ExchangeRateDataService, protected exchangeRateDataService: ExchangeRateDataService,
@ -14,27 +14,28 @@ export class RegionalMarketClusterRiskNorthAmerica extends Rule<Settings> {
) { ) {
super(exchangeRateDataService, { super(exchangeRateDataService, {
key: RegionalMarketClusterRiskNorthAmerica.name, key: RegionalMarketClusterRiskNorthAmerica.name,
name: 'Regional Markets' name: 'North America'
}); });
this.currentValueInBaseCurrency = currentValueInBaseCurrency; this.currentValueInBaseCurrency = currentValueInBaseCurrency;
this.regionalMarketNorthAmericaValueInBaseCurrency = valueInBaseCurrency; this.northAmericaValueInBaseCurrency = valueInBaseCurrency;
} }
public evaluate(ruleSettings: Settings) { public evaluate(ruleSettings: Settings) {
const northAmericaMarketValueRatio = this.currentValueInBaseCurrency const northAmericaMarketValueRatio = this.currentValueInBaseCurrency
? this.regionalMarketNorthAmericaValueInBaseCurrency / ? this.northAmericaValueInBaseCurrency / this.currentValueInBaseCurrency
this.currentValueInBaseCurrency
: 0; : 0;
if (northAmericaMarketValueRatio > ruleSettings.thresholdMax) { if (northAmericaMarketValueRatio > ruleSettings.thresholdMax) {
return { return {
evaluation: `The north america market contribution of your current investment (${(northAmericaMarketValueRatio * 100).toPrecision(3)}%) exceeds ${( evaluation: `The North America market contribution of your current investment (${(northAmericaMarketValueRatio * 100).toPrecision(3)}%) exceeds ${(
ruleSettings.thresholdMax * 100 ruleSettings.thresholdMax * 100
).toPrecision(3)}%`, ).toPrecision(3)}%`,
value: false value: false
}; };
} else if (northAmericaMarketValueRatio < ruleSettings.thresholdMin) { } else if (northAmericaMarketValueRatio < ruleSettings.thresholdMin) {
return { return {
evaluation: `The north america market contribution of your current investment (${(northAmericaMarketValueRatio * 100).toPrecision(3)}%) is below ${( evaluation: `The North America market contribution of your current investment (${(northAmericaMarketValueRatio * 100).toPrecision(3)}%) is below ${(
ruleSettings.thresholdMin * 100 ruleSettings.thresholdMin * 100
).toPrecision(3)}%`, ).toPrecision(3)}%`,
value: false value: false
@ -42,7 +43,7 @@ export class RegionalMarketClusterRiskNorthAmerica extends Rule<Settings> {
} }
return { return {
evaluation: `The north america market contribution of your current investment (${(northAmericaMarketValueRatio * 100).toPrecision(3)}%) is within the range of ${( evaluation: `The North America market contribution of your current investment (${(northAmericaMarketValueRatio * 100).toPrecision(3)}%) is within the range of ${(
ruleSettings.thresholdMin * 100 ruleSettings.thresholdMin * 100
).toPrecision( ).toPrecision(
3 3

2
libs/common/src/lib/interfaces/x-ray-rules-settings.interface.ts

@ -7,9 +7,9 @@ export interface XRayRulesSettings {
CurrencyClusterRiskCurrentInvestment?: RuleSettings; CurrencyClusterRiskCurrentInvestment?: RuleSettings;
EconomicMarketClusterRiskDevelopedMarkets?: RuleSettings; EconomicMarketClusterRiskDevelopedMarkets?: RuleSettings;
EconomicMarketClusterRiskEmergingMarkets?: RuleSettings; EconomicMarketClusterRiskEmergingMarkets?: RuleSettings;
RegionalMarketClusterRisk?: RuleSettings;
EmergencyFundSetup?: RuleSettings; EmergencyFundSetup?: RuleSettings;
FeeRatioInitialInvestment?: RuleSettings; FeeRatioInitialInvestment?: RuleSettings;
RegionalMarketClusterRiskNorthAmerica?: RuleSettings;
} }
interface RuleSettings { interface RuleSettings {

Loading…
Cancel
Save