Fix History tab £0 recovered: work around T212 pagination dropping orders#63
Open
ko5tas wants to merge 1 commit into
Open
Fix History tab £0 recovered: work around T212 pagination dropping orders#63ko5tas wants to merge 1 commit into
ko5tas wants to merge 1 commit into
Conversation
…ders Trading212's unfiltered /equity/history/orders endpoint silently drops orders in certain time windows for some accounts (verified gap up to 112 days for this account). The same orders ARE returned when queried with ?ticker=X, so the workaround is a per-ticker reconciliation pass. Changes: - Per-ticker reconciliation: after the unfiltered fetch builds the returns map, identify closed-position candidates (BUYs in cache, no SELLs/divs, not in current open positions) and fetch each via ?ticker=X to fill the gap. Cost is bounded by the number of mismatched closed positions, typically a handful. - Gap detection log: warn when consecutive orders in the unfiltered stream are >30 days apart, so future T212 regressions surface immediately. - cacheDir(): honor systemd's $CACHE_DIRECTORY env var first, and skip $HOME=/nonexistent (the placeholder for system users) so the cache lands somewhere actually writable when t2 runs as a service. - Optional backup_dir config field: mirror orders.json/dividends.json to a second location after each successful save (typically a Dropbox/OneDrive/rclone-synced folder). On startup, if the primary cache is missing but the backup exists, t2 rehydrates from it. Useful because T212 may eventually purge older records and a pagination bug can drop them sooner. - Footer surface: show orders/dividends cache freshness timestamps and a "backup ✓" indicator next to "Last updated" so cache state is visible without reading logs. - README: document CacheDirectory=t2 / StateDirectory=t2 systemd directives and the new backup_dir option. Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
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.
Summary
/equity/history/ordersendpoint silently drops orders in certain time windows for some accounts. The same orders ARE returned when queried with?ticker=X. After the unfiltered fetch, t2 now identifies "closed-position candidates" (tickers with BUYs but no SELLs/divs and not in current open positions) and fetches each per-ticker to fill the gap.cacheDir()now honors\$CACHE_DIRECTORY(set by systemdCacheDirectory=t2) first, and skips\$HOME=/nonexistent(the placeholder for system users) so the cache lands somewhere actually writable. Without this, t2 silently writes orders.json to the working directory (/), every restart re-fetches ~1,100 orders from T212, and ~4 minutes of warmup is required before any history is visible.backup_dirconfig field mirrors the cache to a second location after each successful save. Pointed at a Dropbox/OneDrive/rclone-synced folder, this keeps an off-machine archive of trading history (T212 may eventually purge old records, and the pagination bug above can drop them sooner). On startup, if the primary cache is missing but the backup exists, t2 rehydrates from it.Last updatednow also shows orders/dividends cache freshness timestamps and abackup ✓indicator whenbackup_diris set, so cache state is visible without reading logs.Evidence
For the user's specific bug, the History tab went from:
Direct API verification (per-ticker query): T212 has 3 SELL orders for SHOP, 2 for PLTR, 1 for AMD that are absent from the unfiltered paginated stream but present when queried with
?ticker=X.The unfiltered stream has 7 detected gaps in the user's history, the largest being 112 days (Sep 5 – Dec 26, 2025).
Test plan
0 new orders (cache is current)andreconcile: no closed-position candidates need fillinglog on second startbackup_dirtest: cache mirrored to second directory, footer showsbackup ✓rehydrated 491 orders from backup_dirgo build ./...,go vet ./...,go test ./...all cleanCacheDirectory=t2andStateDirectory=t2to/usr/lib/systemd/system/t2.service, setbackup_dirin/etc/t2/config.yamlto a Dropbox-synced folder, restart, confirm History tab fixed🤖 Generated with Claude Code