Skip to content

fix(history): period totals read the window or say they are incomplete - #99

Merged
Hiksang merged 19 commits into
mainfrom
fix/period-totals
Nov 15, 2026
Merged

Hiksang merged 19 commits into
mainfrom
fix/period-totals

Conversation

@Hiksang

@Hiksang Hiksang commented Nov 13, 2026

Copy link
Copy Markdown
Collaborator

What does this PR do?

Period totals (funding, fees, PnL over --since / --period / 24 h) summed a capped list and presented it as the whole period. They now read the window, and when the window cannot be proven complete they say so: a label on history commands, null plus a reason on 24 h figures. Never a silent partial sum (SSOT Rule #2).

Command Before Now
history pnl, history funding, MCP get_arb_compare A venue's capped list (for example Hyperliquid 200 rows) filtered by date and summed as the period total Windowed read; incomplete map and a warning line when coverage is unproven
portfolio funding24h Summed the newest rows (for example −200 shown for −240) The 24 h window, or null with fundingIncomplete / fundingError
history compare --period Same capped-list defect Windowed read; incomplete["<venue>.funding"]
history report --since Summed the newest 50 trades and 50 funding rows whatever the period Sums the period; incomplete; an unparseable --since is an error
arb scan --positions Actual 24h 100-row cap inside the 24 h sum The 24 h window, or null with the reason in fundingHistoryErrors
Windowed reads — Pacifica cursor paging, Hyperliquid trades since, Aster getFundingPaymentsSince / getTradesSince (1000 per symbol; a full page is incomplete)
Tie at the window start A full page whose oldest row is exactly the window start counted as complete Incomplete (Hyperliquid, Aster and the capped helpers)

JSON contract changes are in the CHANGELOG with migration notes: new incomplete maps, funding24h / actual24h can be null, history report --since <unparseable> is ok: false.

Costs and limits — please read

  • Pacifica: /funding/history has no time filter, so the read pages the account's whole funding list (up to 10 × 200 rows). An account with more than 2000 lifetime funding rows gets funding24h: null with a warning, and each read costs up to 10 requests (portfolio --watch repeats this per refresh). Pacifica is the default venue, so this is visible. Stopping early needs the row order confirmed live; it is not guessed here.
  • Venues with no windowed read and no declared reach (RISEx, TxFlow, Arcus, Ondo and others): reported incomplete even for an empty list, and funding24h is null. Their end-of-list behaviour needs a live check.
  • No --since / --period: history report / compare read the whole history: Hyperliquid up to 20 requests, Aster up to 60, Pacifica up to 10 per list.
  • Aster, not re-checked live: the userTrades limit of 1000, that a full page holds the newest rows, and income retention. Symbol discovery relies on COMMISSION income rows, so a zero-fee fill on a symbol with no open position or order can be missed.
  • history report / compare / pnl / funding print a lower-bound number with an incomplete label, while the 24 h figures are null. This difference is deliberate and documented.

Known, not fixed here (same class, follow-up): arb/index.ts:1811, arb-auto.ts:3001, funding-arb-strategy.ts:146, spot-perp-arb-strategy.ts:139, funding-auto.ts:573 (realized PnL from 50 rows), trade.ts:2247.

Size: 17 files, +1128 / −112. This is the last of 4 PRs split from the portfolio-totals work; it could be split again (first-round fixes vs. the history compare / report / arb scan follow-ups) if preferred.

Checklist

  • pnpm test passes (336 files / 7313 tests)
  • tsc --noEmit and typecheck:test pass
  • No secrets, API keys, or private keys included
  • Breaking change? See JSON contract changes

Testing standard (docs/TESTING.md §7)

  • One behaviour per test; no test.skip / .only
  • New tests fail before their fix (checked by stashing the source change; the third reviewer traced several independently)
  • No fixture change

Three independent reviews. The third asked for one MEDIUM (the Aster tie rule) and five LOW items; all are applied in the last five commits. Those five commits were read by the session lead but not sent back for a fourth review. One of them (35a3016, building fewer Aster day slices) has no test, because its effect is not observable from outside.

Live ↔ Unit Test Cross-Validation

Unit tests only. Nothing here was run against live venues; the items under "Costs and limits" list what needs a live check before release.

Merge note

Every open fix PR adds entries to CHANGELOG.md [Unreleased] (keep both sides).

🤖 Generated with Claude Code

Hiksang and others added 17 commits November 12, 2026 01:01
…omplete

Hyperliquid getFundingPayments(200) returns at most 200 rows / 7 days; portfolio funding24h,
MCP get_funding_analysis / get_pnl_analysis and `account pnl --period` summed that list as the
period total (240 rows in 24 h gave -200 with fundingIncomplete false; "30d" covered 7 days).
Hyperliquid gets a windowed read (getFundingPaymentsSince: one page for a small window, else
chunks backward from now; past 20 requests, the newest stretch with how far back it reached).
An incomplete window is funding24h null + fundingError in portfolio, meta.incomplete in the MCP
tools (trades included for fees), and the new fundingIncomplete / tradesIncomplete fields of
`account pnl`.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
…covers the period

"Fewer rows than asked" does not prove a history covers a period: Aster getFundingPayments reads 7
days and getTradeHistory finds its symbols in 7 days of income, so a 30-day total claimed complete.
Hyperliquid getTradeHistory filters the latest 2000 fills, and other venues' time windows and page
caps are not documented.

A list is complete when the venue ends it. Lighter follows the cursor to the end (historyReach).
Pacifica trades ([start, now] via start_time / end_time) and funding (no time filter: read whole)
follow next_cursor until has_more is false, up to 10 pages — correct whatever the row order; past
that nothing is proven. Hyperliquid userFills under 2000 fills is the whole list (getTradesSince,
tradesInWindow). Aster gets a windowed funding read (getFundingPaymentsSince): the whole window in
one request when it holds fewer than 1000 rows, else day slices newest first, labelled past its
request cap; only the documented 7 days are proven, so an older window stays labelled. Elsewhere
only rows reaching past the window start prove it; an empty list proves nothing (coveredFrom
Infinity, so the label no longer depends on the clock).
history pnl, history funding and MCP get_pnl_analysis / get_arb_compare move to the same helpers
(new incomplete map; partial still means a venue read failed; --limit applies only to venues
without a period read).

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
getFundingPaymentsSince spent its page budget on empty chunks before the first funding, so a
complete 30-day history read with window 0 ended partial after 20 requests. Chunks now stop at the
earliest row of the first, oldest-first page: 30 days / 7200 rows is complete, and 10 days / 720
rows takes 5 requests instead of 16.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
… read

It was 0, which reads as a known zero (TESTING section 4: unknown = null); the --serve envelope,
which reads no funding, carried 0 too. The type is now number | null, listed under JSON contract
changes.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
…incomplete

history compare summed getFundingPayments(--limit) filtered to the period
with no marker. It now uses fundingPaymentsInWindow (like MCP
get_arb_compare) and reports an incomplete map plus a warning line.

Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
…abels them

history report summed the newest 50 trades / funding payments with no
time filter under period: <since>. It now reads tradesInWindow /
fundingPaymentsInWindow for the period and reports an incomplete map;
an unparseable --since is an error.

Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
handleFundingPositions filtered getFundingPayments(100) to the last 24 h.
It now uses fundingPaymentsInWindow; a read not proven to cover the day
leaves actual24h null and records why in fundingHistoryErrors.

Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
… period incomplete

getTradeHistory passed --limit through to userTrades unbounded. It now
asks at most 1000 rows per symbol, and the new getTradesSince reports a
symbol's full page as coverage back to its oldest fill only. Symbol
discovery's COMMISSION-row dependency is documented.

Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
…stricter period rule

Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
…complete false

The code (buildTotals with zero venues, the --serve envelope) already
reports null + false; the entry said null implies true.

Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
… not cover it

A full page (Hyperliquid's 2000 fills, a capped getFundingPayments /
getTradeHistory list) may have cut other rows of its oldest millisecond,
so coverage now requires that row to be strictly before the window start.

Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
…ions null reason

Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
…art does not cover it

Same tie rule as Hyperliquid getTradesSince and the capped helpers (1b5f144).

Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
…indowed reads report coverage)

historyReach was no longer read by tradesInWindow / fundingPaymentsInWindow
once getTradesSince / getFundingPaymentsSince existed; null says so.

Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
… reach

A window from 0 (history report / compare with no period) listed ~20 000
day slices before the 60-request cap stopped it; only the newest
ASTER_INCOME_MAX_PAGES are built now. Results are unchanged.

Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
…e errors; the cost of a whole-history report

Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
@github-actions

Copy link
Copy Markdown

⚠️ Large PR — 1240 changed lines across 17 files (lockfile and snapshots excluded).

If it holds more than one logical change, consider splitting it for review.

Hiksang and others added 2 commits November 15, 2026 18:22
Conflicts: CHANGELOG.md (both sides kept); portfolio.ts (main's
openOrdersForView with this branch's windowed 24 h funding read).

Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
@Hiksang
Hiksang merged commit c86bcb2 into main Nov 15, 2026
4 checks passed
@Hiksang
Hiksang deleted the fix/period-totals branch November 15, 2026 09:39
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant