Skip to content

Golrypavium/openclaw-polymarket-trading-bot

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

16 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

OpenClaw Polymarket AI Trading Bot

image

AI-powered prediction bot for Polymarket’s 5-minute Bitcoin Up/Down markets. Built with TypeScript, combines momentum, volatility, whale flow, and optional LLM scoring to predict direction and place real CLOB orders.

OpenClaw Polymarket AI Trading Bot is a production-ready TypeScript bot that predicts whether Polymarket’s 5-minute BTC Up/Down markets will move up (YES) or down (NO) and executes real orders via the Polymarket CLOB API. Set your CLOB credentials in .env and run.


✨ Features

  • 🤖 AI-enhanced predictions — Optional OpenAI (or compatible) LLM scoring for an extra bias signal on top of momentum and whale flow
  • 🐋 Whale flow analysis — Tracks large traders (≥ $200 notional) and incorporates net YES/NO bias into the model
  • ⚡ Real-time execution — Places FOK market orders when the signal crosses your edge threshold
  • 📊 Signal Lab dashboard — Web UI to fetch predictions, run auto-compare (forecast vs outcome), and track accuracy over time
  • ⏱️ Timed position close — Optional auto-sell after N seconds via CLOSE_AFTER_SECONDS
  • 🔒 Startup validation — Validates wallet key, CLOB credentials, and API URLs before running

📋 What does this bot do?

  1. 📍 Picks a market
    Finds the current active “BTC up or down in 5 minutes” market on Polymarket (Gamma API + time bucket, with fallbacks via Data API and active-market scan).

  2. 📊 Collects data every 15 seconds

    • Latest YES price from the order book
    • Short-term price moves (e.g. last ~30 seconds, ~2 minutes)
    • Recent whale flow (large trades) on that market
  3. 🔮 Makes a prediction
    Combines:

    • Momentum (recent returns)
    • Volatility (recent price range)
    • Whale bias (whether big traders are buying YES or NO)
    • Optional LLM bias (if you set an OpenAI API key)

    Into a single number: probability that YES goes up in 5 minutes (pUp5m).

  4. ⚖️ Decides an action

    • If pUp5m is clearly above 0.5 (e.g. > 0.53) → OPEN YES
    • If clearly below 0.5 (e.g. < 0.47) → OPEN NO
    • Otherwise → HOLD ⏸️
      Thresholds are set by EDGE_THRESHOLD in .env.
  5. 💰 Executes
    Places real market BUY orders when the signal is OPEN YES/NO. Records open positions in positions.json and can optionally close after CLOSE_AFTER_SECONDS (timed market sell).


⚡ Quick start

1. Install and config

cd openclaw-ai-polymarket-trading-bot
npm install
cp .env.example .env

Edit .env and set CLOB credentials (see Environment variables below).

2. Run the bot

npm run dev

Or after a build: npm run build && npm start (runs dist/entry.js).

The bot places real orders when the signal is OPEN YES/NO. It logs e.g. LIVE BUY orderID=... and records positions in positions.json. If CLOSE_AFTER_SECONDS is set (e.g. 300), positions are closed with a market sell after that many seconds.

3. Run the Signal Lab dashboard

In another terminal:

npm run serve

Open http://localhost:8787 in your browser.

  • Get Prediction — Fetches the same snapshot the bot uses (market, current YES price, prediction, whale stats).
  • Auto Compare — Set “Entry YES price” and “Settle after (sec)” (e.g. 300 for 5 min). The UI waits, then fetches the new YES price and records whether the predicted side (YES/NO) would have been correct.
  • History — Table of past comparisons and accuracy (e.g. “Total: 10 | Correct: 6 | Accuracy: 60%”).

🔧 Environment variables

Copy .env.example to .env and adjust as needed.

Variable Description Default
Data sources
POLYMARKET_REST_BASE Gamma API base URL https://gamma-api.polymarket.com
BINANCE_REST_BASE Binance Futures REST URL https://fapi.binance.com
CLOB (required)
PRIVATE_KEY Wallet private key (hex) (required)
CLOB_API_KEY From Polymarket CLOB “create or derive API key” (required)
CLOB_SECRET Same (required)
CLOB_PASS_PHRASE Same (required)
CLOB_API_URL CLOB API base https://clob.polymarket.com
CLOB_CHAIN_ID Chain ID (Polygon mainnet) 137
CLOSE_AFTER_SECONDS Close positions with market sell after N seconds (0 = hold to resolution) 0
Optional LLM
OPENAI_API_KEY If set, features are sent to the LLM for an extra bias signal (empty = no LLM)
OPENAI_BASE_URL OpenAI-compatible API base https://api.openai.com/v1
OPENAI_MODEL Model name gpt-4o-mini
Runtime
LOOP_SECONDS Seconds between each cycle 15
MAX_POSITION_USD Size in USD per position 100
EDGE_THRESHOLD Min edge to open: |pUp5m - 0.5| > this (e.g. 0.03 → open if pUp5m > 0.53 or < 0.47) 0.03

On startup the bot runs an environment check: valid PRIVATE_KEY (64 hex), real CLOB keys (not placeholders), sensible LOOP_SECONDS / MAX_POSITION_USD / EDGE_THRESHOLD, and valid API URLs. It exits with a clear error list if anything is wrong. Open positions live in positions.json (gitignored).


📁 Project structure

Path Role
core/entry.ts Entry point: run cycle every N seconds, fetch data → indicators → forecast → place orders
core/settings.ts Reads .env, exposes opts
core/setupGuard.ts Startup validation for bot (validateSetup) and dashboard (validateDashboard)
core/schema/defs.ts Shared types: PricePoint, WhaleActivity, IndicatorSet, Forecast, LiveBookEntry, etc.
core/adapters/eventSource.ts Gamma API (market resolution, YES price) + Data API (whale flow). getCondition() for CLOB orders
core/adapters/orderBridge.ts CLOB client wrapper: submitOrder, submitBuy, submitSell, resolveTokenPair
core/strategy/indicators.ts Builds indicator set from ticks + whale (returns, vol, whale bias/intensity)
core/strategy/forecast.ts Combines indicators + LLM bias → pUp5m, confidence
core/strategy/simulator.ts Strategy: given forecast + current price → HOLD / OPEN YES / OPEN NO
core/strategy/book.ts Persisted live positions (positions.json): add, remove, list expired for timed sell
core/ai/remoteScorer.ts Optional: calls OpenAI (or compatible) API with indicators, returns bias in [-1, 1]
core/dashboard.ts Serves the Signal Lab UI and /api/prediction
web/ Static Signal Lab UI (HTML, JS, CSS)

🛠️ Scripts

Command Description
npm run dev Run the bot with tsx (requires CLOB credentials in .env)
npm run serve Start the Signal Lab dashboard on port 8787
npm run build Compile TypeScript to dist/
npm start Run compiled bot: node dist/entry.js

⚠️ Important notes

  • Credentials — The bot requires PRIVATE_KEY, CLOB_API_KEY, CLOB_SECRET, and CLOB_PASS_PHRASE; it exits at startup if any are missing.
  • Market selection — The bot always picks the current 5-minute BTC up/down market (by time bucket or recent trades).
  • Whale flow — Built from public trade data. “Whales” = wallets with ≥ $200 notional in the sampled window.
  • No guarantees — This is a heuristic/experimental strategy. Trade at your own risk and only with money you can afford to lose.
  • Selling / closing — Set CLOSE_AFTER_SECONDS (e.g. 300) to auto-close positions with a market sell after N seconds. If 0, positions are held to market resolution.

📬 Contact

Developer: Telegram @bitship1_1


OpenClaw Polymarket AI Trading Bot — AI-powered prediction and execution for Polymarket.

About

Openclaw Polymarket Trading AI Bot

Topics

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

 
 
 

Contributors