Skip to content

MistKailorg/polymarket-trading-bot-cpp

Repository files navigation

Polymarket Trading Bot

A high-performance, C++17-based trading system for Polymarket prediction markets.
The main strategy targets Bitcoin Up/Down hourly markets with configurable entry, take‑profit, and stop‑loss rules, designed for low latency and 24/7 operation.

Key Features

  • Automated signal generation: Market entry based on configurable price ranges and remaining time to market close.
  • Risk management: Take‑profit and stop‑loss execution with tunable thresholds, plus an optional dedicated stop‑loss process.
  • Custom CLOB client: High‑performance C++ Polymarket CLOB SDK with EIP‑712 signing, Keccak‑256 hashing, and order‑book helpers.
  • Realtime data: WebSocket order‑book streaming for the active BTC markets.
  • Networking layer: Custom HTTP (fasthttp) and WebSocket (GGwebsocket) clients with keep‑alive and latency optimizations.
  • Notifications: Optional Telegram messages for trade alerts and periodic performance statistics.
  • Backtesting & analysis: Probability analyzer with Pearson correlation and drawdown analysis, plus latency benchmarks and tests.
  • Headless deployment: Intended for Linux/EC2 environments with simple process management scripts.

Project Layout

src/                  - Application sources
  bot.cpp             - Main trading bot (entry logic, TP/SL, Telegram alerts)
  stoploss.cpp        - Standalone stop-loss manager for existing positions
  collector.cpp       - Market data collector
  analyzer_main.cpp   - Probability analyzer
clob-cpp-sdk/         - Custom Polymarket CLOB SDK
  api_client.*        - REST API client
  signer.*            - EIP-712 / secp256k1 signing
  order_builder.*     - Order construction utilities
  ws_manager.*        - WebSocket connection management
includes/             - Internal libraries
  websocket/          - WebSocket client (`GGwebsocket.hpp`)
  restApi/            - HTTP client (`fasthttp.hpp`)
  utils/              - JSON, time helpers, env parser, etc.
  terminalGui/        - Optional terminal dashboard
tests/                - Unit tests and latency benchmarks
docs/                 - Architecture and strategy diagrams

Tech Stack

  • Language: C++17
  • Crypto & networking: OpenSSL, libcurl, libwebsockets, libsecp256k1
  • Parsing & formatting: simdjson, fmt
  • Testing: Google Test

Prerequisites

This project targets Linux (or WSL2 on Windows). You will need a recent compiler (GCC or Clang) and the following libraries installed (package names may vary by distribution):

  • build-essential / gcc / g++
  • libssl-dev
  • libcurl4-openssl-dev
  • libsimdjson-dev
  • libwebsockets-dev
  • libsecp256k1-dev
  • libfmt-dev
  • libgtest-dev (for tests)

Database Setup

you must also install MongoDB and the C++ driver.

On Ubuntu (example):

# MongoDB Community Edition (server + tools)
sudo apt-get update
sudo apt-get install -y mongodb-org

# MongoDB C++ driver (mongocxx + bsoncxx)
sudo apt-get install -y libmongocxx-dev libbsoncxx-dev

After that, enable the driver in the Makefile:

FLAGS += -DUSE_EXTERNAL_STORE
STORE_LIBS = -lmongocxx -lbsoncxx

Building

From the project root:

make bot               # Build the main trading bot (bin/bot)
make analyzer_prob     # Build probability analyzer (bin/analyzer_prob)
make latency_benchmark # Build latency benchmark (bin/latency_benchmark)
make test              # Build and run unit tests (tests/run_tests)
make build             # Build the interactive terminal client (bin/main)

Configuration

Create a .env file from the template and populate it with your credentials:

cp .env.example .env

Required variables

  • POLY_WALLET_ADDRESS – Polymarket wallet address.
  • WALLET_PRIVATE_KEY – Private key used to sign CLOB requests (keep this secret).
  • BUILDER_API_KEY, BUILDER_SECRET, BUILDER_PASSPHRASE – Polymarket Builder API credentials.

Optional variables

  • TELEGRAM_BOT_TOKEN – Token for your Telegram bot (for alerts).
  • TELEGRAM_CHAT_ID – Chat ID that will receive notifications.

Without the Telegram variables, the bot will run normally but will not send alerts.

Running

After building:

./bin/bot              # Live trading
./bin/bot --dry-run    # Simulated mode (no real orders sent)

For long‑running deployments, you can use the helper scripts:

./start.sh             # Start bot in background
./start.sh --dry-run   # Start in dry-run mode
./start.sh status      # Check if the bot is running
./start.sh logs        # Tail bot logs
./start.sh stop        # Stop the bot

The dedicated stop‑loss process can be run independently if desired:

./bin/stoploss                       # Monitor and protect open positions
./bin/stoploss --dry-run             # Simulated stop-loss
./bin/stoploss --stop=0.40           # Custom stop threshold
./bin/stoploss --scan-interval=60    # Custom scan interval (seconds)

Security Notes

  • This bot uses your private key to sign orders on Polymarket.
    • Never commit .env to version control.
    • Run it only on machines you control and trust.
  • Any optional external storage or logging of secrets should be secured and reviewed before use.

License

MIT License

About

polymarket trading bot, polymarket trading bot, polymarket trading bot, polymarket trading bot, polymarket trading bot, polymarket trading bot, polymarket trading bot, polymarket trading bot, polymarket trading bot, polymarket trading bot, polymarket trading bot, polymarket trading bot, polymarket trading bot, polymarket trading bot, polymarket

Topics

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

 
 
 

Contributors