|
|
@ -70,6 +70,7 @@ export class PortfolioCalculator { |
|
|
|
|
|
|
|
let lastDate: string = null; |
|
|
|
let lastTransactionPoint: TransactionPoint = null; |
|
|
|
|
|
|
|
for (const order of this.orders) { |
|
|
|
const currentDate = order.date; |
|
|
|
|
|
|
@ -78,12 +79,13 @@ export class PortfolioCalculator { |
|
|
|
|
|
|
|
const factor = getFactor(order.type); |
|
|
|
const unitPrice = new Big(order.unitPrice); |
|
|
|
|
|
|
|
if (oldAccumulatedSymbol) { |
|
|
|
const newQuantity = order.quantity |
|
|
|
.mul(factor) |
|
|
|
.plus(oldAccumulatedSymbol.quantity); |
|
|
|
|
|
|
|
let investment = new Big(0); |
|
|
|
let investment = oldAccumulatedSymbol.investment; |
|
|
|
|
|
|
|
if (newQuantity.gt(0)) { |
|
|
|
if (order.type === 'BUY') { |
|
|
@ -104,6 +106,7 @@ export class PortfolioCalculator { |
|
|
|
investment, |
|
|
|
currency: order.currency, |
|
|
|
dataSource: order.dataSource, |
|
|
|
dividend: new Big(0), |
|
|
|
fee: order.fee.plus(oldAccumulatedSymbol.fee), |
|
|
|
firstBuyDate: oldAccumulatedSymbol.firstBuyDate, |
|
|
|
quantity: newQuantity, |
|
|
@ -115,6 +118,7 @@ export class PortfolioCalculator { |
|
|
|
currentTransactionPointItem = { |
|
|
|
currency: order.currency, |
|
|
|
dataSource: order.dataSource, |
|
|
|
dividend: new Big(0), |
|
|
|
fee: order.fee, |
|
|
|
firstBuyDate: order.date, |
|
|
|
investment: unitPrice.mul(order.quantity).mul(factor), |
|
|
@ -583,6 +587,8 @@ export class PortfolioCalculator { |
|
|
|
); |
|
|
|
|
|
|
|
const { |
|
|
|
dividend, |
|
|
|
dividendInBaseCurrency, |
|
|
|
grossPerformance, |
|
|
|
grossPerformancePercentage, |
|
|
|
grossPerformancePercentageWithCurrencyEffect, |
|
|
@ -608,6 +614,8 @@ export class PortfolioCalculator { |
|
|
|
hasAnySymbolMetricsErrors = hasAnySymbolMetricsErrors || hasErrors; |
|
|
|
|
|
|
|
positions.push({ |
|
|
|
dividend, |
|
|
|
dividendInBaseCurrency, |
|
|
|
timeWeightedInvestment, |
|
|
|
timeWeightedInvestmentWithCurrencyEffect, |
|
|
|
averagePrice: item.quantity.eq(0) |
|
|
@ -842,6 +850,8 @@ export class PortfolioCalculator { |
|
|
|
const currentExchangeRate = exchangeRates[format(new Date(), DATE_FORMAT)]; |
|
|
|
const currentValues: { [date: string]: Big } = {}; |
|
|
|
const currentValuesWithCurrencyEffect: { [date: string]: Big } = {}; |
|
|
|
let dividend = new Big(0); |
|
|
|
let dividendInBaseCurrency = new Big(0); |
|
|
|
let fees = new Big(0); |
|
|
|
let feesAtStartDate = new Big(0); |
|
|
|
let feesAtStartDateWithCurrencyEffect = new Big(0); |
|
|
@ -894,6 +904,8 @@ export class PortfolioCalculator { |
|
|
|
return { |
|
|
|
currentValues: {}, |
|
|
|
currentValuesWithCurrencyEffect: {}, |
|
|
|
dividend: new Big(0), |
|
|
|
dividendInBaseCurrency: new Big(0), |
|
|
|
grossPerformance: new Big(0), |
|
|
|
grossPerformancePercentage: new Big(0), |
|
|
|
grossPerformancePercentageWithCurrencyEffect: new Big(0), |
|
|
@ -934,6 +946,8 @@ export class PortfolioCalculator { |
|
|
|
return { |
|
|
|
currentValues: {}, |
|
|
|
currentValuesWithCurrencyEffect: {}, |
|
|
|
dividend: new Big(0), |
|
|
|
dividendInBaseCurrency: new Big(0), |
|
|
|
grossPerformance: new Big(0), |
|
|
|
grossPerformancePercentage: new Big(0), |
|
|
|
grossPerformancePercentageWithCurrencyEffect: new Big(0), |
|
|
@ -1108,29 +1122,29 @@ export class PortfolioCalculator { |
|
|
|
valueOfInvestmentBeforeTransactionWithCurrencyEffect; |
|
|
|
} |
|
|
|
|
|
|
|
const transactionInvestment = |
|
|
|
order.type === 'BUY' |
|
|
|
? order.quantity |
|
|
|
.mul(order.unitPriceInBaseCurrency) |
|
|
|
.mul(getFactor(order.type)) |
|
|
|
: totalUnits.gt(0) |
|
|
|
? totalInvestment |
|
|
|
.div(totalUnits) |
|
|
|
.mul(order.quantity) |
|
|
|
.mul(getFactor(order.type)) |
|
|
|
: new Big(0); |
|
|
|
|
|
|
|
const transactionInvestmentWithCurrencyEffect = |
|
|
|
order.type === 'BUY' |
|
|
|
? order.quantity |
|
|
|
.mul(order.unitPriceInBaseCurrencyWithCurrencyEffect) |
|
|
|
.mul(getFactor(order.type)) |
|
|
|
: totalUnits.gt(0) |
|
|
|
? totalInvestmentWithCurrencyEffect |
|
|
|
.div(totalUnits) |
|
|
|
.mul(order.quantity) |
|
|
|
.mul(getFactor(order.type)) |
|
|
|
: new Big(0); |
|
|
|
let transactionInvestment = new Big(0); |
|
|
|
let transactionInvestmentWithCurrencyEffect = new Big(0); |
|
|
|
|
|
|
|
if (order.type === 'BUY') { |
|
|
|
transactionInvestment = order.quantity |
|
|
|
.mul(order.unitPriceInBaseCurrency) |
|
|
|
.mul(getFactor(order.type)); |
|
|
|
transactionInvestmentWithCurrencyEffect = order.quantity |
|
|
|
.mul(order.unitPriceInBaseCurrencyWithCurrencyEffect) |
|
|
|
.mul(getFactor(order.type)); |
|
|
|
} else if (order.type === 'SELL') { |
|
|
|
if (totalUnits.gt(0)) { |
|
|
|
transactionInvestment = totalInvestment |
|
|
|
.div(totalUnits) |
|
|
|
.mul(order.quantity) |
|
|
|
.mul(getFactor(order.type)); |
|
|
|
transactionInvestmentWithCurrencyEffect = |
|
|
|
totalInvestmentWithCurrencyEffect |
|
|
|
.div(totalUnits) |
|
|
|
.mul(order.quantity) |
|
|
|
.mul(getFactor(order.type)); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
if (PortfolioCalculator.ENABLE_LOGGING) { |
|
|
|
console.log('totalInvestment', totalInvestment.toNumber()); |
|
|
@ -1186,6 +1200,13 @@ export class PortfolioCalculator { |
|
|
|
|
|
|
|
totalUnits = totalUnits.plus(order.quantity.mul(getFactor(order.type))); |
|
|
|
|
|
|
|
if (order.type === 'DIVIDEND') { |
|
|
|
dividend = dividend.plus(order.quantity.mul(order.unitPrice)); |
|
|
|
dividendInBaseCurrency = dividendInBaseCurrency.plus( |
|
|
|
dividend.mul(exchangeRateAtOrderDate ?? 1) |
|
|
|
); |
|
|
|
} |
|
|
|
|
|
|
|
const valueOfInvestment = totalUnits.mul(order.unitPriceInBaseCurrency); |
|
|
|
|
|
|
|
const valueOfInvestmentWithCurrencyEffect = totalUnits.mul( |
|
|
@ -1277,7 +1298,7 @@ export class PortfolioCalculator { |
|
|
|
grossPerformanceWithCurrencyEffect; |
|
|
|
} |
|
|
|
|
|
|
|
if (i > indexOfStartOrder) { |
|
|
|
if (i > indexOfStartOrder && ['BUY', 'SELL'].includes(order.type)) { |
|
|
|
// Only consider periods with an investment for the calculation of
|
|
|
|
// the time weighted investment
|
|
|
|
if (valueOfInvestmentBeforeTransaction.gt(0)) { |
|
|
@ -1471,6 +1492,7 @@ export class PortfolioCalculator { |
|
|
|
Time weighted investment with currency effect: ${timeWeightedAverageInvestmentBetweenStartAndEndDateWithCurrencyEffect.toFixed( |
|
|
|
2 |
|
|
|
)} |
|
|
|
Total dividend: ${dividend.toFixed(2)} |
|
|
|
Gross performance: ${totalGrossPerformance.toFixed( |
|
|
|
2 |
|
|
|
)} / ${grossPerformancePercentage.mul(100).toFixed(2)}% |
|
|
@ -1495,6 +1517,8 @@ export class PortfolioCalculator { |
|
|
|
return { |
|
|
|
currentValues, |
|
|
|
currentValuesWithCurrencyEffect, |
|
|
|
dividend, |
|
|
|
dividendInBaseCurrency, |
|
|
|
grossPerformancePercentage, |
|
|
|
grossPerformancePercentageWithCurrencyEffect, |
|
|
|
initialValue, |
|
|
|