You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
76 lines
2.1 KiB
76 lines
2.1 KiB
import { Currency } from '@prisma/client';
|
|
import { PortfolioPosition } from '@ghostfolio/common/interfaces';
|
|
import { ExchangeRateDataService } from 'apps/api/src/services/exchange-rate-data.service';
|
|
|
|
import { Rule } from '../../rule';
|
|
import { UserSettings } from '@ghostfolio/api/models/interfaces/user-settings.interface';
|
|
import { RuleSettings } from '@ghostfolio/api/models/interfaces/rule-settings.interface';
|
|
|
|
export class CurrencyClusterRiskInitialInvestment extends Rule<Settings> {
|
|
public constructor(public exchangeRateDataService: ExchangeRateDataService) {
|
|
super(exchangeRateDataService, {
|
|
name: 'Initial Investment'
|
|
});
|
|
}
|
|
|
|
public evaluate(
|
|
aPositions: { [symbol: string]: PortfolioPosition },
|
|
aFees: number,
|
|
ruleSettings: Settings
|
|
) {
|
|
const positionsGroupedByCurrency = this.groupPositionsByAttribute(
|
|
aPositions,
|
|
'currency',
|
|
ruleSettings.baseCurrency
|
|
);
|
|
|
|
let maxItem = positionsGroupedByCurrency[0];
|
|
let totalInvestment = 0;
|
|
|
|
positionsGroupedByCurrency.forEach((groupItem) => {
|
|
// Calculate total investment
|
|
totalInvestment += groupItem.investment;
|
|
|
|
// Find maximum
|
|
if (groupItem.investment > maxItem.investment) {
|
|
maxItem = groupItem;
|
|
}
|
|
});
|
|
|
|
const maxInvestmentRatio = maxItem.investment / totalInvestment;
|
|
|
|
if (maxInvestmentRatio > ruleSettings.threshold) {
|
|
return {
|
|
evaluation: `Over ${
|
|
ruleSettings.threshold * 100
|
|
}% of your initial investment is in ${maxItem.groupKey} (${(
|
|
maxInvestmentRatio * 100
|
|
).toPrecision(3)}%)`,
|
|
value: false
|
|
};
|
|
}
|
|
|
|
return {
|
|
evaluation: `The major part of your initial investment is in ${
|
|
maxItem.groupKey
|
|
} (${(maxInvestmentRatio * 100).toPrecision(3)}%) and does not exceed ${
|
|
ruleSettings.threshold * 100
|
|
}%`,
|
|
value: true
|
|
};
|
|
}
|
|
|
|
public getSettings(aUserSettings: UserSettings): Settings {
|
|
return {
|
|
baseCurrency: aUserSettings.baseCurrency,
|
|
isActive: true,
|
|
threshold: 0.5
|
|
};
|
|
}
|
|
}
|
|
|
|
interface Settings extends RuleSettings {
|
|
baseCurrency: Currency;
|
|
threshold: number;
|
|
}
|