Feature/fix twr performance (#679)

* Fix TWR performance

Co-authored-by: Reto Kaul <retokaul@sublimd.com>
pull/681/head
gizmodus 3 years ago committed by GitHub
parent 8d2fcc6b42
commit 9b5ec0c56d
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

@ -337,8 +337,8 @@ export class PortfolioCalculatorNew {
let grossPerformanceFromSells = new Big(0); let grossPerformanceFromSells = new Big(0);
let initialValue: Big; let initialValue: Big;
let lastAveragePrice = new Big(0); let lastAveragePrice = new Big(0);
let lastValueOfInvestment = new Big(0); let lastTransactionInvestment = new Big(0);
let lastNetValueOfInvestment = new Big(0); let lastValueOfInvestmentBeforeTransaction = new Big(0);
let timeWeightedGrossPerformancePercentage = new Big(1); let timeWeightedGrossPerformancePercentage = new Big(1);
let timeWeightedNetPerformancePercentage = new Big(1); let timeWeightedNetPerformancePercentage = new Big(1);
let totalInvestment = new Big(0); let totalInvestment = new Big(0);
@ -394,7 +394,13 @@ export class PortfolioCalculatorNew {
for (let i = 0; i < orders.length; i += 1) { for (let i = 0; i < orders.length; i += 1) {
const order = orders[i]; const order = orders[i];
const transactionInvestment = order.quantity.mul(order.unitPrice); const valueOfInvestmentBeforeTransaction = totalUnits.mul(
order.unitPrice
);
const transactionInvestment = order.quantity
.mul(order.unitPrice)
.mul(this.getFactor(order.type));
if ( if (
!initialValue && !initialValue &&
@ -411,7 +417,6 @@ export class PortfolioCalculatorNew {
); );
const valueOfInvestment = totalUnits.mul(order.unitPrice); const valueOfInvestment = totalUnits.mul(order.unitPrice);
const netValueOfInvestment = totalUnits.mul(order.unitPrice).sub(fees);
const grossPerformanceFromSell = const grossPerformanceFromSell =
order.type === TypeOfOrder.SELL order.type === TypeOfOrder.SELL
@ -423,7 +428,7 @@ export class PortfolioCalculatorNew {
); );
totalInvestment = totalInvestment totalInvestment = totalInvestment
.plus(transactionInvestment.mul(this.getFactor(order.type))) .plus(transactionInvestment)
.plus(grossPerformanceFromSell); .plus(grossPerformanceFromSell);
lastAveragePrice = totalUnits.eq(0) lastAveragePrice = totalUnits.eq(0)
@ -436,48 +441,52 @@ export class PortfolioCalculatorNew {
if ( if (
i > indexOfStartOrder && i > indexOfStartOrder &&
!lastValueOfInvestment !lastValueOfInvestmentBeforeTransaction
.plus(transactionInvestment.mul(this.getFactor(order.type))) .plus(lastTransactionInvestment)
.eq(0) .eq(0)
) { ) {
timeWeightedGrossPerformancePercentage = const grossHoldingPeriodReturn = valueOfInvestmentBeforeTransaction
timeWeightedGrossPerformancePercentage.mul( .sub(
new Big(1).plus( lastValueOfInvestmentBeforeTransaction.plus(
valueOfInvestment lastTransactionInvestment
.minus(
lastValueOfInvestment.plus(
transactionInvestment.mul(this.getFactor(order.type))
) )
) )
.div( .div(
lastValueOfInvestment.plus( lastValueOfInvestmentBeforeTransaction.plus(
transactionInvestment.mul(this.getFactor(order.type)) lastTransactionInvestment
)
)
) )
); );
timeWeightedNetPerformancePercentage = timeWeightedGrossPerformancePercentage =
timeWeightedNetPerformancePercentage.mul( timeWeightedGrossPerformancePercentage.mul(
new Big(1).plus( new Big(1).plus(grossHoldingPeriodReturn)
netValueOfInvestment );
.minus(
lastNetValueOfInvestment.plus( const netHoldingPeriodReturn = valueOfInvestmentBeforeTransaction
transactionInvestment.mul(this.getFactor(order.type)) .sub(fees.sub(order.fee))
.sub(
lastValueOfInvestmentBeforeTransaction.plus(
lastTransactionInvestment
) )
) )
.div( .div(
lastNetValueOfInvestment.plus( lastValueOfInvestmentBeforeTransaction.plus(
transactionInvestment.mul(this.getFactor(order.type)) lastTransactionInvestment
)
)
) )
); );
timeWeightedNetPerformancePercentage =
timeWeightedNetPerformancePercentage.mul(
new Big(1).plus(netHoldingPeriodReturn)
);
} }
grossPerformance = newGrossPerformance; grossPerformance = newGrossPerformance;
lastNetValueOfInvestment = netValueOfInvestment;
lastValueOfInvestment = valueOfInvestment; lastTransactionInvestment = transactionInvestment;
lastValueOfInvestmentBeforeTransaction =
valueOfInvestmentBeforeTransaction;
if (order.itemType === 'start') { if (order.itemType === 'start') {
feesAtStartDate = fees; feesAtStartDate = fees;

Loading…
Cancel
Save