Skip to content

Open Source Wealth Management Software. Angular + NestJS + Prisma + Nx + TypeScript 🤍

Fix performance percentage for 1d (#2814)

Thomas Kaul23d323073d9f
diff --git a/CHANGELOG.md b/CHANGELOG.md
index bb13ba0723..35560bf959 100644
--- a/CHANGELOG.md
+++ b/CHANGELOG.md
@@ -18,6 +18,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
### Fixed
+- Improved the time-weighted performance calculation for `1D`
- Improved the tabs on iOS (_Add to Home Screen_)
## 2.33.0 - 2023-12-31
diff --git a/apps/api/src/app/portfolio/portfolio-calculator.ts b/apps/api/src/app/portfolio/portfolio-calculator.ts
index 06a5ca6ae6..71ddbb6ddb 100644
--- a/apps/api/src/app/portfolio/portfolio-calculator.ts
+++ b/apps/api/src/app/portfolio/portfolio-calculator.ts
@@ -1246,7 +1246,7 @@ export class PortfolioCalculator {
if (i > indexOfStartOrder) {
// Only consider periods with an investment for the calculation of
// the time weighted investment
- if (totalInvestmentBeforeTransaction.gt(0)) {
+ if (valueOfInvestmentBeforeTransaction.gt(0)) {
// Calculate the number of days since the previous order
const orderDate = new Date(order.date);
const previousOrderDate = new Date(orders[i - 1].date);
@@ -1267,7 +1267,10 @@ export class PortfolioCalculator {
totalInvestmentDays += daysSinceLastOrder;
sumOfTimeWeightedInvestments = sumOfTimeWeightedInvestments.add(
- totalInvestmentBeforeTransaction.mul(daysSinceLastOrder)
+ valueAtStartDate
+ .minus(investmentAtStartDate)
+ .plus(totalInvestmentBeforeTransaction)
+ .mul(daysSinceLastOrder)
);
}
diff --git a/apps/api/src/app/portfolio/portfolio.service.ts b/apps/api/src/app/portfolio/portfolio.service.ts
index 050559c859..cd4fa0bae2 100644
--- a/apps/api/src/app/portfolio/portfolio.service.ts
+++ b/apps/api/src/app/portfolio/portfolio.service.ts
@@ -1035,25 +1035,44 @@ export class PortfolioService {
return {
hasErrors: currentPositions.hasErrors,
- positions: positions.map((position) => {
- return {
- ...position,
- assetClass: symbolProfileMap[position.symbol].assetClass,
- assetSubClass: symbolProfileMap[position.symbol].assetSubClass,
- averagePrice: new Big(position.averagePrice).toNumber(),
- grossPerformance: position.grossPerformance?.toNumber() ?? null,
- grossPerformancePercentage:
- position.grossPerformancePercentage?.toNumber() ?? null,
- investment: new Big(position.investment).toNumber(),
- marketState:
- dataProviderResponses[position.symbol]?.marketState ?? 'delayed',
- name: symbolProfileMap[position.symbol].name,
- netPerformance: position.netPerformance?.toNumber() ?? null,
- netPerformancePercentage:
- position.netPerformancePercentage?.toNumber() ?? null,
- quantity: new Big(position.quantity).toNumber()
- };
- })
+ positions: positions.map(
+ ({
+ averagePrice,
+ currency,
+ dataSource,
+ firstBuyDate,
+ investment,
+ grossPerformance,
+ grossPerformancePercentage,
+ netPerformance,
+ netPerformancePercentage,
+ quantity,
+ symbol,
+ transactionCount
+ }) => {
+ return {
+ currency,
+ dataSource,
+ firstBuyDate,
+ symbol,
+ transactionCount,
+ assetClass: symbolProfileMap[symbol].assetClass,
+ assetSubClass: symbolProfileMap[symbol].assetSubClass,
+ averagePrice: averagePrice.toNumber(),
+ grossPerformance: grossPerformance?.toNumber() ?? null,
+ grossPerformancePercentage:
+ grossPerformancePercentage?.toNumber() ?? null,
+ investment: investment.toNumber(),
+ marketState:
+ dataProviderResponses[symbol]?.marketState ?? 'delayed',
+ name: symbolProfileMap[symbol].name,
+ netPerformance: netPerformance?.toNumber() ?? null,
+ netPerformancePercentage:
+ netPerformancePercentage?.toNumber() ?? null,
+ quantity: quantity.toNumber()
+ };
+ }
+ )
};
}