Browse Source

Fix algorithm and unit tests

pull/2778/head
Reto Kaul 2 years ago
committed by Thomas Kaul
parent
commit
101317ea29
  1. 8
      apps/api/src/app/portfolio/portfolio-calculator-btcusd-buy-and-sell-partially.spec.ts
  2. 8
      apps/api/src/app/portfolio/portfolio-calculator-novn-buy-and-sell-partially.spec.ts
  3. 39
      apps/api/src/app/portfolio/portfolio-calculator.ts

8
apps/api/src/app/portfolio/portfolio-calculator-btcusd-buy-and-sell-partially.spec.ts

@ -73,10 +73,10 @@ describe('PortfolioCalculator', () => {
currentValue: new Big('13657.2'),
errors: [],
grossPerformance: new Big('27172.74'),
grossPerformancePercentage: new Big('42.40043067128546016291'),
grossPerformancePercentage: new Big('42.41978276196153750666'),
hasErrors: false,
netPerformance: new Big('27172.74'),
netPerformancePercentage: new Big('42.40043067128546016291'),
netPerformancePercentage: new Big('42.41978276196153750666'),
positions: [
{
averagePrice: new Big('320.43'),
@ -85,10 +85,10 @@ describe('PortfolioCalculator', () => {
fee: new Big('0'),
firstBuyDate: '2015-01-01',
grossPerformance: new Big('27172.74'),
grossPerformancePercentage: new Big('42.40043067128546016291'),
grossPerformancePercentage: new Big('42.41978276196153750666'),
investment: new Big('320.43'),
netPerformance: new Big('27172.74'),
netPerformancePercentage: new Big('42.40043067128546016291'),
netPerformancePercentage: new Big('42.41978276196153750666'),
marketPrice: 13657.2,
quantity: new Big('1'),
symbol: 'BTCUSD',

8
apps/api/src/app/portfolio/portfolio-calculator-novn-buy-and-sell-partially.spec.ts

@ -73,10 +73,10 @@ describe('PortfolioCalculator', () => {
currentValue: new Big('87.8'),
errors: [],
grossPerformance: new Big('21.93'),
grossPerformancePercentage: new Big('0.14465699208443271768'),
grossPerformancePercentage: new Big('0.15113417083448194384'),
hasErrors: false,
netPerformance: new Big('17.68'),
netPerformancePercentage: new Big('0.11662269129287598945'),
netPerformancePercentage: new Big('0.12184460284330327256'),
positions: [
{
averagePrice: new Big('75.80'),
@ -85,10 +85,10 @@ describe('PortfolioCalculator', () => {
fee: new Big('4.25'),
firstBuyDate: '2022-03-07',
grossPerformance: new Big('21.93'),
grossPerformancePercentage: new Big('0.14465699208443271768'),
grossPerformancePercentage: new Big('0.15113417083448194384'),
investment: new Big('75.80'),
netPerformance: new Big('17.68'),
netPerformancePercentage: new Big('0.11662269129287598945'),
netPerformancePercentage: new Big('0.12184460284330327256'),
marketPrice: 87.8,
quantity: new Big('1'),
symbol: 'NOVN.SW',

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

@ -15,9 +15,9 @@ import {
addMilliseconds,
addMonths,
addYears,
differenceInDays,
endOfDay,
format,
intervalToDuration,
isAfter,
isBefore,
isSameDay,
@ -1195,6 +1195,7 @@ export class PortfolioCalculator {
console.log('transactionInvestment', transactionInvestment.toNumber());
}
const totalInvestmentBeforeTransaction = totalInvestment;
totalInvestment = totalInvestment.plus(transactionInvestment);
if (i >= indexOfStartOrder && totalInvestment.gt(maxTotalInvestment)) {
@ -1265,22 +1266,32 @@ export class PortfolioCalculator {
}
if (i > indexOfStartOrder) {
// Calculate the number of days since the previous order
const orderDate = new Date(order.date);
const previousOrderDate = new Date(orders[i - 1].date);
// Only consider periods with an investment for the calculation of
// the time weighted investment
if (totalInvestmentBeforeTransaction.gt(0)) {
// Calculate the number of days since the previous order
const orderDate = new Date(order.date);
const previousOrderDate = new Date(orders[i - 1].date);
let daysSinceLastOrder = differenceInDays(
orderDate,
previousOrderDate
);
const daysSinceLastOrder = intervalToDuration({
end: orderDate,
start: previousOrderDate
}).days;
// Set to at least 1 day, otherwise the transactions on the same day
// would not be considered in the time weighted calculation
if (daysSinceLastOrder <= 0) {
daysSinceLastOrder = 1;
}
// Sum up the total investment days since the start date to calculate the
// time weighted investment
totalInvestmentDays += daysSinceLastOrder;
// Sum up the total investment days since the start date to calculate
// the time weighted investment
totalInvestmentDays += daysSinceLastOrder;
sumOfTimeWeightedInvestments = sumOfTimeWeightedInvestments.add(
totalInvestment.mul(daysSinceLastOrder)
);
sumOfTimeWeightedInvestments = sumOfTimeWeightedInvestments.add(
totalInvestmentBeforeTransaction.mul(daysSinceLastOrder)
);
}
if (isChartMode) {
currentValues[order.date] = valueOfInvestment;

Loading…
Cancel
Save