From f0d173a03f35668d1df205a5cf1ab2a8224001cb Mon Sep 17 00:00:00 2001 From: Thomas <4159106+dtslvr@users.noreply.github.com> Date: Thu, 3 Nov 2022 19:31:18 +0100 Subject: [PATCH] Fix test --- ...folio-calculator-novn-buy-and-sell.spec.ts | 8 ++-- .../src/app/portfolio/portfolio-calculator.ts | 45 +++++++++++++------ 2 files changed, 35 insertions(+), 18 deletions(-) diff --git a/apps/api/src/app/portfolio/portfolio-calculator-novn-buy-and-sell.spec.ts b/apps/api/src/app/portfolio/portfolio-calculator-novn-buy-and-sell.spec.ts index cede95aa0..a27eba341 100644 --- a/apps/api/src/app/portfolio/portfolio-calculator-novn-buy-and-sell.spec.ts +++ b/apps/api/src/app/portfolio/portfolio-calculator-novn-buy-and-sell.spec.ts @@ -94,8 +94,8 @@ describe('PortfolioCalculator', () => { grossPerformance: new Big('19.86'), grossPerformancePercentage: new Big('0.13100263852242744063'), hasErrors: false, - netPerformance: new Big('15.61'), - netPerformancePercentage: new Big('0.1029683377308707124'), + netPerformance: new Big('19.86'), + netPerformancePercentage: new Big('0.13100263852242744063'), positions: [ { averagePrice: new Big('0'), @@ -105,8 +105,8 @@ describe('PortfolioCalculator', () => { grossPerformance: new Big('19.86'), grossPerformancePercentage: new Big('0.13100263852242744063'), investment: new Big('0'), - netPerformance: new Big('15.61'), - netPerformancePercentage: new Big('0.1029683377308707124'), + netPerformance: new Big('19.86'), + netPerformancePercentage: new Big('0.13100263852242744063'), marketPrice: 87.8, quantity: new Big('0'), symbol: 'NOVN.SW', diff --git a/apps/api/src/app/portfolio/portfolio-calculator.ts b/apps/api/src/app/portfolio/portfolio-calculator.ts index 2aae81031..acf612aa5 100644 --- a/apps/api/src/app/portfolio/portfolio-calculator.ts +++ b/apps/api/src/app/portfolio/portfolio-calculator.ts @@ -40,7 +40,7 @@ export class PortfolioCalculator { private static readonly CALCULATE_PERCENTAGE_PERFORMANCE_WITH_MAX_INVESTMENT = true; - private static readonly ENABLE_LOGGING = true; + private static readonly ENABLE_LOGGING = false; private currency: string; private currentRateService: CurrentRateService; @@ -234,21 +234,28 @@ export class PortfolioCalculator { [symbol: string]: { [date: string]: Big }; } = {}; + const maxInvestmentValuesBySymbol: { + [symbol: string]: { [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)) { - const { netPerformanceValues, investmentValues } = this.getSymbolMetrics({ - end, - marketSymbolMap, - start, - step, - symbol, - isChartMode: true - }); + const { investmentValues, maxInvestmentValues, netPerformanceValues } = + this.getSymbolMetrics({ + end, + marketSymbolMap, + start, + step, + symbol, + isChartMode: true + }); netPerformanceValuesBySymbol[symbol] = netPerformanceValues; investmentValuesBySymbol[symbol] = investmentValues; + maxInvestmentValuesBySymbol[symbol] = maxInvestmentValues; } for (const currentDate of dates) { @@ -267,19 +274,28 @@ export class PortfolioCalculator { totalInvestmentValues[dateString] = totalInvestmentValues[dateString] ?? new Big(0); + maxTotalInvestmentValues[dateString] = + maxTotalInvestmentValues[dateString] ?? new Big(0); + if (investmentValuesBySymbol[symbol]?.[dateString]) { totalInvestmentValues[dateString] = totalInvestmentValues[ dateString ].add(investmentValuesBySymbol[symbol][dateString]); } + + if (maxInvestmentValuesBySymbol[symbol]?.[dateString]) { + maxTotalInvestmentValues[dateString] = maxTotalInvestmentValues[ + dateString + ].add(maxInvestmentValuesBySymbol[symbol][dateString]); + } } } return Object.keys(totalNetPerformanceValues).map((date) => { - const netPerformanceInPercentage = totalInvestmentValues[date].eq(0) + const netPerformanceInPercentage = maxTotalInvestmentValues[date].eq(0) ? 0 : totalNetPerformanceValues[date] - .div(totalInvestmentValues[date]) + .div(maxTotalInvestmentValues[date]) .mul(100) .toNumber(); @@ -287,9 +303,7 @@ export class PortfolioCalculator { date, netPerformanceInPercentage, netPerformance: totalNetPerformanceValues[date].toNumber(), - // TODO totalInvestment: totalInvestmentValues[date].toNumber(), - // TODO value: totalInvestmentValues[date] .plus(totalNetPerformanceValues[date]) .toNumber() @@ -901,6 +915,7 @@ export class PortfolioCalculator { let initialValue: Big; let investmentAtStartDate: Big; const investmentValues: { [date: string]: Big } = {}; + const maxInvestmentValues: { [date: string]: Big } = {}; let lastAveragePrice = new Big(0); // let lastTransactionInvestment = new Big(0); // let lastValueOfInvestmentBeforeTransaction = new Big(0); @@ -1172,7 +1187,8 @@ export class PortfolioCalculator { .minus(grossPerformanceAtStartDate) .minus(fees.minus(feesAtStartDate)); - investmentValues[order.date] = maxTotalInvestment; + investmentValues[order.date] = totalInvestment; + maxInvestmentValues[order.date] = maxTotalInvestment; } if (PortfolioCalculator.ENABLE_LOGGING) { @@ -1274,6 +1290,7 @@ export class PortfolioCalculator { initialValue, grossPerformancePercentage, investmentValues, + maxInvestmentValues, netPerformancePercentage, netPerformanceValues, hasErrors: totalUnits.gt(0) && (!initialValue || !unitPriceAtEndDate),