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. 27
      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'), currentValue: new Big('13657.2'),
errors: [], errors: [],
grossPerformance: new Big('27172.74'), grossPerformance: new Big('27172.74'),
grossPerformancePercentage: new Big('42.40043067128546016291'), grossPerformancePercentage: new Big('42.41978276196153750666'),
hasErrors: false, hasErrors: false,
netPerformance: new Big('27172.74'), netPerformance: new Big('27172.74'),
netPerformancePercentage: new Big('42.40043067128546016291'), netPerformancePercentage: new Big('42.41978276196153750666'),
positions: [ positions: [
{ {
averagePrice: new Big('320.43'), averagePrice: new Big('320.43'),
@ -85,10 +85,10 @@ describe('PortfolioCalculator', () => {
fee: new Big('0'), fee: new Big('0'),
firstBuyDate: '2015-01-01', firstBuyDate: '2015-01-01',
grossPerformance: new Big('27172.74'), grossPerformance: new Big('27172.74'),
grossPerformancePercentage: new Big('42.40043067128546016291'), grossPerformancePercentage: new Big('42.41978276196153750666'),
investment: new Big('320.43'), investment: new Big('320.43'),
netPerformance: new Big('27172.74'), netPerformance: new Big('27172.74'),
netPerformancePercentage: new Big('42.40043067128546016291'), netPerformancePercentage: new Big('42.41978276196153750666'),
marketPrice: 13657.2, marketPrice: 13657.2,
quantity: new Big('1'), quantity: new Big('1'),
symbol: 'BTCUSD', 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'), currentValue: new Big('87.8'),
errors: [], errors: [],
grossPerformance: new Big('21.93'), grossPerformance: new Big('21.93'),
grossPerformancePercentage: new Big('0.14465699208443271768'), grossPerformancePercentage: new Big('0.15113417083448194384'),
hasErrors: false, hasErrors: false,
netPerformance: new Big('17.68'), netPerformance: new Big('17.68'),
netPerformancePercentage: new Big('0.11662269129287598945'), netPerformancePercentage: new Big('0.12184460284330327256'),
positions: [ positions: [
{ {
averagePrice: new Big('75.80'), averagePrice: new Big('75.80'),
@ -85,10 +85,10 @@ describe('PortfolioCalculator', () => {
fee: new Big('4.25'), fee: new Big('4.25'),
firstBuyDate: '2022-03-07', firstBuyDate: '2022-03-07',
grossPerformance: new Big('21.93'), grossPerformance: new Big('21.93'),
grossPerformancePercentage: new Big('0.14465699208443271768'), grossPerformancePercentage: new Big('0.15113417083448194384'),
investment: new Big('75.80'), investment: new Big('75.80'),
netPerformance: new Big('17.68'), netPerformance: new Big('17.68'),
netPerformancePercentage: new Big('0.11662269129287598945'), netPerformancePercentage: new Big('0.12184460284330327256'),
marketPrice: 87.8, marketPrice: 87.8,
quantity: new Big('1'), quantity: new Big('1'),
symbol: 'NOVN.SW', symbol: 'NOVN.SW',

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

@ -15,9 +15,9 @@ import {
addMilliseconds, addMilliseconds,
addMonths, addMonths,
addYears, addYears,
differenceInDays,
endOfDay, endOfDay,
format, format,
intervalToDuration,
isAfter, isAfter,
isBefore, isBefore,
isSameDay, isSameDay,
@ -1195,6 +1195,7 @@ export class PortfolioCalculator {
console.log('transactionInvestment', transactionInvestment.toNumber()); console.log('transactionInvestment', transactionInvestment.toNumber());
} }
const totalInvestmentBeforeTransaction = totalInvestment;
totalInvestment = totalInvestment.plus(transactionInvestment); totalInvestment = totalInvestment.plus(transactionInvestment);
if (i >= indexOfStartOrder && totalInvestment.gt(maxTotalInvestment)) { if (i >= indexOfStartOrder && totalInvestment.gt(maxTotalInvestment)) {
@ -1265,22 +1266,32 @@ export class PortfolioCalculator {
} }
if (i > indexOfStartOrder) { if (i > indexOfStartOrder) {
// 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 // Calculate the number of days since the previous order
const orderDate = new Date(order.date); const orderDate = new Date(order.date);
const previousOrderDate = new Date(orders[i - 1].date); const previousOrderDate = new Date(orders[i - 1].date);
const daysSinceLastOrder = intervalToDuration({ let daysSinceLastOrder = differenceInDays(
end: orderDate, orderDate,
start: previousOrderDate 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 // Sum up the total investment days since the start date to calculate
// time weighted investment // the time weighted investment
totalInvestmentDays += daysSinceLastOrder; totalInvestmentDays += daysSinceLastOrder;
sumOfTimeWeightedInvestments = sumOfTimeWeightedInvestments.add( sumOfTimeWeightedInvestments = sumOfTimeWeightedInvestments.add(
totalInvestment.mul(daysSinceLastOrder) totalInvestmentBeforeTransaction.mul(daysSinceLastOrder)
); );
}
if (isChartMode) { if (isChartMode) {
currentValues[order.date] = valueOfInvestment; currentValues[order.date] = valueOfInvestment;

Loading…
Cancel
Save