Larry Sinclair 1 day ago
committed by GitHub
parent
commit
93e7431633
No known key found for this signature in database GPG Key ID: B5690EEEBB952194
  1. 114
      apps/api/src/app/portfolio/calculator/portfolio-calculator.ts

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

@ -910,65 +910,72 @@ export abstract class PortfolioCalculator {
type, type,
unitPrice unitPrice
} of this.activities) { } of this.activities) {
const factor = getFactor(type);
// Only create transaction point symbols for investment activities
let currentTransactionPointItem: TransactionPointSymbol; let currentTransactionPointItem: TransactionPointSymbol;
const oldAccumulatedSymbol = symbols[SymbolProfile.symbol]; const shouldCreateSymbol = ['BUY', 'SELL', 'DIVIDEND'].includes(
type as string
);
const factor = getFactor(type); if (shouldCreateSymbol) {
const oldAccumulatedSymbol = symbols[SymbolProfile.symbol];
if (oldAccumulatedSymbol) { if (oldAccumulatedSymbol) {
let investment = oldAccumulatedSymbol.investment; let investment = oldAccumulatedSymbol.investment;
const newQuantity = quantity const newQuantity = quantity
.mul(factor) .mul(factor)
.plus(oldAccumulatedSymbol.quantity); .plus(oldAccumulatedSymbol.quantity);
if (type === 'BUY') { if (type === 'BUY') {
investment = oldAccumulatedSymbol.investment.plus( investment = oldAccumulatedSymbol.investment.plus(
quantity.mul(unitPrice) quantity.mul(unitPrice)
); );
} else if (type === 'SELL') { } else if (type === 'SELL') {
investment = oldAccumulatedSymbol.investment.minus( investment = oldAccumulatedSymbol.investment.minus(
quantity.mul(oldAccumulatedSymbol.averagePrice) quantity.mul(oldAccumulatedSymbol.averagePrice)
); );
} }
currentTransactionPointItem = { currentTransactionPointItem = {
investment, investment,
averagePrice: newQuantity.gt(0) averagePrice: newQuantity.gt(0)
? investment.div(newQuantity) ? investment.div(newQuantity)
: new Big(0), : new Big(0),
currency: SymbolProfile.currency, currency: SymbolProfile.currency,
dataSource: SymbolProfile.dataSource, dataSource: SymbolProfile.dataSource,
dividend: new Big(0), dividend: new Big(0),
fee: oldAccumulatedSymbol.fee.plus(fee), fee: oldAccumulatedSymbol.fee.plus(fee),
firstBuyDate: oldAccumulatedSymbol.firstBuyDate, firstBuyDate: oldAccumulatedSymbol.firstBuyDate,
quantity: newQuantity, quantity: newQuantity,
symbol: SymbolProfile.symbol, symbol: SymbolProfile.symbol,
tags: oldAccumulatedSymbol.tags.concat(tags), tags: oldAccumulatedSymbol.tags.concat(tags),
transactionCount: oldAccumulatedSymbol.transactionCount + 1 transactionCount: oldAccumulatedSymbol.transactionCount + 1
}; };
} else { } else {
currentTransactionPointItem = { currentTransactionPointItem = {
fee, fee,
tags, tags,
averagePrice: unitPrice, averagePrice: unitPrice,
currency: SymbolProfile.currency, currency: SymbolProfile.currency,
dataSource: SymbolProfile.dataSource, dataSource: SymbolProfile.dataSource,
dividend: new Big(0), dividend: new Big(0),
firstBuyDate: date, firstBuyDate: date,
investment: unitPrice.mul(quantity).mul(factor), investment: unitPrice.mul(quantity).mul(factor),
quantity: quantity.mul(factor), quantity: quantity.mul(factor),
symbol: SymbolProfile.symbol, symbol: SymbolProfile.symbol,
transactionCount: 1 transactionCount: 1
}; };
} }
currentTransactionPointItem.tags = uniqBy( currentTransactionPointItem.tags = uniqBy(
currentTransactionPointItem.tags, currentTransactionPointItem.tags,
'id' 'id'
); );
symbols[SymbolProfile.symbol] = currentTransactionPointItem; symbols[SymbolProfile.symbol] = currentTransactionPointItem;
}
const items = lastTransactionPoint?.items ?? []; const items = lastTransactionPoint?.items ?? [];
@ -976,7 +983,10 @@ export abstract class PortfolioCalculator {
return symbol !== SymbolProfile.symbol; return symbol !== SymbolProfile.symbol;
}); });
newItems.push(currentTransactionPointItem); // Only add the symbol if it was created (i.e., for investment activities)
if (shouldCreateSymbol && currentTransactionPointItem) {
newItems.push(currentTransactionPointItem);
}
newItems.sort((a, b) => { newItems.sort((a, b) => {
return a.symbol?.localeCompare(b.symbol); return a.symbol?.localeCompare(b.symbol);

Loading…
Cancel
Save