|
@ -78,32 +78,29 @@ export class PortfolioCalculator { |
|
|
const oldAccumulatedSymbol = symbols[order.symbol]; |
|
|
const oldAccumulatedSymbol = symbols[order.symbol]; |
|
|
|
|
|
|
|
|
const factor = getFactor(order.type); |
|
|
const factor = getFactor(order.type); |
|
|
const unitPrice = new Big(order.unitPrice); |
|
|
|
|
|
|
|
|
|
|
|
if (oldAccumulatedSymbol) { |
|
|
if (oldAccumulatedSymbol) { |
|
|
|
|
|
let investment = oldAccumulatedSymbol.investment; |
|
|
|
|
|
|
|
|
const newQuantity = order.quantity |
|
|
const newQuantity = order.quantity |
|
|
.mul(factor) |
|
|
.mul(factor) |
|
|
.plus(oldAccumulatedSymbol.quantity); |
|
|
.plus(oldAccumulatedSymbol.quantity); |
|
|
|
|
|
|
|
|
let investment = oldAccumulatedSymbol.investment; |
|
|
|
|
|
|
|
|
|
|
|
if (newQuantity.gt(0)) { |
|
|
|
|
|
if (order.type === 'BUY') { |
|
|
if (order.type === 'BUY') { |
|
|
investment = oldAccumulatedSymbol.investment.plus( |
|
|
investment = oldAccumulatedSymbol.investment.plus( |
|
|
order.quantity.mul(unitPrice) |
|
|
order.quantity.mul(order.unitPrice) |
|
|
); |
|
|
); |
|
|
} else if (order.type === 'SELL') { |
|
|
} else if (order.type === 'SELL') { |
|
|
const averagePrice = oldAccumulatedSymbol.investment.div( |
|
|
|
|
|
oldAccumulatedSymbol.quantity |
|
|
|
|
|
); |
|
|
|
|
|
investment = oldAccumulatedSymbol.investment.minus( |
|
|
investment = oldAccumulatedSymbol.investment.minus( |
|
|
order.quantity.mul(averagePrice) |
|
|
order.quantity.mul(oldAccumulatedSymbol.averagePrice) |
|
|
); |
|
|
); |
|
|
} |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
currentTransactionPointItem = { |
|
|
currentTransactionPointItem = { |
|
|
investment, |
|
|
investment, |
|
|
|
|
|
averagePrice: newQuantity.gt(0) |
|
|
|
|
|
? investment.div(newQuantity) |
|
|
|
|
|
: new Big(0), |
|
|
currency: order.currency, |
|
|
currency: order.currency, |
|
|
dataSource: order.dataSource, |
|
|
dataSource: order.dataSource, |
|
|
dividend: new Big(0), |
|
|
dividend: new Big(0), |
|
@ -116,12 +113,13 @@ export class PortfolioCalculator { |
|
|
}; |
|
|
}; |
|
|
} else { |
|
|
} else { |
|
|
currentTransactionPointItem = { |
|
|
currentTransactionPointItem = { |
|
|
|
|
|
averagePrice: order.unitPrice, |
|
|
currency: order.currency, |
|
|
currency: order.currency, |
|
|
dataSource: order.dataSource, |
|
|
dataSource: order.dataSource, |
|
|
dividend: new Big(0), |
|
|
dividend: new Big(0), |
|
|
fee: order.fee, |
|
|
fee: order.fee, |
|
|
firstBuyDate: order.date, |
|
|
firstBuyDate: order.date, |
|
|
investment: unitPrice.mul(order.quantity).mul(factor), |
|
|
investment: order.unitPrice.mul(order.quantity).mul(factor), |
|
|
quantity: order.quantity.mul(factor), |
|
|
quantity: order.quantity.mul(factor), |
|
|
symbol: order.symbol, |
|
|
symbol: order.symbol, |
|
|
tags: order.tags, |
|
|
tags: order.tags, |
|
@ -132,22 +130,28 @@ export class PortfolioCalculator { |
|
|
symbols[order.symbol] = currentTransactionPointItem; |
|
|
symbols[order.symbol] = currentTransactionPointItem; |
|
|
|
|
|
|
|
|
const items = lastTransactionPoint?.items ?? []; |
|
|
const items = lastTransactionPoint?.items ?? []; |
|
|
|
|
|
|
|
|
const newItems = items.filter( |
|
|
const newItems = items.filter( |
|
|
(transactionPointItem) => transactionPointItem.symbol !== order.symbol |
|
|
(transactionPointItem) => transactionPointItem.symbol !== order.symbol |
|
|
); |
|
|
); |
|
|
|
|
|
|
|
|
newItems.push(currentTransactionPointItem); |
|
|
newItems.push(currentTransactionPointItem); |
|
|
|
|
|
|
|
|
newItems.sort((a, b) => { |
|
|
newItems.sort((a, b) => { |
|
|
return a.symbol?.localeCompare(b.symbol); |
|
|
return a.symbol?.localeCompare(b.symbol); |
|
|
}); |
|
|
}); |
|
|
|
|
|
|
|
|
if (lastDate !== currentDate || lastTransactionPoint === null) { |
|
|
if (lastDate !== currentDate || lastTransactionPoint === null) { |
|
|
lastTransactionPoint = { |
|
|
lastTransactionPoint = { |
|
|
date: currentDate, |
|
|
date: currentDate, |
|
|
items: newItems |
|
|
items: newItems |
|
|
}; |
|
|
}; |
|
|
|
|
|
|
|
|
this.transactionPoints.push(lastTransactionPoint); |
|
|
this.transactionPoints.push(lastTransactionPoint); |
|
|
} else { |
|
|
} else { |
|
|
lastTransactionPoint.items = newItems; |
|
|
lastTransactionPoint.items = newItems; |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
lastDate = currentDate; |
|
|
lastDate = currentDate; |
|
|
} |
|
|
} |
|
|
} |
|
|
} |
|
|