Polymarket — Бот исследований и торговли
Version 0.2.0 · MIT License · Python 3.9+
A production-grade, AI-powered autonomous trading agent for Polymarket prediction markets. It discovers markets, gathers authoritative evidence from whitelisted sources, generates calibrated probability forecasts using an LLM ensemble, enforces strict risk controls, and executes trades with fractional Kelly sizing.
Ключевые возможности
Обнаружение рынков
Auto-scans Polymarket's Gamma API for markets matching volume, liquidity, spread, and category filters.
Исследование на основе данных
Fetches sources from whitelisted authoritative domains (bls.gov, sec.gov, etc.) with LLM-powered evidence extraction.
Ансамбль нескольких моделей
Queries GPT-4o, Claude 3.5 Sonnet, and Gemini 1.5 Pro in parallel with adaptive weighting and Platt calibration.
Движок рисков (15 проверок)
Kill switch, drawdown management, portfolio limits, min edge, evidence quality gates, and more. ALL must pass.
Размер позиции по Kelly
Fractional Kelly criterion with confidence scaling, drawdown heat multiplier, and liquidity-aware caps.
Отслеживание китов
Monitors top Polymarket leaderboard wallets for smart-money conviction signals to boost or penalize edge.
Архитектура
The bot follows a modular pipeline architecture with clear separation of concerns:
┌─────────────────────────────────────────────────────────────────────┐ │ CLI (Click) / Dashboard (Flask) │ │ scan │ research │ forecast │ paper-trade │ trade │ engine │ dash │ ├───────┴──────────┴──────────┴─────────────┴───────┴────────┴───────┤ │ │ │ Connectors Research Forecast │ │ ┌──────────────┐ ┌───────────────┐ ┌───────────────────┐ │ │ │ Gamma API │ │ Query Builder │ │ Feature Builder │ │ │ │ CLOB API │───▶│ Source Fetcher│───▶│ LLM Forecaster │ │ │ │ Web Search │ │ Evidence Ext. │ │ Ensemble (3 LLMs) │ │ │ │ WebSocket │ └───────────────┘ │ Calibrator │ │ │ │ Rate Limiter │ └───────────────────┘ │ │ └──────────────┘ │ │ │ ▼ │ │ Policy Analytics │ │ ┌─────────────────────┐ ┌──────────────────────────┐ │ │ │ Edge Calculator │ │ Regime Detector │ │ │ │ Risk Limits (15) │◀─────▶│ Calibration Feedback │ │ │ │ Position Sizer │ │ Adaptive Model Weights │ │ │ │ Drawdown Manager │ │ Smart Entry Calculator │ │ │ │ Portfolio Risk │ │ Wallet Scanner │ │ │ │ Timeline Intel │ └──────────────────────────┘ │ │ └─────────────────────┘ │ │ │ │ │ ▼ │ │ Execution Storage / Observability │ │ ┌─────────────────────┐ ┌──────────────────────────┐ │ │ │ Order Builder │ │ SQLite + WAL + Migrations│ │ │ │ Order Router (dry) │ │ structlog JSON Logging │ │ │ │ Fill Tracker │ │ In-Process Metrics │ │ │ │ Cancel Manager │ │ Alerts (Telegram/Slack) │ │ │ │ TWAP / Iceberg │ │ JSON Run Reports │ │ │ └─────────────────────┘ └──────────────────────────┘ │ └─────────────────────────────────────────────────────────────────────┘
Module Map
| Package | Purpose | Key Files |
|---|---|---|
src/connectors/ | External API integrations | polymarket_gamma.py, polymarket_clob.py, web_search.py, ws_feed.py |
src/research/ | Evidence gathering pipeline | query_builder.py, source_fetcher.py, evidence_extractor.py |
src/forecast/ | Probability forecasting | ensemble.py, llm_forecaster.py, calibrator.py, feature_builder.py |
src/policy/ | Risk & sizing decisions | risk_limits.py, position_sizer.py, edge_calc.py, drawdown.py, portfolio_risk.py |
src/execution/ | Order management | order_builder.py, order_router.py, fill_tracker.py, cancels.py |
src/engine/ | Continuous trading loop | loop.py, market_filter.py, market_classifier.py, event_monitor.py |
src/analytics/ | Self-improving analytics | regime_detector.py, adaptive_weights.py, calibration_feedback.py, smart_entry.py, wallet_scanner.py |
src/storage/ | Persistence layer | database.py, models.py, migrations.py, cache.py, audit.py |
src/observability/ | Logging & monitoring | logger.py, metrics.py, alerts.py, reports.py, sentry_integration.py |
src/dashboard/ | Web UI | app.py, templates/index.html, static/dashboard.js, static/style.css |
Торговый пайплайн
Every trading cycle follows this deterministic pipeline. Each step must succeed for the next to proceed:
Обнаружение рынков
Gamma API scan → filter by volume, liquidity, spread, category → classify market type
Research & Evidence Gathering
Build site-restricted queries → fetch from whitelisted sources → LLM extracts structured evidence with citations
Feature Engineering
30+ features from market data, orderbook, trade history, and evidence quality scores
LLM Ensemble Forecast
GPT-4o + Claude 3.5 Sonnet + Gemini 1.5 Pro queried in parallel → trimmed mean aggregation
Калибровка
Platt scaling shrinks extremes toward 0.5 → low-evidence penalty → contradiction penalty → historical calibration learning
Расчёт Edge
Edge = model_prob − implied_prob → deduct transaction costs → determine BUY_YES or BUY_NO direction
Risk Check (15 rules)
Kill switch, drawdown, max stake, daily loss, open positions, min edge, liquidity, spread, evidence quality, confidence, implied prob floor, edge direction, market type, portfolio exposure, timeline
Размер позиции
Fractional Kelly × confidence × drawdown heat × timeline × volatility × regime × category multipliers
Исполнение ордеров
Build limit/market/TWAP/iceberg order → route through triple dry-run gate → submit to CLOB or simulate
Market Discovery & Classification
The bot discovers markets through Polymarket's Gamma REST API and automatically classifies them by keyword matching.
Scanning Filters
| Filter | Default | Config Key | Description |
|---|---|---|---|
| Minimum Volume | $1,000 | scanning.min_volume_usd | Markets below this total volume are skipped |
| Minimum Liquidity | $500 | scanning.min_liquidity_usd | Current available liquidity threshold |
| Maximum Spread | 8% | scanning.max_spread | Markets with wide bid-ask spreads are illiquid |
| Max Days to Expiry | 120 | scanning.max_days_to_expiry | Skip markets too far from resolution |
| Batch Size | 100 | scanning.batch_size | Markets fetched per API call |
| Research Cooldown | 60 min | scanning.research_cooldown_minutes | Don't re-research the same market within this window |
Market Type Classification
Markets are classified by matching keywords in the question text:
| Тип | Example Keywords | Primary Sources |
|---|---|---|
| MACRO | CPI, inflation, GDP, unemployment, Fed, FOMC | bls.gov, federalreserve.gov, fred.stlouisfed.org |
| ELECTION | election, vote, president, senate, poll | fec.gov, ballotpedia.org |
| CORPORATE | IPO, earnings, stock, SEC, merger | sec.gov, investor relations |
| WEATHER | hurricane, temperature, NOAA, wildfire | weather.gov, noaa.gov |
| SPORTS | NFL, NBA, Super Bowl, championship | espn.com, sports-reference.com |
Pre-Research Filter
Before spending API credits on research, markets are scored (0-100) on volume, liquidity, recency, and category. Markets scoring below filter_min_score (default 25) are skipped. Markets of type UNKNOWN are blocked entirely.
Движок исследований
The research module implements a source-first, whitelisted search pipeline to ensure evidence comes from authoritative sources.
Query Strategy (4 phases)
site:bls.gov CPI January 2026)Evidence Extraction
An LLM processes fetched sources to extract structured evidence. Every bullet must include:
{
"text": "CPI-U increased 3.1% YoY in January 2026",
"metric_name": "CPI-U YoY",
"metric_value": "3.1",
"metric_unit": "percent",
"metric_date": "2026-01-31",
"confidence": 0.97,
"citation": {
"url": "https://www.bls.gov/...",
"publisher": "Bureau of Labor Statistics",
"authority_score": 1.0
}
}
Evidence Quality Scoring
Quality is computed independently of LLM self-assessment using:
- Source recency — Penalty for stale data (>7 days old, severe >30 days)
- Domain authority — Government sources score 1.0, news outlets 0.6-0.8
- Cross-source agreement — Multiple sources confirming the same metric
- Numeric density — Bonus for evidence with concrete numbers, dates, and units
- Contradiction detection — Conflicting sources reduce confidence
Search Providers
| Provider | Env Variable | Notes |
|---|---|---|
| Tavily | TAVILY_API_KEY | Preferred; purpose-built for research agents |
| SerpAPI | SERPAPI_KEY | Google search results |
| Bing | BING_API_KEY | Microsoft Bing search |
| Fallback | Any available | Tries each provider in order (default mode) |
Blocked Domains
The following domains are always excluded: wikipedia.org, reddit.com, medium.com, twitter.com/x.com, quora.com, and other user-generated content sites.
Прогнозирование и ансамбль
The forecasting system queries multiple Large Language Models in parallel to produce probability estimates.
Ensemble Architecture
| Model | Provider | Default Weight | Env Variable |
|---|---|---|---|
| GPT-4o | OpenAI | 40% | OPENAI_API_KEY |
| Claude 3.5 Sonnet | Anthropic | 35% | ANTHROPIC_API_KEY |
| Gemini 1.5 Pro | 25% | GOOGLE_API_KEY |
Aggregation Methods
- Trimmed Mean (default) — Removes the highest and lowest forecasts, averages the rest. Robust to outlier models.
- Median — Takes the middle forecast. Completely resistant to outliers.
- Weighted — Uses per-model weights (static or adaptive). Best when model quality is known.
Graceful Degradation
If some models fail (API errors, timeouts), the ensemble degrades gracefully. It requires min_models_required (default 1) to succeed. If all models fail, it falls back to a single-model query to fallback_model (default: GPT-4o).
Prompt Structure
Each model receives: the market question, market type, evidence summary, top evidence bullets, contradictions, and market features (volume, spread, implied probability, days to resolution). The model must return a probability (0-1), confidence level (LOW/MEDIUM/HIGH), reasoning, and invalidation triggers.
Adaptive Model Weights
The AdaptiveModelWeighter module tracks per-model, per-category Brier scores from resolved markets. It computes inverse-Brier weights so that models with better track records in a specific category receive more influence. Uses Bayesian smoothing to blend learned weights with priors, requiring 50+ samples for full confidence.
Калибровка
Raw LLM probabilities are systematically adjusted for better real-world calibration.
Platt Scaling (Default)
Applies logistic compression with shrinkage toward 0.5. This counteracts the tendency of LLMs to produce overconfident extreme probabilities.
calibrated = sigmoid(a × logit(raw) + b)
Where a < 1 provides shrinkage (pulling extremes inward) and b provides a base-rate shift.
Adjustments Applied
- Shrinkage — Extreme probabilities (>0.95 or <0.05) are pulled toward 0.5
- Low-Evidence Penalty — Up to 15% penalty when evidence quality is below threshold
- Contradiction Penalty — Each contradiction reduces confidence proportionally
- Ensemble Disagreement — Wide spread among models triggers additional shrinkage
Historical Calibration Learning
The CalibrationFeedbackLoop records every (forecast, outcome) pair when markets resolve. After 30+ samples, the bot trains a logistic regression to learn optimal calibration parameters from its own track record, becoming more accurate over time.
Расчёт Edge
Edge measures the difference between the bot's model probability and the market-implied probability.
raw_edge = model_probability − implied_probability
net_edge = |raw_edge| − transaction_cost_pct
Direction Logic
- If
model_prob > implied_prob→ BUY YES (the market underestimates probability) - If
model_prob < implied_prob→ BUY NO (the market overestimates probability)
Transaction Costs
Costs include entry fees (transaction_fee_pct, default 2%) and gas costs. The bot uses a hold-to-resolution model (single-leg entry, no exit trade), so only entry costs are deducted.
Expected Value
EV per dollar = (model_prob × $1 − cost) / cost. Only positive net-EV trades pass risk checks.
Управление рисками
The risk engine enforces 15 independent checks. A trade is only allowed if ALL pass. Any single violation blocks the trade.
Manual global halt; immediately stops all trading
Auto-kill when drawdown exceeds max (default 20%)
Reduces sizing at heat level 2+; blocks at level 3
No single trade exceeds max_stake_per_market ($50)
Cumulative daily losses capped at max_daily_loss ($500)
Maximum concurrent positions (default 25)
Net edge after costs must exceed min_edge (default 4%)
Market must have sufficient liquidity ($2,000 default)
Bid-ask spread must be under max_spread (default 6%)
Must meet min_evidence_quality threshold (default 0.55)
Rejects LOW confidence forecasts (requires MEDIUM+)
Blocks micro-probability markets (<5% implied)
Edge must be positive after costs in the chosen direction
Market category must be in allowed list (not restricted)
Category and event exposure within portfolio limits
Position Sizing (Kelly Criterion)
Position sizes are computed using the fractional Kelly criterion with multiple adjustment layers.
f* = (p × b − q) / b
Where: p = model probability, q = 1−p, b = odds = (1/cost) − 1
Adjustment Multipliers
| Multiplier | Source | Range | Purpose |
|---|---|---|---|
| Kelly Fraction | Config | 0.25 (quarter-Kelly) | Reduces full Kelly for real-world variance |
| Confidence | LLM Forecast | LOW=0.5, MED=0.75, HIGH=1.0 | Scale by forecast confidence |
| Drawdown Heat | DrawdownManager | 0.0 – 1.0 | Reduce sizing during drawdowns |
| Timeline | TimelineAssessment | 0.5 – 1.3 | Adjust for resolution timing |
| Volatility | Market microstructure | 0.0 – 1.0 | Reduce in volatile markets |
| Regime | RegimeDetector | 0.5 – 1.0 | Reduce in HIGH_VOLATILITY regime |
| Category | Config per-category | MACRO=1.0, CORP=0.75, ELEC=0.5 | Category-specific risk appetite |
Caps
max_stake_per_market— Hard cap per trade (default $50)max_bankroll_fraction— No trade exceeds 5% of bankroll- Liquidity cap — Stake capped at 5% of available market liquidity
Drawdown Management
The drawdown system tracks the equity curve and progressively reduces risk during losing periods.
Heat Levels
| Level | Drawdown % | Kelly Multiplier | Action |
|---|---|---|---|
| 0 (Cool) | <10% | 1.00 | Normal trading |
| 1 (Warning) | 10% | 0.50 | Position sizes halved |
| 2 (Critical) | 15% | 0.25 | Position sizes quartered |
| 3 (Kill) | 20% | 0.00 | All trading halted (kill switch) |
When auto_kill_at_max: true (default), the bot automatically engages the kill switch at max drawdown. This prevents catastrophic losses but requires manual reset to resume trading.
Portfolio Risk Management
Portfolio-level constraints prevent over-concentration and correlated losses.
| Constraint | Default | Description |
|---|---|---|
| Max Category Exposure | 35% | No single market category can exceed this % of bankroll |
| Max Event Exposure | 25% | No single event (grouped markets) can exceed this % |
| Max Correlated Positions | 4 | Limit on positions with >70% correlation similarity |
| Category Limits | MACRO: 40%, ELEC: 35%, CORP: 30% | Per-category exposure caps |
Исполнение ордеров
The execution layer builds orders and routes them through safety checks before submission.
Order Types
- Limit Orders — Default; placed at calculated price with TTL (default 300s)
- Market Orders — Immediate fill at current price; used for urgent entries
- TWAP — Time-Weighted Average Price; splits large orders into 5 slices over 30s intervals
- Iceberg — Hides order size; shows only 20% of total, refills as filled. Triggered at $500+ orders
Triple Dry-Run Gate
order.dry_runflag on the order object itselfconfig.execution.dry_runin config.yaml (default: true)ENABLE_LIVE_TRADINGenvironment variable (must be "true")
ALL THREE must allow live trading for any order to be submitted to the CLOB.
Smart Entry Timing
The SmartEntryCalculator calculates optimal entry levels using orderbook depth analysis, VWAP divergence, microstructure signals, and momentum. This can improve entry prices by 1-3% (10-30 basis points).
Position Exits
| Exit Trigger | Default | Description |
|---|---|---|
| Stop Loss | 20% | Exit when position loses 20% of entry value |
| Take Profit | 30% | Exit when position gains 30% |
| Max Holding Time | 240 hours | Auto-exit positions held longer than 10 days |
| Resolution / 100% | N/A | Exit when market resolves or price reaches 100% |
Continuous Trading Engine
The engine runs the full pipeline in a loop, coordinating all components autonomously.
Cycle (Default: every 3 minutes)
Between Cycles
- Check drawdown state and heat level
- Monitor position exits (stop-loss, take-profit, time-based, resolution)
- Scan whale wallet activity
- Detect market regime changes
- Persist engine state to DB for dashboard visibility
Analytics & Regime Detection
Market Regime Detector
Adapts strategy based on detected market conditions:
| Regime | Detection Signal | Strategy Adjustment |
|---|---|---|
| NORMAL | Stable spreads, moderate volume | Standard parameters |
| TRENDING | Directional price momentum | Favor momentum entries |
| MEAN_REVERTING | Oscillating prices | Contrarian entries, more patience |
| HIGH_VOLATILITY | Wide spreads, rapid price swings | Reduce sizes, raise edge threshold |
| LOW_ACTIVITY | Low volume, few markets | Conservative, fewer trades |
Calibration Feedback Loop
When markets resolve, the bot records (forecast_prob, actual_outcome) pairs and periodically retrains calibration. Per-model accuracy is tracked for adaptive weighting. This creates a self-improving system that gets more accurate over time.
Performance Tracking
The analytics module tracks: win rate, Brier score, Sharpe ratio, max drawdown, average edge, P&L by category, accuracy by confidence level, and more.
Whale / Smart-Money Scanner
The Whale Tracker is a comprehensive smart-money intelligence system that monitors the top 15 Polymarket leaderboard wallets and generates actionable signals. It provides real-time visibility into what the most profitable traders are doing.
Архитектура
The scanner runs on a 15-minute cycle within the bot's main engine loop:
- Phase 1 – Fetch: Queries positions for all 15 tracked wallets via the Data API (up to 200 positions each)
- Phase 2 – Score: Computes a composite quality score (0-100) per wallet based on P&L, win rate, and activity
- Phase 3 – Delta Detection: Compares current positions against the previous snapshot to identify new entries, exits, and size changes (>10% threshold)
- Phase 4 – Conviction Signals: Groups positions by market+outcome and generates conviction signals where 2+ whales agree
Smart Money Index (SMI)
A proprietary aggregate indicator (0-100) computed from all active whale signals, weighted by position size:
| SMI Range | Interpretation | Description |
|---|---|---|
| 70-100 | Very Бычьи | Сильные net positive whale conviction across markets |
| 60-70 | Бычьи | Умеренные positive bias from smart money |
| 40-60 | Neutral | Mixed signals — no clear directional consensus |
| 30-40 | Медвежьи | Умеренные negative bias from smart money |
| 0-30 | Very Медвежьи | Сильные net negative whale conviction |
SMI = (Σ bullish_conviction × position_usd) / (Σ all_conviction × position_usd) × 100. A value of 50 means equal bullish and bearish conviction-weighted capital.
Conviction Scoring
Each market where 2+ whales hold the same outcome receives a conviction score (0-100):
| Component | Weight | Description |
|---|---|---|
| Whale Count | 25 pts/whale | More whales on same market = higher conviction |
| Capital Size | log₁₀(USD) × 8 | Logarithmic scaling of total whale capital committed |
| Фактор прибыли | Up to 15 pts | Bonus when whales are in profit (entry < current price) |
Signal strength classification:
| Strength | Score Range | Dashboard Badge |
|---|---|---|
| 🟢 STRONG | ≥70 | Green pill — highest confidence signals |
| 🟡 MODERATE | 45-69 | Orange pill — worth monitoring |
| ⚪ WEAK | 30-44 | Gray pill — low confidence |
High Consensus Alerts
When 3+ whales agree on the same market outcome, a "High Consensus Alert" is triggered. These are the strongest smart money signals and appear as highlighted cards in the dashboard. The bot uses these for whale-edge convergence detection.
Чистый поток китов
Tracks aggregate capital entering vs. exiting whale positions:
- Flow In: Sum of $ from NEW_ENTRY and SIZE_INCREASE deltas
- Flow Out: Sum of $ from EXIT and SIZE_DECREASE deltas
- Net Flow: Flow In - Flow Out (positive = net accumulation, negative = net distribution)
Whale Tier System
Each tracked wallet is classified into a performance tier based on total P&L:
| Tier | P&L Threshold | Icon |
|---|---|---|
| 👑 LEGENDARY | ≥$2M | Gold crown |
| 💎 ELITE | ≥$1M | Purple diamond |
| ⭐ PRO | ≥$500K | Blue star |
| 🌱 RISING | <$500K | Green seedling |
Delta Detection
Position changes tracked between scan cycles:
| Action | Trigger | Feed Icon |
|---|---|---|
| NEW_ENTRY | Position appears for first time | 🟢 |
| EXIT | Position disappears from snapshot | 🔴 |
| SIZE_INCREASE | Position size grows >10% | 📈 |
| SIZE_DECREASE | Position size shrinks >10% | 📉 |
Dashboard Components
The Whale Tracker tab includes:
- 6 KPI Cards: Tracked Wallets, Conviction Signals, Smart Money Index, Net Whale Flow, Avg Win Rate, Top Conviction
- SMI Gauge: Visual gauge showing bullish/bearish sentiment with animated fill
- Flow Breakdown: Bar chart of entry/exit/increase/decrease activity
- Direction Distribution: Doughnut chart (bullish vs bearish signals)
- Top Markets by Whale Capital: Horizontal bar chart of most concentrated markets
- High Consensus Alerts: Highlighted cards for 3+ whale agreement
- Conviction Signals Table: Filterable table with conviction progress bars
- Live Activity Feed: Styled event feed of whale moves with icons
- Whale Leaderboard: Card-based leaderboard with tiers, scores, and activity counts
Edge Integration
| Scenario | Adjustment | Default |
|---|---|---|
| Whales agree with model | Edge boost | +8% |
| Whales disagree with model | Edge penalty | −2% |
| Whale + model convergence | Lower min_edge | Down to 2% |
The scanner monitors 15 wallets scraped from Polymarket's leaderboard, with combined P&L exceeding $18M. Wallet list is defined in wallet_scanner.py and can be customized.
Database Tables
| Table | Purpose |
|---|---|
tracked_wallets | Wallet metadata: address, name, P&L, win rate, score |
wallet_signals | Conviction signals per market+outcome (unique constraint) |
wallet_deltas | Position change events (entries, exits, size changes) |
API Endpoint
GET /api/whale-activity
Returns: tracked_wallets, conviction_signals, recent_deltas, high_consensus_signals, top_markets, and summary (including SMI, net flow, direction distribution, action breakdown, tier enrichment).
Decision Intelligence Log
A comprehensive audit trail of every decision the engine makes, from market scanning through trade execution. Provides full transparency into the AI's reasoning process.
What Gets Logged
- SCAN: Market discovery and initial filtering decisions
- RESEARCH: Evidence gathering, source quality, query construction
- FORECAST: Probability estimates from individual models and the ensemble
- EDGE: Edge calculations — market price vs. forecast probability
- RISK: Risk check passes/failures (drawdown, exposure, position limits)
- SIZE: Position sizing decisions (Kelly fraction, scaling)
- EXECUTE: Order submission, fill tracking, simulated execution
- MONITOR: Position monitoring, TP/SL proximity, exit triggers
Dashboard Tab Features
- Real-time feed of all decisions with timestamps
- Color-coded decision types (TRADE, NO_TRADE, SKIP, ERROR)
- Filter by stage (SCAN, RESEARCH, FORECAST, etc.)
- Search across all decision fields
- Integrity verification via SHA-256 hashes
- Summary cards: total decisions, trade rate, top categories
API Endpoint
GET /api/decision-log
Returns: entries (decision records with id, market_id, decision, stage, details, integrity_hash, timestamp), summary (total, by_stage breakdown, by_decision breakdown, recent_rate).
Data Integrity
Each decision entry includes a SHA-256 integrity_hash computed from the decision data, ensuring the audit trail cannot be tampered with. The dashboard shows a ✅ indicator for verified entries.
Стратегии и кошельки
A portfolio management system for organizing trading activity across multiple wallets and strategies, enabling multi-strategy allocation and performance tracking.
Core Concepts
| Concept | Description |
|---|---|
| Кошелёк | A funding source with a name, address (optional), initial balance, and P&L tracking |
| Стратегия | A named trading approach (e.g., "AI Trading", "Momentum", "Arbitrage") with its own risk parameters and description |
| Binding | A many-to-many link between strategies and wallets, controlling how capital is allocated across approaches |
Database Tables
| Table | Purpose |
|---|---|
wallets | Wallet metadata: name, address, balance, P&L, status, created/updated timestamps |
strategies | Strategy definitions: name, description, type, risk level, status |
strategy_wallets | Many-to-many binding with allocation percentage per pair |
wallet_trades | Trade records linked to specific wallets |
wallet_equity_snapshots | Point-in-time equity snapshots for equity curve charting |
API Endpoints
| Method | Endpoint | Description |
|---|---|---|
| GET | /api/strategies-overview | Full overview: wallets, strategies, bindings, summary stats |
| POST | /api/wallets | Create a new wallet |
| PUT | /api/wallets/<id> | Update wallet (name, balance, status) |
| DELETE | /api/wallets/<id> | Delete a wallet |
| POST | /api/strategies | Create a new strategy |
| PUT | /api/strategies/<id> | Update strategy details |
| DELETE | /api/strategies/<id> | Delete a strategy |
| POST | /api/strategy-wallets/bind | Bind a strategy to a wallet with allocation % |
| DELETE | /api/strategy-wallets/unbind | Unbind a strategy from a wallet |
Dashboard Tab
The Strategies tab provides a visual interface to:
- View all wallets with balance, P&L, status, and linked strategies
- View all strategies with risk levels, descriptions, and linked wallets
- Create, edit, and delete wallets and strategies via modal dialogs
- Bind/unbind strategies to wallets with allocation percentages
- Summary cards showing total capital, active wallets, total P&L, and average allocation
API коннекторы
Gamma API (Market Discovery)
The GammaClient connects to Polymarket's public market-listing API at https://gamma-api.polymarket.com. Used for: listing markets, fetching metadata (question, category, end date, resolution source), and getting pricing snapshots.
CLOB API (Orderbook & Trading)
The CLOBClient connects to https://clob.polymarket.com for real-time orderbook data (bids/asks), trade history, spread calculations, and order placement via the py-clob-client signing library.
WebSocket Feed
The WebSocketFeed subscribes to real-time price ticks for monitored markets, providing live data for the dashboard and microstructure analysis.
Rate Limiter
A global rate limiter prevents API abuse with configurable per-endpoint limits and automatic backoff on 429 responses.
Хранилище и БД
All data is persisted in a SQLite database with WAL (Write-Ahead Logging) mode for concurrent reads.
Tables
| Table | Purpose |
|---|---|
markets | Discovered market metadata (question, type, volume, liquidity) |
forecasts | All forecast results (probabilities, confidence, evidence quality) |
trades | Executed and simulated trade records |
positions | Open positions with entry price and current value |
closed_positions | Realized P&L with close reason (TP hit, SL hit, resolved, time exit) |
performance_log | Per-trade performance data for analytics |
calibration_history | Forecast vs. outcome pairs for calibration learning |
model_forecast_log | Per-model forecasts for adaptive weighting |
audit_log | Admin actions and configuration changes |
whale_scans | Wallet scanner results and conviction signals |
tracked_wallets | Whale tracker wallet metadata and scores |
wallet_signals | Per-market conviction signals from whale wallets |
wallet_deltas | Position change events (entries, exits, size changes) |
decision_log | Decision intelligence audit trail with integrity hashes |
wallets | Strategy wallet definitions (name, address, balance, P&L) |
strategies | Named trading strategies with risk parameters |
strategy_wallets | Many-to-many strategy↔wallet bindings with allocation % |
wallet_trades | Trade records linked to specific strategy wallets |
wallet_equity_snapshots | Point-in-time equity snapshots for performance tracking |
Migrations
Schema is managed through numbered migrations (currently 10). Migrations run automatically on startup, ensuring the database schema stays in sync with the code. Each migration is idempotent and includes proper error handling.
Backup
Database backups can be triggered from the Admin Panel. Backups are stored as timestamped copies in the data/ directory.
Наблюдаемость
Structured Logging (structlog)
All logs use JSON-structured format via structlog. Sensitive data (API keys, private keys) is automatically redacted by a custom log processor. Log levels: DEBUG, INFO, WARNING, ERROR, CRITICAL.
In-Process Metrics
The metrics module tracks counters, gauges, and histograms in-memory:
orders.simulated,orders.submitted,orders.filledforecasts.total,forecasts.trade,forecasts.no_traderisk.violations,risk.killsapi.calls,api.errors,api.latency_ms
Cost Tracking
LLM and search API costs are tracked per-provider with running totals visible in the Admin Panel. Includes token counts and estimated USD costs for OpenAI, Anthropic, Google, and search providers.
JSON Run Reports
Each engine cycle produces a JSON report saved to reports/ with full pipeline details.
Sentry Integration
Optional error tracking via Sentry SDK. Configure with SENTRY_DSN environment variable.
Алерты и уведомления
Multi-channel alerting for critical events:
| Channel | Config Keys |
|---|---|
| Console | Always enabled |
| Telegram | alerts.telegram_bot_token, alerts.telegram_chat_id |
| Discord | alerts.discord_webhook_url |
| Slack | alerts.slack_webhook_url |
alerts.email_smtp_host/port/user/password |
Alert Triggers
- Trade executions (paper and live)
- Large P&L moves
- Drawdown warnings and kill switch activations
- Risk limit breaches
- System errors
- Daily summary (configurable hour, default 6pm)
Руководство по дашборду
The web dashboard provides a real-time view of all bot operations, accessible at http://localhost:2345.
Tabs Overview
| Tab | Contents |
|---|---|
| Обзор | Portfolio summary cards, equity curve, engine status, drawdown gauge, regime indicator |
| Trading | Open positions with live P&L, pipeline candidates, recent forecasts, enhanced trade history with TP/SL hit tracking, close reasons, duration, PnL%, and summary stats |
| Analytics | Performance metrics (Sharpe, Sortino, Calmar), accuracy charts, P&L by category, strategy leaderboard |
| Whale Tracker | Tracked wallets with tiering, conviction signals, whale activity feed, Smart Money Index, consensus analysis |
| Decision Intel | Full decision audit trail — every stage (SCAN→RESEARCH→FORECAST→EDGE→RISK→SIZE→EXECUTE→MONITOR) with filtering, search, and integrity hashes |
| Strategies | Multi-wallet/strategy portfolio management — create wallets & strategies, bind them together, track allocation percentages and per-strategy performance |
| Journal | VaR calculations, watchlist, trade journal entries, equity snapshots |
| Admin Panel | Engine health, risk monitor, API keys, feature flags, config editor, audit trail, alerts, database stats, system info, danger zone actions |
| Documentation | This page — complete bot reference with 28 sections |
Authentication
Set DASHBOARD_API_KEY in your .env file. Access the dashboard with the API key as a header (X-API-Key) or query parameter (?api_key=...). If no key is set, the dashboard runs in open-access mode.
Auto-Refresh
Обновление каждые 15 сек — только для активной вкладки, чтобы минимизировать нагрузку на API.
Справочник конфигурации
All configuration is managed via config.yaml with environment variable overrides. The dashboard Admin Panel provides a visual config editor.
| Section | Key Settings | Description |
|---|---|---|
scanning | min_volume_usd, min_liquidity_usd, max_spread, preferred_types, restricted_types | Market discovery filters |
research | max_sources, search_provider, min_corroborating_sources, stale_days_penalty | Evidence gathering settings |
forecasting | llm_model, llm_temperature, min_evidence_quality, min_confidence_level | LLM forecasting parameters |
ensemble | models, aggregation, weights, min_models_required, timeout_per_model_secs | Multi-model ensemble config |
risk | max_stake_per_market, max_daily_loss, min_edge, kelly_fraction, bankroll, stop_loss_pct | Risk management thresholds |
drawdown | max_drawdown_pct, warning/critical thresholds, auto_kill_at_max | Drawdown heat system |
portfolio | max_category_exposure_pct, max_single_event_exposure_pct, max_correlated_positions | Portfolio-level risk limits |
timeline | near_resolution_hours, confidence_boost, exit_before_resolution_hours | Resolution timing adjustments |
microstructure | whale_size_threshold_usd, flow_imbalance_windows, depth_change_alert_pct | Market microstructure analysis |
execution | dry_run, slippage_tolerance, twap_enabled, iceberg_threshold, adaptive_pricing | Order execution settings |
engine | cycle_interval_secs, max_markets_per_cycle, paper_mode, auto_start | Trading engine loop settings |
alerts | telegram/discord/slack/email credentials, alert triggers, daily_summary_hour | Notification channels |
wallet_scanner | scan_interval, min_conviction_score, conviction_edge_boost/penalty | Whale tracking config |
cache | search_ttl_secs, orderbook_ttl_secs, max_cache_size_mb | Response caching |
storage | db_type, sqlite_path | Database settings |
observability | log_level, log_format, log_file, enable_metrics | Logging and monitoring |
CLI команды
The bot is controlled via the bot CLI (Click-based). Entry point: src.cli:cli.
| Command | Description | Key Flags |
|---|---|---|
bot scan | Discover active markets from Gamma API | --limit N |
bot research --market ID | Gather sources & extract evidence | |
bot forecast --market ID | Full pipeline: research → forecast → risk → size | |
bot paper-trade | Run paper trading simulation | --days N, --markets N |
bot trade --live | Live trading (requires ENABLE_LIVE_TRADING) | --live (required) |
bot engine start | Start the continuous trading engine | |
bot engine status | Show engine status summary | |
bot portfolio | Show portfolio risk report | |
bot drawdown | Show drawdown state | |
bot arbitrage | Scan for arbitrage opportunities | --limit N |
bot alerts | Show recent alerts | --limit N |
bot dashboard | Launch web dashboard | --host, --port, --debug |
Quick Start
# Install git clone <repo-url> polymarket-bot cd polymarket-bot python -m venv .venv && source .venv/bin/activate pip install -e ".[dev]" # Configure cp .env.example .env # Edit .env with API keys: OPENAI_API_KEY, TAVILY_API_KEY, etc. # Run bot scan --limit 20 # Discover markets bot forecast --market <CONDITION_ID> # Full forecast pipeline bot dashboard # Launch web UI on :2345
Безопасность
Always start with dry_run: true (default) and thoroughly test with paper-trade before enabling live trading.
Safety Mechanisms
- Triple dry-run gate — Three independent flags must all allow live trading
- Kill switch — Instant halt, accessible from CLI, config, and dashboard
- Auto-kill on max drawdown — Prevents catastrophic losses
- 15 independent risk checks — All must pass for any trade
- Position limits — Per-trade, per-category, and portfolio-wide caps
Security Practices
- API keys loaded from environment only, never committed to code
- Private keys automatically redacted in structlog output
- Dashboard auth via API key header or query parameter
- Docker — Runs as non-root user in container
- Audit trail — All admin actions and config changes logged to database
Развёртывание
Local Development
python -m venv .venv source .venv/bin/activate pip install -e ".[dev]" bot dashboard
Docker
docker compose build docker compose up -d # Runs dashboard + engine docker compose logs -f # Follow logs
Environment Variables
| Variable | Required | Description |
|---|---|---|
OPENAI_API_KEY | Yes | GPT-4o forecasting |
ANTHROPIC_API_KEY | Recommended | Claude 3.5 Sonnet (ensemble) |
GOOGLE_API_KEY | Recommended | Gemini 1.5 Pro (ensemble) |
TAVILY_API_KEY | Yes* | Web search provider (*or SERPAPI_KEY or BING_API_KEY) |
POLYMARKET_API_KEY | For live trading | Polymarket CLOB API credentials |
POLYMARKET_API_SECRET | For live trading | CLOB API secret |
POLYMARKET_PASSPHRASE | For live trading | CLOB API passphrase |
ENABLE_LIVE_TRADING | No | Must be "true" to allow live orders |
DASHBOARD_API_KEY | No | Protects dashboard access |
SENTRY_DSN | No | Sentry error tracking |
Testing
pytest # Run all tests pytest --cov=src --cov-report=term # With coverage pytest tests/test_policy.py -v # Specific test file mypy src/ # Type checking ruff check src/ tests/ # Linting
Глоссарий
| Term | Definition |
|---|---|
| Edge | The difference between model probability and market-implied probability; the bot's informational advantage. |
| Implied Probability | The probability implied by the current market price (best bid or mid-point). |
| Kelly Criterion | A mathematical formula for optimal bet sizing that maximizes long-term bankroll growth. |
| Fractional Kelly | Using a fraction (e.g., 25%) of full Kelly to reduce variance at the cost of slightly lower expected growth. |
| Просадка | The percentage decline from peak equity; measures how much has been lost from the highest point. |
| Уровень нагрева | A 0-3 scale representing drawdown severity; higher levels reduce position sizes and may halt trading. |
| Brier Score | A calibration metric: mean squared error between predicted probabilities and outcomes. Lower is better (0 = perfect). |
| Platt Scaling | A calibration technique using logistic regression to adjust raw probabilities for better real-world accuracy. |
| CLOB | Central Limit Order Book; Polymarket's orderbook for matching trades. |
| Gamma API | Polymarket's public REST API for market metadata and discovery. |
| TWAP | Time-Weighted Average Price; splits a large order into smaller slices executed over time. |
| Iceberg Order | An order that hides its full size, showing only a fraction to the market at a time. |
| Аварийный стоп | An emergency mechanism that immediately halts all trading activity. |
| Conviction Score | A whale tracking metric measuring how strongly top traders agree on a market direction. |
| WAL | Write-Ahead Logging; a SQLite journal mode that allows concurrent reads during writes. |
| Paper Mode | Simulation mode where all trades are recorded but no real orders are placed. |