diff --git a/apps/api/src/app/portfolio/portfolio-calculator-novn-buy-and-sell-partially.spec.ts b/apps/api/src/app/portfolio/portfolio-calculator-novn-buy-and-sell-partially.spec.ts index fffba0638..d215f9e1e 100644 --- a/apps/api/src/app/portfolio/portfolio-calculator-novn-buy-and-sell-partially.spec.ts +++ b/apps/api/src/app/portfolio/portfolio-calculator-novn-buy-and-sell-partially.spec.ts @@ -80,7 +80,7 @@ describe('PortfolioCalculator', () => { firstBuyDate: '2022-03-07', grossPerformance: new Big('21.93'), grossPerformancePercentage: new Big('0.14465699208443271768'), - investment: new Big('65.87'), + investment: new Big('75.80'), netPerformance: new Big('17.68'), netPerformancePercentage: new Big('0.11662269129287598945'), marketPrice: 87.8, @@ -89,7 +89,7 @@ describe('PortfolioCalculator', () => { transactionCount: 2 } ], - totalInvestment: new Big('65.87') + totalInvestment: new Big('75.80') }); }); }); diff --git a/apps/api/src/app/portfolio/portfolio-calculator.ts b/apps/api/src/app/portfolio/portfolio-calculator.ts index 20b0a8709..4f8631a3f 100644 --- a/apps/api/src/app/portfolio/portfolio-calculator.ts +++ b/apps/api/src/app/portfolio/portfolio-calculator.ts @@ -77,17 +77,30 @@ export class PortfolioCalculator { const newQuantity = order.quantity .mul(factor) .plus(oldAccumulatedSymbol.quantity); + + let investment = new Big(0); + + 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) + ); + } + } + currentTransactionPointItem = { + investment, currency: order.currency, dataSource: order.dataSource, fee: order.fee.plus(oldAccumulatedSymbol.fee), firstBuyDate: oldAccumulatedSymbol.firstBuyDate, - investment: newQuantity.eq(0) - ? new Big(0) - : unitPrice - .mul(order.quantity) - .mul(factor) - .plus(oldAccumulatedSymbol.investment), quantity: newQuantity, symbol: order.symbol, transactionCount: oldAccumulatedSymbol.transactionCount + 1 diff --git a/libs/ui/src/lib/activities-table/activities-table.component.html b/libs/ui/src/lib/activities-table/activities-table.component.html index 7cbfffe05..388a7e420 100644 --- a/libs/ui/src/lib/activities-table/activities-table.component.html +++ b/libs/ui/src/lib/activities-table/activities-table.component.html @@ -268,16 +268,11 @@ > -