Feature/refactor portfolio calculator (#1803)

* Refactor chart calculation in portfolio calculator

* Update changelog
This commit is contained in:
Thomas Kaul 2023-03-25 12:20:42 +01:00 committed by GitHub
parent fb576376dc
commit 0c85380dbf
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 61 additions and 64 deletions

View File

@ -9,6 +9,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
### Changed ### Changed
- Refactored the calculation of the chart
- Upgraded `ng-extract-i18n-merge` from version `2.5.0` to `2.6.0` - Upgraded `ng-extract-i18n-merge` from version `2.5.0` to `2.6.0`
## 1.247.0 - 2023-03-23 ## 1.247.0 - 2023-03-23

View File

@ -182,10 +182,10 @@ export class PortfolioCalculator {
return isBefore(parseDate(transactionPoint.date), end); return isBefore(parseDate(transactionPoint.date), end);
}) ?? []; }) ?? [];
const firstIndex = transactionPointsBeforeEndDate.length; const currencies: { [symbol: string]: string } = {};
const dates: Date[] = []; const dates: Date[] = [];
const dataGatheringItems: IDataGatheringItem[] = []; const dataGatheringItems: IDataGatheringItem[] = [];
const currencies: { [symbol: string]: string } = {}; const firstIndex = transactionPointsBeforeEndDate.length;
let day = start; let day = start;
@ -235,27 +235,24 @@ export class PortfolioCalculator {
} }
} }
const currentValuesBySymbol: { const valuesByDate: {
[symbol: string]: { [date: string]: Big }; [date: string]: {
maxTotalInvestmentValue: Big;
totalCurrentValue: Big;
totalInvestmentValue: Big;
totalNetPerformanceValue: Big;
};
} = {}; } = {};
const investmentValuesBySymbol: { const valuesBySymbol: {
[symbol: string]: { [date: string]: Big }; [symbol: string]: {
currentValues: { [date: string]: Big };
investmentValues: { [date: string]: Big };
maxInvestmentValues: { [date: string]: Big };
netPerformanceValues: { [date: string]: Big };
};
} = {}; } = {};
const maxInvestmentValuesBySymbol: {
[symbol: string]: { [date: string]: Big };
} = {};
const netPerformanceValuesBySymbol: {
[symbol: string]: { [date: string]: Big };
} = {};
const totalCurrentValues: { [date: string]: Big } = {};
const totalNetPerformanceValues: { [date: string]: Big } = {};
const totalInvestmentValues: { [date: string]: Big } = {};
const maxTotalInvestmentValues: { [date: string]: Big } = {};
for (const symbol of Object.keys(symbols)) { for (const symbol of Object.keys(symbols)) {
const { const {
currentValues, currentValues,
@ -271,68 +268,67 @@ export class PortfolioCalculator {
isChartMode: true isChartMode: true
}); });
currentValuesBySymbol[symbol] = currentValues; valuesBySymbol[symbol] = {
netPerformanceValuesBySymbol[symbol] = netPerformanceValues; currentValues,
investmentValuesBySymbol[symbol] = investmentValues; investmentValues,
maxInvestmentValuesBySymbol[symbol] = maxInvestmentValues; maxInvestmentValues,
netPerformanceValues
};
} }
for (const currentDate of dates) { for (const currentDate of dates) {
const dateString = format(currentDate, DATE_FORMAT); const dateString = format(currentDate, DATE_FORMAT);
for (const symbol of Object.keys(netPerformanceValuesBySymbol)) { for (const symbol of Object.keys(valuesBySymbol)) {
totalCurrentValues[dateString] = const symbolValues = valuesBySymbol[symbol];
totalCurrentValues[dateString] ?? new Big(0);
if (currentValuesBySymbol[symbol]?.[dateString]) { const currentValue =
totalCurrentValues[dateString] = totalCurrentValues[dateString].add( symbolValues.currentValues?.[dateString] ?? new Big(0);
currentValuesBySymbol[symbol][dateString] const investmentValue =
); symbolValues.investmentValues?.[dateString] ?? new Big(0);
} const maxInvestmentValue =
symbolValues.maxInvestmentValues?.[dateString] ?? new Big(0);
const netPerformanceValue =
symbolValues.netPerformanceValues?.[dateString] ?? new Big(0);
totalNetPerformanceValues[dateString] = valuesByDate[dateString] = {
totalNetPerformanceValues[dateString] ?? new Big(0); totalCurrentValue: (
valuesByDate[dateString]?.totalCurrentValue ?? new Big(0)
if (netPerformanceValuesBySymbol[symbol]?.[dateString]) { ).add(currentValue),
totalNetPerformanceValues[dateString] = totalNetPerformanceValues[ totalInvestmentValue: (
dateString valuesByDate[dateString]?.totalInvestmentValue ?? new Big(0)
].add(netPerformanceValuesBySymbol[symbol][dateString]); ).add(investmentValue),
} maxTotalInvestmentValue: (
valuesByDate[dateString]?.maxTotalInvestmentValue ?? new Big(0)
totalInvestmentValues[dateString] = ).add(maxInvestmentValue),
totalInvestmentValues[dateString] ?? new Big(0); totalNetPerformanceValue: (
valuesByDate[dateString]?.totalNetPerformanceValue ?? new Big(0)
if (investmentValuesBySymbol[symbol]?.[dateString]) { ).add(netPerformanceValue)
totalInvestmentValues[dateString] = totalInvestmentValues[ };
dateString
].add(investmentValuesBySymbol[symbol][dateString]);
}
maxTotalInvestmentValues[dateString] =
maxTotalInvestmentValues[dateString] ?? new Big(0);
if (maxInvestmentValuesBySymbol[symbol]?.[dateString]) {
maxTotalInvestmentValues[dateString] = maxTotalInvestmentValues[
dateString
].add(maxInvestmentValuesBySymbol[symbol][dateString]);
}
} }
} }
return Object.keys(totalNetPerformanceValues).map((date) => { return Object.entries(valuesByDate).map(([date, values]) => {
const netPerformanceInPercentage = maxTotalInvestmentValues[date].eq(0) const {
maxTotalInvestmentValue,
totalCurrentValue,
totalInvestmentValue,
totalNetPerformanceValue
} = values;
const netPerformanceInPercentage = maxTotalInvestmentValue.eq(0)
? 0 ? 0
: totalNetPerformanceValues[date] : totalNetPerformanceValue
.div(maxTotalInvestmentValues[date]) .div(maxTotalInvestmentValue)
.mul(100) .mul(100)
.toNumber(); .toNumber();
return { return {
date, date,
netPerformanceInPercentage, netPerformanceInPercentage,
netPerformance: totalNetPerformanceValues[date].toNumber(), netPerformance: totalNetPerformanceValue.toNumber(),
totalInvestment: totalInvestmentValues[date].toNumber(), totalInvestment: totalInvestmentValue.toNumber(),
value: totalCurrentValues[date].toNumber() value: totalCurrentValue.toNumber()
}; };
}); });
} }