Thomas Kaul 3 days ago
committed by GitHub
parent
commit
829554ebce
No known key found for this signature in database GPG Key ID: B5690EEEBB952194
  1. 8
      apps/api/src/models/rule.ts
  2. 3
      apps/api/src/models/rules/asset-class-cluster-risk/equity.ts
  3. 3
      apps/api/src/models/rules/asset-class-cluster-risk/fixed-income.ts
  4. 2
      apps/api/src/models/rules/currency-cluster-risk/base-currency-current-investment.ts
  5. 2
      apps/api/src/models/rules/currency-cluster-risk/current-investment.ts
  6. 14
      libs/common/src/lib/helper.ts

8
apps/api/src/models/rule.ts

@ -1,6 +1,5 @@
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 { DEFAULT_LANGUAGE_CODE } from '@ghostfolio/common/config'; import { DEFAULT_LANGUAGE_CODE } from '@ghostfolio/common/config';
import { groupBy } from '@ghostfolio/common/helper';
import { import {
PortfolioPosition, PortfolioPosition,
PortfolioReportRule, PortfolioReportRule,
@ -9,6 +8,7 @@ import {
} from '@ghostfolio/common/interfaces'; } from '@ghostfolio/common/interfaces';
import { Big } from 'big.js'; import { Big } from 'big.js';
import { groupBy } from 'lodash';
import { EvaluationResult } from './interfaces/evaluation-result.interface'; import { EvaluationResult } from './interfaces/evaluation-result.interface';
import { RuleInterface } from './interfaces/rule.interface'; import { RuleInterface } from './interfaces/rule.interface';
@ -41,10 +41,10 @@ export abstract class Rule<T extends RuleSettings> implements RuleInterface<T> {
public groupCurrentHoldingsByAttribute( public groupCurrentHoldingsByAttribute(
holdings: PortfolioPosition[], holdings: PortfolioPosition[],
attribute: keyof PortfolioPosition, attribute: `assetProfile.${Extract<keyof PortfolioPosition['assetProfile'], string>}`,
baseCurrency: string baseCurrency: string
) { ) {
return Array.from(groupBy(attribute, holdings).entries()).map( return Object.entries(groupBy(holdings, attribute)).map(
([attributeValue, objs]) => ({ ([attributeValue, objs]) => ({
groupKey: attributeValue, groupKey: attributeValue,
investment: objs.reduce( investment: objs.reduce(
@ -59,7 +59,7 @@ export abstract class Rule<T extends RuleSettings> implements RuleInterface<T> {
new Big(currentValue.quantity) new Big(currentValue.quantity)
.mul(currentValue.marketPrice ?? 0) .mul(currentValue.marketPrice ?? 0)
.toNumber(), .toNumber(),
currentValue.currency, currentValue.assetProfile.currency,
baseCurrency baseCurrency
), ),
0 0

3
apps/api/src/models/rules/asset-class-cluster-risk/equity.ts

@ -27,9 +27,10 @@ export class AssetClassClusterRiskEquity extends Rule<Settings> {
public evaluate(ruleSettings: Settings) { public evaluate(ruleSettings: Settings) {
const holdingsGroupedByAssetClass = this.groupCurrentHoldingsByAttribute( const holdingsGroupedByAssetClass = this.groupCurrentHoldingsByAttribute(
this.holdings, this.holdings,
'assetClass', 'assetProfile.assetClass',
ruleSettings.baseCurrency ruleSettings.baseCurrency
); );
let totalValue = 0; let totalValue = 0;
const equityValueInBaseCurrency = const equityValueInBaseCurrency =

3
apps/api/src/models/rules/asset-class-cluster-risk/fixed-income.ts

@ -27,9 +27,10 @@ export class AssetClassClusterRiskFixedIncome extends Rule<Settings> {
public evaluate(ruleSettings: Settings) { public evaluate(ruleSettings: Settings) {
const holdingsGroupedByAssetClass = this.groupCurrentHoldingsByAttribute( const holdingsGroupedByAssetClass = this.groupCurrentHoldingsByAttribute(
this.holdings, this.holdings,
'assetClass', 'assetProfile.assetClass',
ruleSettings.baseCurrency ruleSettings.baseCurrency
); );
let totalValue = 0; let totalValue = 0;
const fixedIncomeValueInBaseCurrency = const fixedIncomeValueInBaseCurrency =

2
apps/api/src/models/rules/currency-cluster-risk/base-currency-current-investment.ts

@ -27,7 +27,7 @@ export class CurrencyClusterRiskBaseCurrencyCurrentInvestment extends Rule<Setti
public evaluate(ruleSettings: Settings) { public evaluate(ruleSettings: Settings) {
const holdingsGroupedByCurrency = this.groupCurrentHoldingsByAttribute( const holdingsGroupedByCurrency = this.groupCurrentHoldingsByAttribute(
this.holdings, this.holdings,
'currency', 'assetProfile.currency',
ruleSettings.baseCurrency ruleSettings.baseCurrency
); );

2
apps/api/src/models/rules/currency-cluster-risk/current-investment.ts

@ -27,7 +27,7 @@ export class CurrencyClusterRiskCurrentInvestment extends Rule<Settings> {
public evaluate(ruleSettings: Settings) { public evaluate(ruleSettings: Settings) {
const holdingsGroupedByCurrency = this.groupCurrentHoldingsByAttribute( const holdingsGroupedByCurrency = this.groupCurrentHoldingsByAttribute(
this.holdings, this.holdings,
'currency', 'assetProfile.currency',
ruleSettings.baseCurrency ruleSettings.baseCurrency
); );

14
libs/common/src/lib/helper.ts

@ -342,20 +342,6 @@ export function getYesterday() {
return subDays(new Date(Date.UTC(year, month, day)), 1); return subDays(new Date(Date.UTC(year, month, day)), 1);
} }
export function groupBy<T, K extends keyof T>(
key: K,
arr: T[]
): Map<T[K], T[]> {
const map = new Map<T[K], T[]>();
arr.forEach((t) => {
if (!map.has(t[key])) {
map.set(t[key], []);
}
map.get(t[key])!.push(t);
});
return map;
}
export function interpolate(template: string, context: any) { export function interpolate(template: string, context: any) {
return template?.replace(/[$]{([^}]+)}/g, (_, objectPath) => { return template?.replace(/[$]{([^}]+)}/g, (_, objectPath) => {
const properties = objectPath.split('.'); const properties = objectPath.split('.');

Loading…
Cancel
Save