Browse Source

fix(portfolio): use calendar days for ROAI weighting

pull/8288/head
piggei 1 month ago
parent
commit
de882bf97c
  1. 35
      apps/api/src/app/portfolio/calculator/roai/accounting-days.spec.ts
  2. 17
      apps/api/src/app/portfolio/calculator/roai/accounting-days.ts
  3. 22
      apps/api/src/app/portfolio/calculator/roai/portfolio-calculator.ts

35
apps/api/src/app/portfolio/calculator/roai/accounting-days.spec.ts

@ -0,0 +1,35 @@
import { differenceInAccountingDays } from './accounting-days';
describe('ROAI accounting-day weighting', () => {
it('counts one day across the autumn daylight-saving transition', () => {
expect(differenceInAccountingDays('2024-10-28', '2024-10-27')).toBe(1);
});
it('counts one day across the spring daylight-saving transition', () => {
expect(differenceInAccountingDays('2024-04-01', '2024-03-31')).toBe(1);
});
it('counts 366 days across the leap year 2024', () => {
expect(differenceInAccountingDays('2024-12-31', '2023-12-31')).toBe(366);
});
it('counts days before the Unix epoch', () => {
expect(differenceInAccountingDays('1969-12-31', '1969-12-30')).toBe(1);
});
it('counts days across the Unix epoch', () => {
expect(differenceInAccountingDays('1970-01-01', '1969-12-31')).toBe(1);
});
it('preserves years before 100 CE', () => {
expect(differenceInAccountingDays('0099-01-02', '0099-01-01')).toBe(1);
});
it('returns zero for the same accounting date', () => {
expect(differenceInAccountingDays('2024-10-27', '2024-10-27')).toBe(0);
});
it('preserves the sign for reversed accounting dates', () => {
expect(differenceInAccountingDays('2024-10-27', '2024-10-28')).toBe(-1);
});
});

17
apps/api/src/app/portfolio/calculator/roai/accounting-days.ts

@ -0,0 +1,17 @@
const MILLISECONDS_PER_DAY = 86_400_000;
export function differenceInAccountingDays(
later: string,
earlier: string
): number {
const toDayNumber = (date: string): number => {
const [year, month, day] = date.split('-').map(Number);
const utcDate = new Date(0);
utcDate.setUTCFullYear(year, month - 1, day);
return Math.floor(utcDate.getTime() / MILLISECONDS_PER_DAY);
};
return toDayNumber(later) - toDayNumber(earlier);
}

22
apps/api/src/app/portfolio/calculator/roai/portfolio-calculator.ts

@ -5,7 +5,7 @@ import { HoldingPerformance } from '@ghostfolio/api/app/portfolio/interfaces/hol
import { PortfolioCalculatorHolding } from '@ghostfolio/api/app/portfolio/interfaces/portfolio-calculator-holding.interface'; import { PortfolioCalculatorHolding } from '@ghostfolio/api/app/portfolio/interfaces/portfolio-calculator-holding.interface';
import { WeightedInvestment } from '@ghostfolio/api/app/portfolio/interfaces/weighted-investment.interface'; import { WeightedInvestment } from '@ghostfolio/api/app/portfolio/interfaces/weighted-investment.interface';
import { PerformancePercentages } from '@ghostfolio/api/app/portfolio/types/performance-percentages.type'; import { PerformancePercentages } from '@ghostfolio/api/app/portfolio/types/performance-percentages.type';
import { getSum, parseDate } from '@ghostfolio/common/helper'; import { getSum } from '@ghostfolio/common/helper';
import { import {
AssetProfileIdentifier, AssetProfileIdentifier,
HistoricalDataItem HistoricalDataItem
@ -15,9 +15,10 @@ import { DateRange } from '@ghostfolio/common/types';
import { PerformanceCalculationType } from '@ghostfolio/common/types/performance-calculation-type.type'; import { PerformanceCalculationType } from '@ghostfolio/common/types/performance-calculation-type.type';
import { Big } from 'big.js'; import { Big } from 'big.js';
import { differenceInDays } from 'date-fns';
import { mapValues, sortedIndex } from 'lodash-es'; import { mapValues, sortedIndex } from 'lodash-es';
import { differenceInAccountingDays } from './accounting-days';
export class RoaiPortfolioCalculator extends PortfolioCalculator { export class RoaiPortfolioCalculator extends PortfolioCalculator {
protected calculateOverallPerformance({ protected calculateOverallPerformance({
positions positions
@ -78,12 +79,10 @@ export class RoaiPortfolioCalculator extends PortfolioCalculator {
return performancePercentagesByDate; return performancePercentagesByDate;
} }
const parsedDates = historicalDataItems.map(({ date }) => { const dates = historicalDataItems.map(({ date }) => date);
return parseDate(date);
});
const daysUntilNextItem = parsedDates.slice(1).map((nextDate, index) => { const daysUntilNextItem = dates.slice(1).map((nextDate, index) => {
return differenceInDays(nextDate, parsedDates[index]); return differenceInAccountingDays(nextDate, dates[index]);
}); });
// Take the values at the start date from the first day of the date range // Take the values at the start date from the first day of the date range
@ -213,12 +212,9 @@ export class RoaiPortfolioCalculator extends PortfolioCalculator {
['BUY', 'SELL'].includes(item.type) ['BUY', 'SELL'].includes(item.type)
) { ) {
// Calculate the number of days since the previous activity // Calculate the number of days since the previous activity
const activityDate = new Date(item.date); let daysSinceLastActivity = differenceInAccountingDays(
const previousActivityDate = new Date(items[i - 1].date); item.date,
items[i - 1].date
let daysSinceLastActivity = differenceInDays(
activityDate,
previousActivityDate
); );
if (daysSinceLastActivity <= 0) { if (daysSinceLastActivity <= 0) {
// The time between two activities on the same day is unknown // The time between two activities on the same day is unknown

Loading…
Cancel
Save