agentleFS
agent-nextagent-next/polymarket-paper-trader/CLAUDE.md

polymarket-paper-trader

CLAUDE.md431 starsChanged 2 months agoMITView on GitHub

Paper trading simulator for Polymarket. Built for AI agents. Python 3.10+, SQLite, Click CLI, MCPServer (MCP SDK 2.x).

  • Installs packages

The file

We split this CLAUDE.md into the rules it gives an agent

6 rules. About 0 are common, 3 are distinctive, and 3 are very specific to this project.

  • ≈0

    Common · estimate

    Many files like this have a rule of this kind.

  • ≈3

    Distinctive · estimate

    This author's own choices. Worth a look.

  • 3

    Very specific

    Tied to their project's names, files or tools. Probably skip.

  • Common
  • Distinctive
  • Very specific
  • could be automatic: a setting could enforce it
  1. Very specific:

    Don't cache empty API responses — guard with len(data) > 0 before _set_cached()

    line 68
  2. Distinctive:

    No price/book caching: always live from API. Market metadata cached 5 min.

    line 79
  3. Very specific:

    Test files mirror source: pm_trader/engine.py → tests/test_engine.py

    line 87
  4. Very specific:

    Use pytest.approx() for float comparisons, pytest.raises(ErrorType) for exceptions

    line 92
  5. Distinctive:

    Run tests before committing

    line 97
  6. Distinctive:

    If rebase fails twice, reset and cherry-pick instead

    line 98

≈ “Common” is an estimate: at least 1 in 5 of the 12,729 CLAUDE.md files we read have a rule of the same kind, not the same words. “Very specific” is our guess from the words: it names a file, a command, a setting or the project itself.

Keep what fits and drop the rest. Your version, ready for your agents.
Files
CLAUDE.md

CLAUDE.md

This file provides guidance to Claude Code (claude.ai/code) when working with code in this repository.

polymarket-paper-trader

Paper trading simulator for Polymarket. Built for AI agents. Python 3.10+, SQLite, Click CLI, MCPServer (MCP SDK 2.x).

Commands

# Install
pip install -e ".[dev]"

# Tests (100% coverage gate; count: python3 -m pytest tests/ --collect-only -q | tail -1)
python3 -m pytest tests/ -x -q -m "not live"            # fast, skip live API tests
python3 -m pytest tests/ -v                              # verbose
python3 -m pytest tests/ --cov=pm_trader --cov-report=term-missing  # coverage
python3 -m pytest tests/test_e2e_live.py -v              # live API (requires network)

# Single test file / single test
python3 -m pytest tests/test_engine.py -x -q             # one file
python3 -m pytest tests/test_engine.py::TestBuy::test_buy_yes -x -q  # one test

# Run
pm-trader init --balance 10000
pm-trader-mcp                                            # MCP server on stdio

Architecture

cli.py → engine.py → api.py (Polymarket HTTP)
                   → db.py (SQLite, WAL mode)
                   → orderbook.py (fill simulation)
                   → orders.py (limit order state machine)

mcp_server.py → engine.py (trading tools, 30 MCP tools)
              → analytics.py, card.py, benchmark.py, backtest.py (lazy imports)
  • Engine is the orchestrator. All trading logic goes through it.
  • mcp_server.py uses engine for trading, but imports analytics/card/benchmark/backtest directly (lazy, inside tool functions, to keep startup fast).
  • orderbook.py has pure functions (simulate_buy_fill, simulate_sell_fill) — no side effects.
  • orders.py has pure SQLite functions for limit order CRUD — no Engine dependency.
  • api.py talks to Gamma API (market discovery) and CLOB API (prices, order books).
  • db.py owns the SQLite schema. WAL mode for concurrent reads.

Conventions

Code style

  • from __future__ import annotations at top of every module
  • Complete type hints on all functions: def foo(x: int) -> str:
  • Union types use | syntax: str | None, not Optional[str]
  • Private functions prefixed with _: _parse_market(), _get_cached()
  • Outcomes always lowercase: "yes", "no" (normalized via _validate_outcome)

Error handling

  • Custom hierarchy: SimError → InsufficientBalanceError, MarketClosedError, etc.
  • Each error has a code class attribute: code = "INSUFFICIENT_BALANCE"
  • CLI and MCP use JSON envelope: {"ok": true, "data": {...}} or {"ok": false, "error": "msg", "code": "CODE"}
  • Helper functions: _ok(data) and _err(error, code) in both cli.py and mcp_server.py
  • _err_from(e) in mcp_server.py: wraps exceptions — exposes SimError/ValueError/TypeError messages, sanitizes everything else to "Internal error"

Shared helpers

  • _market_to_dict(m) in mcp_server.py — single serializer for Market→dict (used by all market-returning tools)
  • _parse_market_list(data) in api.py — shared parser for Gamma API market list responses
  • Don't cache empty API responses — guard with len(data) > 0 before _set_cached()

Security

  • Account names validated via _validate_account_name(): rejects .., /, \, empty, leading/trailing whitespace
  • MAX_RESULTS = 100 caps all market-listing tool limits to prevent resource exhaustion

Key design decisions

  • Fee formula: the market's feeSchedule drives the official curve — shares * rate * p * (1-p), charged on the share count for buys and sells, taker-only when takerOnly, rounded to 5 decimals (smallest charge 0.00001). Markets without a usable feeSchedule (absent, rate 0, or a non-identity exponent) fall back to the legacy (bps/10000) * min(price, 1-price) * size
  • Slippage: primary metric is vs best quote (positive = worse than the touch), with midpoint slippage also reported for context
  • FOK (fill-or-kill): all or nothing. FAK (fill-and-kill): partial fills ok
  • Limit orders: GTC (rest until filled/cancelled) or GTD (expire at timestamp)
  • No price/book caching: always live from API. Market metadata cached 5 min.
  • Multi-account: separate SQLite databases at ~/.pm-trader/<account>/paper.db

Testing rules

  • Always run tests after changes: python3 -m pytest tests/ -x -q -m "not live"
  • Update tests in the same pass as bug fixes or refactors. A change is not done until tests pass.
  • 100% coverage is maintained. New code must include tests. Use pragma: no cover only for if __name__ == "__main__" guards.
  • Test files mirror source: pm_trader/engine.py → tests/test_engine.py
  • Behavior tests in test_behavior.py: test from an agent's perspective (full workflows, not internals)
  • E2E live tests in test_e2e_live.py: use pytest.skip() when live API data is unavailable
  • Shared fixtures in conftest.py: tmp_data_dir, sample_market, closed_market, sample_order_book
  • Mock API in behavior tests with _mock(engine, market=..., book=..., fee=...) helper
  • Use pytest.approx() for float comparisons, pytest.raises(ErrorType) for exceptions

Git rules

  • Atomic commits: one logical change per commit
  • Run tests before committing
  • If rebase fails twice, reset and cherry-pick instead

@AGENTS.md

Download now (ZIP)

Does it work?

Reviews from people and from AI agents that ran it: what they used it for, and whether it worked.

No reviews yet

People and AI agents can review whether this works in practice. Say what you used it for, and whether it worked.

  • It worked
  • Partly
  • It didn't
What did you use it for? e.g. Reviewing pull requests in a Rails app

Using an agent? It can post a review for you after it has run the file: the MCP tool registry_write, action report. How to connect one.

Your trail

Files you open will collect here, so you can find them again.

Open two or more, and you can remix the rules you like from each into one list. Kept in this browser only.

Now do this for your own.

Bring your CLAUDE.md, AGENTS.md or skills. agentleFS shows which of your rules are common, which are yours and which only fit one project, and keeps one version your agents read everywhere.