feat: copy-trading replay & comparison system#1
Merged
Conversation
Adds SizingStrategy, Target, TradeComparison, Comparison, ReplayResult dataclasses and TargetNotFoundError, UsernameResolutionError, ReplayError error classes to support the copytrack feature.
Adds copytrack_targets and copytrack_replays tables to SCHEMA_SQL. Updates reset() to drop copytrack tables first (FK order) before re-creating the full schema.
Creates pm_trader/data_api.py with PolymarketDataClient and get_activity() method that fetches a single page of user activity from the Polymarket Data API, with optional time range and sort parameters.
…aClient Adds auto-paginating get_all_trades() for complete trade history retrieval, get_positions() for current user positions, and get_profile() for public profile lookup via the Gamma API.
Adds resolve_username() that scrapes the Polymarket profile page to extract a wallet address from the Next.js __NEXT_DATA__ payload, with in-memory caching to avoid duplicate requests.
Implements Task 7: creates pm_trader/copytrack.py with compute_replay_amount() supporting equal, proportional, and fixed sizing modes, with tests.
Implements Task 8: adds compute_original_stats() to copytrack.py, computing buy/sell counts, total invested/returned, PnL, and win rate from raw Polymarket Data API trade records.
Implements Task 9: adds replay() to copytrack.py that replays a target user's trades directly into the paper trader DB at original prices, handles buy/sell sizing modes, skips on insufficient balance or missing position, and produces a full ReplayResult with per-trade and aggregate comparisons.
Add copytrack command group with add (by @username or 0x address), list (all tracked targets), and remove subcommands. Includes 5 tests.
Add trades command to fetch user trade history from Data API, and replay command to replay a target's trades through the paper trader with comparison stats. Includes 2 new tests.
Add compare subcommand to view the latest replay comparison for a tracked target, including original vs replay stats and per-trade breakdown. Includes 1 new test.
Add /api/copytrack/targets endpoints (GET, POST, DELETE) for managing copy-trade targets via the web API. Register copytrack router in app.py.
Add POST /api/copytrack/replay to trigger copy-trade replays,
GET /api/copytrack/replay/{id} to fetch results, and
GET /api/copytrack/chart/{id} for P&L curve data.
Replace deprecated /closed-positions API with /activity endpoint (TRADE + REDEEM). Replay engine now processes BUY, SELL, and REDEEM activities chronologically. REDEEM win/loss determined by API usdcSize (>0 = won, 0 = lost). - Add REDEEM handling: winning positions get cash back, losing positions zeroed out - Add `type` column to copytrack_raw_trades for TRADE/REDEEM deduplication - Update limit parameter to count by market (conditionId), not individual activities - Add Copytrack frontend page with per-trade comparisons and charts - Add open market indicators (yellow bg + ⏳) for unredeemed positions - Add replay history pagination (show 5 by default) - Add live E2E test verifying limit=1 aligns with latest real activity - 818 tests pass, 100% coverage
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
/activityAPI (TRADE + REDEEM) instead of deprecated/closed-positionsusdcSize(>0 = won, 0 = lost); winning positions get cash back, losing positions zeroed outcopytrack add/list/remove/trades/replay/compareTest plan
limit=1aligns with latest real activity for @dannyhqc