From 1cf3aae6597d7dcac7cc13c61fc22110d7f0c3a3 Mon Sep 17 00:00:00 2001 From: Thomas Kaul <4159106+dtslvr@users.noreply.github.com> Date: Sun, 3 Mar 2024 21:40:11 +0100 Subject: [PATCH] Fix average price --- apps/api/jest.config.ts | 1 - .../transaction-point-symbol.interface.ts | 1 + .../src/app/portfolio/portfolio-calculator.ts | 38 ++++++++++--------- .../src/app/portfolio/portfolio.service.ts | 5 +-- 4 files changed, 23 insertions(+), 22 deletions(-) diff --git a/apps/api/jest.config.ts b/apps/api/jest.config.ts index 8152c3f2a..b87f91a79 100644 --- a/apps/api/jest.config.ts +++ b/apps/api/jest.config.ts @@ -13,7 +13,6 @@ export default { }, moduleFileExtensions: ['ts', 'js', 'html'], coverageDirectory: '../../coverage/apps/api', - testTimeout: 10000, testEnvironment: 'node', preset: '../../jest.preset.js' }; diff --git a/apps/api/src/app/portfolio/interfaces/transaction-point-symbol.interface.ts b/apps/api/src/app/portfolio/interfaces/transaction-point-symbol.interface.ts index 5f8e64406..fb2f0a389 100644 --- a/apps/api/src/app/portfolio/interfaces/transaction-point-symbol.interface.ts +++ b/apps/api/src/app/portfolio/interfaces/transaction-point-symbol.interface.ts @@ -2,6 +2,7 @@ import { DataSource, Tag } from '@prisma/client'; import Big from 'big.js'; export interface TransactionPointSymbol { + averagePrice: Big; currency: string; dataSource: DataSource; dividend: Big; diff --git a/apps/api/src/app/portfolio/portfolio-calculator.ts b/apps/api/src/app/portfolio/portfolio-calculator.ts index 2bad796d9..966ebebdf 100644 --- a/apps/api/src/app/portfolio/portfolio-calculator.ts +++ b/apps/api/src/app/portfolio/portfolio-calculator.ts @@ -78,32 +78,29 @@ export class PortfolioCalculator { const oldAccumulatedSymbol = symbols[order.symbol]; const factor = getFactor(order.type); - const unitPrice = new Big(order.unitPrice); if (oldAccumulatedSymbol) { + let investment = oldAccumulatedSymbol.investment; + const newQuantity = order.quantity .mul(factor) .plus(oldAccumulatedSymbol.quantity); - let investment = oldAccumulatedSymbol.investment; - - if (newQuantity.gt(0)) { - if (order.type === 'BUY') { - investment = oldAccumulatedSymbol.investment.plus( - order.quantity.mul(unitPrice) - ); - } else if (order.type === 'SELL') { - const averagePrice = oldAccumulatedSymbol.investment.div( - oldAccumulatedSymbol.quantity - ); - investment = oldAccumulatedSymbol.investment.minus( - order.quantity.mul(averagePrice) - ); - } + if (order.type === 'BUY') { + investment = oldAccumulatedSymbol.investment.plus( + order.quantity.mul(order.unitPrice) + ); + } else if (order.type === 'SELL') { + investment = oldAccumulatedSymbol.investment.minus( + order.quantity.mul(oldAccumulatedSymbol.averagePrice) + ); } currentTransactionPointItem = { investment, + averagePrice: newQuantity.gt(0) + ? investment.div(newQuantity) + : new Big(0), currency: order.currency, dataSource: order.dataSource, dividend: new Big(0), @@ -116,12 +113,13 @@ export class PortfolioCalculator { }; } else { currentTransactionPointItem = { + averagePrice: order.unitPrice, currency: order.currency, dataSource: order.dataSource, dividend: new Big(0), fee: order.fee, firstBuyDate: order.date, - investment: unitPrice.mul(order.quantity).mul(factor), + investment: order.unitPrice.mul(order.quantity).mul(factor), quantity: order.quantity.mul(factor), symbol: order.symbol, tags: order.tags, @@ -132,22 +130,28 @@ export class PortfolioCalculator { symbols[order.symbol] = currentTransactionPointItem; const items = lastTransactionPoint?.items ?? []; + const newItems = items.filter( (transactionPointItem) => transactionPointItem.symbol !== order.symbol ); + newItems.push(currentTransactionPointItem); + newItems.sort((a, b) => { return a.symbol?.localeCompare(b.symbol); }); + if (lastDate !== currentDate || lastTransactionPoint === null) { lastTransactionPoint = { date: currentDate, items: newItems }; + this.transactionPoints.push(lastTransactionPoint); } else { lastTransactionPoint.items = newItems; } + lastDate = currentDate; } } diff --git a/apps/api/src/app/portfolio/portfolio.service.ts b/apps/api/src/app/portfolio/portfolio.service.ts index 6f63916e1..d55b3d647 100644 --- a/apps/api/src/app/portfolio/portfolio.service.ts +++ b/apps/api/src/app/portfolio/portfolio.service.ts @@ -753,7 +753,6 @@ export class PortfolioService { averagePrice, currency, dataSource, - dividend, dividendInBaseCurrency, fee, firstBuyDate, @@ -804,9 +803,7 @@ export class PortfolioService { ); if (currentSymbol) { - currentAveragePrice = currentSymbol.quantity.eq(0) - ? 0 - : currentSymbol.investment.div(currentSymbol.quantity).toNumber(); + currentAveragePrice = currentSymbol.averagePrice.toNumber(); currentQuantity = currentSymbol.quantity.toNumber(); }