Browse Source

fix(api): add logic for selling all units

pull/5509/head
KenTandrian 1 week ago
parent
commit
d0b26963aa
  1. 10
      apps/api/src/app/portfolio/calculator/portfolio-calculator.ts

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

@ -937,11 +937,19 @@ export abstract class PortfolioCalculator {
);
}
} else if (type === 'SELL') {
if (oldAccumulatedSymbol.investment.gt(0)) {
if (
oldAccumulatedSymbol.quantity.gt(0) &&
oldAccumulatedSymbol.quantity.eq(quantity)
) {
// Selling all units, so investment should be 0
investment = new Big(0);
} else if (oldAccumulatedSymbol.investment.gt(0)) {
// Selling part of a positive investment
investment = oldAccumulatedSymbol.investment.minus(
quantity.mul(oldAccumulatedSymbol.averagePrice)
);
} else {
// Selling part of a negative investment (short sell)
investment = oldAccumulatedSymbol.investment.minus(
quantity.mul(unitPrice)
);

Loading…
Cancel
Save