Skip to content

feat: copy-trading replay & comparison system#1

Merged
qhuang20 merged 19 commits into
mainfrom
feature/copytrack
Mar 15, 2026
Merged

feat: copy-trading replay & comparison system#1
qhuang20 merged 19 commits into
mainfrom
feature/copytrack

Conversation

@qhuang20

@qhuang20 qhuang20 commented Mar 13, 2026

Copy link
Copy Markdown
Member

Summary

  • Copy-trading replay system — fetch any Polymarket user's trade history, replay it through the paper trader, produce per-trade + aggregate comparisons
  • Activity-based replay — uses /activity API (TRADE + REDEEM) instead of deprecated /closed-positions
  • REDEEM support — win/loss determined by API usdcSize (>0 = won, 0 = lost); winning positions get cash back, losing positions zeroed out
  • data_api.py — Polymarket Data API client with activity fetching, auto-pagination, profile lookup, @username resolution
  • copytrack.py — replay engine with 3 sizing strategies (equal, proportional, fixed), limit by market count
  • Copytrack frontend page — per-trade comparisons, P&L charts, open market indicators, replay history pagination
  • CLI commands: copytrack add/list/remove/trades/replay/compare
  • Web API routes: targets CRUD, fetch, replay trigger, replay results, chart data

Test plan

  • 818 non-live tests pass, 100% coverage
  • Edge cases: zero price, no position, unknown side, insufficient balance, position merge, proportional/fixed sell
  • REDEEM: winning redeem, losing redeem, win+loss combo, no-position skip
  • Live E2E: limit=1 aligns with latest real activity for @dannyhqc
  • Manual: full replay against live API

qhuang20 added 19 commits March 13, 2026 16:23
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
@qhuang20
qhuang20 merged commit c0283c3 into main Mar 15, 2026
3 of 4 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant