From de882bf97c7a4084638d603ce5e9e2ed35434ddc Mon Sep 17 00:00:00 2001 From: piggei Date: Sat, 24 Oct 2026 05:15:08 +0200 Subject: [PATCH] fix(portfolio): use calendar days for ROAI weighting --- .../calculator/roai/accounting-days.spec.ts | 35 +++++++++++++++++++ .../calculator/roai/accounting-days.ts | 17 +++++++++ .../calculator/roai/portfolio-calculator.ts | 22 +++++------- 3 files changed, 61 insertions(+), 13 deletions(-) create mode 100644 apps/api/src/app/portfolio/calculator/roai/accounting-days.spec.ts create mode 100644 apps/api/src/app/portfolio/calculator/roai/accounting-days.ts diff --git a/apps/api/src/app/portfolio/calculator/roai/accounting-days.spec.ts b/apps/api/src/app/portfolio/calculator/roai/accounting-days.spec.ts new file mode 100644 index 0000000000..9320334a00 --- /dev/null +++ b/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); + }); +}); diff --git a/apps/api/src/app/portfolio/calculator/roai/accounting-days.ts b/apps/api/src/app/portfolio/calculator/roai/accounting-days.ts new file mode 100644 index 0000000000..d3b86f1e59 --- /dev/null +++ b/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); +} diff --git a/apps/api/src/app/portfolio/calculator/roai/portfolio-calculator.ts b/apps/api/src/app/portfolio/calculator/roai/portfolio-calculator.ts index f0af309d75..11e19fc187 100644 --- a/apps/api/src/app/portfolio/calculator/roai/portfolio-calculator.ts +++ b/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 { WeightedInvestment } from '@ghostfolio/api/app/portfolio/interfaces/weighted-investment.interface'; 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 { AssetProfileIdentifier, HistoricalDataItem @@ -15,9 +15,10 @@ import { DateRange } from '@ghostfolio/common/types'; import { PerformanceCalculationType } from '@ghostfolio/common/types/performance-calculation-type.type'; import { Big } from 'big.js'; -import { differenceInDays } from 'date-fns'; import { mapValues, sortedIndex } from 'lodash-es'; +import { differenceInAccountingDays } from './accounting-days'; + export class RoaiPortfolioCalculator extends PortfolioCalculator { protected calculateOverallPerformance({ positions @@ -78,12 +79,10 @@ export class RoaiPortfolioCalculator extends PortfolioCalculator { return performancePercentagesByDate; } - const parsedDates = historicalDataItems.map(({ date }) => { - return parseDate(date); - }); + const dates = historicalDataItems.map(({ date }) => date); - const daysUntilNextItem = parsedDates.slice(1).map((nextDate, index) => { - return differenceInDays(nextDate, parsedDates[index]); + const daysUntilNextItem = dates.slice(1).map((nextDate, index) => { + return differenceInAccountingDays(nextDate, dates[index]); }); // 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) ) { // Calculate the number of days since the previous activity - const activityDate = new Date(item.date); - const previousActivityDate = new Date(items[i - 1].date); - - let daysSinceLastActivity = differenceInDays( - activityDate, - previousActivityDate + let daysSinceLastActivity = differenceInAccountingDays( + item.date, + items[i - 1].date ); if (daysSinceLastActivity <= 0) { // The time between two activities on the same day is unknown