Browse Source

Feature/refactor portfolio calculator (#1803)

* Refactor chart calculation in portfolio calculator

* Update changelog
pull/1805/head
Thomas Kaul 2 years ago
committed by GitHub
parent
commit
0c85380dbf
No known key found for this signature in database GPG Key ID: 4AEE18F83AFDEB23
  1. 1
      CHANGELOG.md
  2. 126
      apps/api/src/app/portfolio/portfolio-calculator.ts

1
CHANGELOG.md

@ -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

126
apps/api/src/app/portfolio/portfolio-calculator.ts

@ -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;
const investmentValuesBySymbol: { totalInvestmentValue: Big;
[symbol: string]: { [date: string]: Big }; totalNetPerformanceValue: Big;
} = {}; };
const maxInvestmentValuesBySymbol: {
[symbol: string]: { [date: string]: Big };
} = {}; } = {};
const netPerformanceValuesBySymbol: { 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 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);
const currentValue =
if (currentValuesBySymbol[symbol]?.[dateString]) { symbolValues.currentValues?.[dateString] ?? new Big(0);
totalCurrentValues[dateString] = totalCurrentValues[dateString].add( const investmentValue =
currentValuesBySymbol[symbol][dateString] symbolValues.investmentValues?.[dateString] ?? new Big(0);
); const maxInvestmentValue =
} symbolValues.maxInvestmentValues?.[dateString] ?? new Big(0);
const netPerformanceValue =
totalNetPerformanceValues[dateString] = symbolValues.netPerformanceValues?.[dateString] ?? new Big(0);
totalNetPerformanceValues[dateString] ?? new Big(0);
valuesByDate[dateString] = {
if (netPerformanceValuesBySymbol[symbol]?.[dateString]) { totalCurrentValue: (
totalNetPerformanceValues[dateString] = totalNetPerformanceValues[ valuesByDate[dateString]?.totalCurrentValue ?? new Big(0)
dateString ).add(currentValue),
].add(netPerformanceValuesBySymbol[symbol][dateString]); totalInvestmentValue: (
valuesByDate[dateString]?.totalInvestmentValue ?? new Big(0)
).add(investmentValue),
maxTotalInvestmentValue: (
valuesByDate[dateString]?.maxTotalInvestmentValue ?? new Big(0)
).add(maxInvestmentValue),
totalNetPerformanceValue: (
valuesByDate[dateString]?.totalNetPerformanceValue ?? new Big(0)
).add(netPerformanceValue)
};
} }
totalInvestmentValues[dateString] =
totalInvestmentValues[dateString] ?? new Big(0);
if (investmentValuesBySymbol[symbol]?.[dateString]) {
totalInvestmentValues[dateString] = totalInvestmentValues[
dateString
].add(investmentValuesBySymbol[symbol][dateString]);
} }
maxTotalInvestmentValues[dateString] = return Object.entries(valuesByDate).map(([date, values]) => {
maxTotalInvestmentValues[dateString] ?? new Big(0); const {
maxTotalInvestmentValue,
if (maxInvestmentValuesBySymbol[symbol]?.[dateString]) { totalCurrentValue,
maxTotalInvestmentValues[dateString] = maxTotalInvestmentValues[ totalInvestmentValue,
dateString totalNetPerformanceValue
].add(maxInvestmentValuesBySymbol[symbol][dateString]); } = values;
}
}
}
return Object.keys(totalNetPerformanceValues).map((date) => { const netPerformanceInPercentage = maxTotalInvestmentValue.eq(0)
const netPerformanceInPercentage = maxTotalInvestmentValues[date].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()
}; };
}); });
} }

Loading…
Cancel
Save