Browse Source

Refactoring

pull/4240/head
Thomas Kaul 7 months ago
parent
commit
a8eb7a6643
  1. 34
      apps/api/src/app/portfolio/portfolio.service.ts
  2. 9
      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

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

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

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

@ -261,16 +261,17 @@ export class UserService {
undefined,
undefined
).getSettings(user.Settings.settings),
RegionalMarketClusterRisk: new RegionalMarketClusterRiskNorthAmerica(
undefined,
EmergencyFundSetup: new EmergencyFundSetup(
undefined,
undefined
).getSettings(user.Settings.settings),
EmergencyFundSetup: new EmergencyFundSetup(
FeeRatioInitialInvestment: new FeeRatioInitialInvestment(
undefined,
undefined,
undefined
).getSettings(user.Settings.settings),
FeeRatioInitialInvestment: new FeeRatioInitialInvestment(
RegionalMarketClusterRiskNorthAmerica:
new RegionalMarketClusterRiskNorthAmerica(
undefined,
undefined,
undefined

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> {
private currentValueInBaseCurrency: number;
private regionalMarketNorthAmericaValueInBaseCurrency: number;
private northAmericaValueInBaseCurrency: number;
public constructor(
protected exchangeRateDataService: ExchangeRateDataService,
@ -14,27 +14,28 @@ export class RegionalMarketClusterRiskNorthAmerica extends Rule<Settings> {
) {
super(exchangeRateDataService, {
key: RegionalMarketClusterRiskNorthAmerica.name,
name: 'Regional Markets'
name: 'North America'
});
this.currentValueInBaseCurrency = currentValueInBaseCurrency;
this.regionalMarketNorthAmericaValueInBaseCurrency = valueInBaseCurrency;
this.northAmericaValueInBaseCurrency = valueInBaseCurrency;
}
public evaluate(ruleSettings: Settings) {
const northAmericaMarketValueRatio = this.currentValueInBaseCurrency
? this.regionalMarketNorthAmericaValueInBaseCurrency /
this.currentValueInBaseCurrency
? this.northAmericaValueInBaseCurrency / this.currentValueInBaseCurrency
: 0;
if (northAmericaMarketValueRatio > ruleSettings.thresholdMax) {
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
).toPrecision(3)}%`,
value: false
};
} else if (northAmericaMarketValueRatio < ruleSettings.thresholdMin) {
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
).toPrecision(3)}%`,
value: false
@ -42,7 +43,7 @@ export class RegionalMarketClusterRiskNorthAmerica extends Rule<Settings> {
}
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
).toPrecision(
3

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

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

Loading…
Cancel
Save