mirror of https://github.com/ghostfolio/ghostfolio
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.
64 lines
1.7 KiB
64 lines
1.7 KiB
import { PortfolioPosition } from '@ghostfolio/api/app/portfolio/interfaces/portfolio-position.interface';
|
|
import { ExchangeRateDataService } from 'apps/api/src/services/exchange-rate-data.service';
|
|
|
|
import { Rule } from '../../rule';
|
|
|
|
export class CurrencyClusterRiskCurrentInvestment extends Rule {
|
|
public constructor(public exchangeRateDataService: ExchangeRateDataService) {
|
|
super(exchangeRateDataService, {
|
|
name: 'Current Investment'
|
|
});
|
|
}
|
|
|
|
public evaluate(
|
|
aPositions: { [symbol: string]: PortfolioPosition },
|
|
aFees: number,
|
|
aRuleSettingsMap?: {
|
|
[key: string]: any;
|
|
}
|
|
) {
|
|
const ruleSettings =
|
|
aRuleSettingsMap[CurrencyClusterRiskCurrentInvestment.name];
|
|
|
|
const positionsGroupedByCurrency = this.groupPositionsByAttribute(
|
|
aPositions,
|
|
'currency',
|
|
ruleSettings.baseCurrency
|
|
);
|
|
|
|
let maxItem = positionsGroupedByCurrency[0];
|
|
let totalValue = 0;
|
|
|
|
positionsGroupedByCurrency.forEach((groupItem) => {
|
|
// Calculate total value
|
|
totalValue += groupItem.value;
|
|
|
|
// Find maximum
|
|
if (groupItem.value > maxItem.value) {
|
|
maxItem = groupItem;
|
|
}
|
|
});
|
|
|
|
const maxValueRatio = maxItem.value / totalValue;
|
|
|
|
if (maxValueRatio > ruleSettings.threshold) {
|
|
return {
|
|
evaluation: `Over ${
|
|
ruleSettings.threshold * 100
|
|
}% of your current investment is in ${maxItem.groupKey} (${(
|
|
maxValueRatio * 100
|
|
).toPrecision(3)}%)`,
|
|
value: false
|
|
};
|
|
}
|
|
|
|
return {
|
|
evaluation: `The major part of your current investment is in ${
|
|
maxItem.groupKey
|
|
} (${(maxValueRatio * 100).toPrecision(3)}%) and does not exceed ${
|
|
ruleSettings.threshold * 100
|
|
}%`,
|
|
value: true
|
|
};
|
|
}
|
|
}
|
|
|