Browse Source

Fix average price for short positions

pull/5416/head
Marcin Szymański 4 months ago
committed by Thomas Kaul
parent
commit
fe0c56fe2d
  1. 16
      apps/api/src/app/portfolio/calculator/portfolio-calculator.ts

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

@ -926,14 +926,22 @@ export abstract class PortfolioCalculator {
.mul(factor)
.plus(oldAccumulatedSymbol.quantity);
if (type === 'BUY') {
if (type === 'BUY' && oldAccumulatedSymbol.investment.gte(0)) {
investment = oldAccumulatedSymbol.investment.plus(
quantity.mul(unitPrice)
);
} else if (type === 'SELL') {
} else if (type === 'BUY' && oldAccumulatedSymbol.investment.lt(0)) {
investment = oldAccumulatedSymbol.investment.plus(
quantity.mul(oldAccumulatedSymbol.averagePrice)
);
} else if (type === 'SELL' && oldAccumulatedSymbol.investment.gt(0)) {
investment = oldAccumulatedSymbol.investment.minus(
quantity.mul(oldAccumulatedSymbol.averagePrice)
);
} else if (type === 'SELL' && oldAccumulatedSymbol.investment.lte(0)) {
investment = oldAccumulatedSymbol.investment.minus(
quantity.mul(unitPrice)
);
}
currentTransactionPointItem = {
@ -942,8 +950,8 @@ export abstract class PortfolioCalculator {
investment,
skipErrors,
symbol,
averagePrice: newQuantity.gt(0)
? investment.div(newQuantity)
averagePrice: !newQuantity.eq(0)
? investment.div(newQuantity).abs()
: new Big(0),
dividend: new Big(0),
fee: oldAccumulatedSymbol.fee.plus(fee),

Loading…
Cancel
Save