Repository navigation
feat(analytics): compute portfolio TWR from subperiod snapshots (#21) - #23
Open
jeffrey-graham wants to merge 1 commit into
Open
jeffrey-graham wants to merge 1 commit into
jeffrey-graham wants to merge 1 commit into
Conversation
jeffrey-graham
force-pushed
the
jeffrey-graham-patch-1
branch
from
November 12, 2026 02:31
eb39871 to
be88579
Compare
This branch has not been deployed
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Closes #21
Replaces the synthetic
[roi / 200, roi / 200]two-half ROI split incomputePortfolioFromTradeswith real sequential subperiod compounding from chronological valuation and net cash-flow snapshots (PortfolioValuationSnapshot):calculateTWRFromSnapshots(snapshots)(lib/analytics/portfolio.ts): Sorts snapshots chronologically, validates strictly increasing finite timestamps and positive starting subperiod valuations, computes each subperiod holding return asr_i = (V_end - CF_i - V_start) / V_start, and compounds via((∏ (1 + r_i)) - 1) * 100.PortfolioPerformance): TypestimeWeightedReturnasnumber | nullalongsidetwrAvailable: booleanso callers receivenull/falsewhenever fewer than two valid chronological snapshots exist rather than a fabricated ROI split.__tests__/analytics/portfolio.test.ts):$1,000 -> $1,100(+10%) followed by$1,100 -> $1,320(+20%) yields(1.10 * 1.20 - 1) * 100 = 32%.$1,000, growing+10%to$1,100with a+$900deposit (V_end = $2,000,netCashFlow = 900->r_1 = (2000 - 900 - 1000) / 1000 = +10%), then declining-5%to$1,900with a-$400withdrawal (V_end = $1,500,netCashFlow = -400->r_2 = (1500 - (-400) - 2000) / 2000 = -5%) yields(1.10 * 0.95 - 1) * 100 = 4.5%, identical to an unadjusted$1,000 -> $1,100 -> $1,045series.Local Verification
npm run type-check->0errorsnpm run lint->0warnings,0errorsnpx jest --runInBand->8suites passed (16 / 16tests)npm audit --omit=dev --audit-level=moderate&npm audit --audit-level=high-> passed (0prod vulnerabilities; GitHub Actions CI awaiting maintainer approval)