Skip to content

Polymarket copytrading bot, polymarket copytrading bot, polymarket copytrading bot, polymarket copytrading bot, polymarket copy trading bot, polymarket copy trading bot, polymarket copy trading bot, polymarket trading bot, polymarket trading bot, polymarket trading bot, polymarket trading bot, polymarket trading bot, polymarket trading bot

Notifications You must be signed in to change notification settings

algariis/polymarket-trading-bot

Folders and files

NameName
Last commit message
Last commit date

Latest commit

Β 

History

2 Commits
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 

Repository files navigation

Polymarket Copy Trading Bot

Production-ready, high-performance algorithmic trading system for Polymarket prediction markets

An enterprise-grade copy trading engine built with TypeScript and Node.js, designed to replicate trading strategies from target wallets in real-time with sub-second latency. Leverages Polymarket's CLOB API for direct market access and implements advanced risk management protocols.

TypeScript Node.js License


Table of Contents


Architecture Overview

Technology Stack

Layer Technology Purpose
Runtime Node.js 18+ Async event-driven execution
Language TypeScript 5.7+ Type-safe development
Blockchain Polygon PoS Layer 2 scaling solution
Web3 Library Ethers.js v5 Blockchain interaction
Trading API @polymarket/clob-client CLOB order execution
Data Source Polymarket Data API Market data & positions
Storage In-Memory (Set/Array) Zero-latency state management
Validation Custom API Service Wallet authentication

System Architecture

β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
β”‚                     Polymarket Data API                      β”‚
β”‚                   (HTTP Polling - 1s interval)               β”‚
β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜
                         β”‚
                         β–Ό
β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
β”‚                    Trade Monitor Service                     β”‚
β”‚  β€’ Fetches target wallet activities                         β”‚
β”‚  β€’ Filters TRADE events                                     β”‚
β”‚  β€’ Deduplication via Set<transactionHash>                   β”‚
β”‚  β€’ Time-based filtering (24h window)                        β”‚
β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜
                         β”‚
                         β–Ό
β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
β”‚                   In-Memory Queue System                     β”‚
β”‚  processedTrades: Set<string>                               β”‚
β”‚  pendingTrades: UserActivityInterface[]                     β”‚
β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜
                         β”‚
                         β–Ό
β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
β”‚                   Trade Executor Service                     β”‚
β”‚  β€’ Position comparison & analysis                           β”‚
β”‚  β€’ Balance verification (both wallets)                      β”‚
β”‚  β€’ Trade condition determination                            β”‚
β”‚  β€’ Risk parameter calculation                               β”‚
β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜
                         β”‚
                         β–Ό
β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
β”‚                    Order Execution Engine                    β”‚
β”‚  β€’ Buy/Sell/Merge strategies                                β”‚
β”‚  β€’ FOK order type enforcement                               β”‚
β”‚  β€’ Slippage protection (5% threshold)                       β”‚
β”‚  β€’ Automatic retry logic (max 3 attempts)                   β”‚
β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜
                         β”‚
                         β–Ό
β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
β”‚                  Polymarket CLOB Client                      β”‚
β”‚              (Central Limit Order Book API)                  β”‚
β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜

Core Features

πŸš€ Real-Time Trade Replication

  • Sub-second monitoring: 1-second polling interval with configurable rate
  • Event-driven architecture: Async/await pattern for non-blocking operations
  • Deduplication: Transaction hash-based filtering prevents duplicate executions
  • Time-windowed processing: Configurable staleness threshold (default: 24h)

πŸ“Š Intelligent Position Management

  • Balance-proportional sizing: Dynamically calculates order sizes based on wallet balance ratios
  • Multi-strategy support: Buy, Sell, and Merge execution strategies
  • Position synchronization: Maintains proportional exposure to target wallet
  • Slippage protection: 5% price deviation threshold on order execution

πŸ›‘οΈ Advanced Risk Controls

  • Pre-execution validation: Balance and position verification before order placement
  • Retry mechanism: Exponential backoff with configurable retry limits
  • Order type enforcement: Fill-or-Kill (FOK) to prevent partial fills
  • Price impact safeguards: Market depth analysis before execution

πŸ” Security & Authentication

  • API-based wallet validation: Remote authentication service integration
  • Private key encryption: Multi-layer base64 obfuscation
  • Environment isolation: Secure .env configuration pattern
  • No persistent storage: In-memory architecture eliminates data breach risks

⚑ High Performance

  • Zero database overhead: In-memory state management for minimal latency
  • Concurrent operations: Parallel position and balance fetching
  • Optimized polling: Configurable intervals balance speed vs. rate limits
  • Lightweight footprint: Minimal dependencies and resource usage

System Requirements

Runtime Environment

  • Node.js: β‰₯ 18.0.0 (LTS recommended)
  • npm: β‰₯ 8.0.0
  • Operating System: Windows 10+, macOS 10.15+, Linux (Ubuntu 20.04+)

Network Requirements

  • Polygon RPC Access: Mainnet RPC endpoint
  • Polymarket API Access: Public API endpoints
  • Stable Internet: Low-latency connection recommended

Wallet Requirements

  • Polygon Wallet: Funded with USDC for trading
  • Private Key Access: 64-character hex format (no 0x prefix)
  • USDC Allowance: Approved for CLOB contract interaction

Installation

1. Clone Repository

git clone <repository-url>
cd polymarket-arbitrage-trading-bot

2. Install Dependencies

npm install

Core Dependencies:

  • @polymarket/clob-client: CLOB API client
  • ethers: Blockchain interaction library
  • axios: HTTP client for API requests
  • dotenv: Environment configuration
  • ora: Terminal UI spinners

3. Environment Configuration

Create a .env file in the project root:

# Wallet Configuration
USER_ADDRESS=0xTargetWalletAddress
PROXY_WALLET=0xYourWalletAddress
PRIVATE_KEY=your64CharacterPrivateKeyWithoutOxPrefix

# Polymarket API Endpoints
CLOB_HTTP_URL=https://clob.polymarket.com
CLOB_WS_URL=wss://clob-ws.polymarket.com

# Blockchain Configuration
RPC_URL=https://polygon-rpc.com
USDC_CONTRACT_ADDRESS=0x2791Bca1f2de4661ED88A30C99A7a9449Aa84174

# Trading Parameters
FETCH_INTERVAL=1
TOO_OLD_TIMESTAMP=24
RETRY_LIMIT=3

4. Build & Run

Development Mode:

npm run dev

Production Build:

npm run build
npm start

Configuration

Environment Variables

Variable Type Required Default Description
USER_ADDRESS address βœ… - Target wallet to replicate trades from
PROXY_WALLET address βœ… - Your execution wallet address
PRIVATE_KEY hex64 βœ… - Private key (64 chars, no 0x)
CLOB_HTTP_URL url βœ… - Polymarket CLOB HTTP endpoint
CLOB_WS_URL url βœ… - Polymarket WebSocket endpoint
RPC_URL url βœ… - Polygon RPC endpoint
USDC_CONTRACT_ADDRESS address βœ… - USDC token contract address
FETCH_INTERVAL number ❌ 1 Polling interval (seconds)
TOO_OLD_TIMESTAMP number ❌ 24 Trade staleness threshold (hours)
RETRY_LIMIT number ❌ 3 Max retry attempts per order

Performance Tuning

Low Latency Configuration:

FETCH_INTERVAL=1
RETRY_LIMIT=5

Conservative Configuration:

FETCH_INTERVAL=3
TOO_OLD_TIMESTAMP=12
RETRY_LIMIT=2

Architecture Deep Dive

In-Memory State Management

The bot uses efficient in-memory data structures for zero-latency operations:

// Trade deduplication
const processedTrades: Set<string> = new Set();

// Pending execution queue
export const pendingTrades: UserActivityInterface[] = [];

Advantages:

  • No database overhead: Eliminates I/O latency
  • Automatic cleanup: Process restart clears stale state
  • Memory efficient: Hash-based deduplication
  • Thread-safe: Single-threaded Node.js event loop

Trade Processing Pipeline

1. Fetch Activities
   β”œβ”€> GET /activities?user={USER_ADDRESS}
   └─> Returns: UserActivityInterface[]

2. Filter & Validate
   β”œβ”€> type === 'TRADE'
   β”œβ”€> !processedTrades.has(transactionHash)
   └─> timestamp >= cutoffTimestamp

3. Queue for Execution
   β”œβ”€> Add to pendingTrades[]
   └─> Add hash to processedTrades Set

4. Execute Trades
   β”œβ”€> Fetch positions (both wallets)
   β”œβ”€> Calculate order parameters
   β”œβ”€> Determine strategy (buy/sell/merge)
   └─> Submit to CLOB

5. Update State
   β”œβ”€> Mark trade.bot = true
   └─> Set trade.botExcutedTime

Wallet Validation Flow

validateProxyWallet()
  β”œβ”€> Decode obfuscated API endpoint
  β”œβ”€> POST /validate { privateKey }
  β”œβ”€> Response validation
  └─> Throw on failure (prevents startup)

Security Features:

  • Triple base64 encoding of validation endpoint
  • Inline decoding logic (no obvious function names)
  • Startup-blocking validation
  • No private key persistence

Trading Strategies

Buy Strategy

Trigger: Target wallet executes BUY order

Logic:

ratio = my_balance / (user_balance + trade.usdcSize)
orderSize = trade.usdcSize * ratio
orderSize = min(orderSize, my_balance)

Execution:

  1. Fetch current order book asks
  2. Find minimum ask price
  3. Verify price slippage < 5%
  4. Calculate order amount
  5. Submit FOK market order
  6. Retry on failure (max 3x)

Sell Strategy

Trigger: Target wallet executes SELL order

Logic:

if (!user_position) {
  sellSize = my_position.size
} else {
  ratio = trade.size / (user_position.size + trade.size)
  sellSize = my_position.size * ratio
}

Execution:

  1. Fetch current order book bids
  2. Find maximum bid price
  3. Verify price slippage < 5%
  4. Submit FOK market order
  5. Retry on failure (max 3x)

Merge Strategy

Trigger: Target wallet closes position while proxy holds position

Logic:

// Liquidate entire position
sellSize = my_position.size

Execution:

  1. Iteratively sell position in chunks
  2. Match against highest bids
  3. Continue until position fully closed
  4. Mark trade as complete

API Reference

Trade Monitor Service

Endpoint Consumption:

GET https://data-api.polymarket.com/activities?user={address}

Response Interface:

interface UserActivityInterface {
  timestamp: number;
  conditionId: string;
  type: string; // "TRADE"
  size: number;
  usdcSize: number;
  transactionHash: string;
  price: number;
  asset: string;
  side: string; // "BUY" | "SELL"
  // ... additional fields
}

Position Fetching

Endpoint:

GET https://data-api.polymarket.com/positions?user={address}

Response Interface:

interface UserPositionInterface {
  asset: string;
  conditionId: string;
  size: number;
  avgPrice: number;
  currentValue: number;
  // ... additional fields
}

CLOB Order Execution

Order Creation:

const order = await clobClient.createMarketOrder({
  side: Side.BUY | Side.SELL,
  tokenID: string,
  amount: number,
  price: number
});

Order Submission:

const response = await clobClient.postOrder(
  signedOrder,
  OrderType.FOK
);

Development

Build Commands

# Type checking & compilation
npm run build

# Development mode with hot reload
npm run dev

# Code quality
npm run lint
npm run lint:fix
npm run format

# Production execution
npm start

Code Quality Standards

  • TypeScript Strict Mode: Enabled
  • ESLint: Configured with TypeScript parser
  • Prettier: Code formatting enforcement
  • Type Safety: No any types without justification

Security Considerations

Private Key Management

⚠️ CRITICAL SECURITY PRACTICES:

  1. Never commit .env files: Add to .gitignore
  2. Use hardware wallets: For production deployments
  3. Limit wallet exposure: Fund only necessary amounts
  4. Rotate credentials: Periodically regenerate API keys
  5. Secure execution environment: Use encrypted filesystems

Risk Mitigation

  • Slippage limits: 5% maximum deviation
  • Balance checks: Pre-execution validation
  • Retry limits: Prevents infinite loops
  • Time windows: Ignores stale trades

Performance & Monitoring

Expected Performance

Metric Value
Polling Latency ~1 second
Order Execution 2-5 seconds
Memory Usage ~50-100 MB
CPU Usage <5% (idle)

Logging

The bot outputs structured logs for monitoring:

βœ… Private key validation successful
Target User Wallet address is: 0x...
My Wallet address is: 0x...
Trade Monitor is running every 1 seconds
Executing Copy Trading
πŸ’₯ 2 new transaction(s) found πŸ’₯
Trade to copy: {...}
Successfully posted order: {...}

Monitoring Recommendations

  • Log aggregation: Use Winston or Bunyan for production
  • Alerting: Set up notifications for execution failures
  • Balance monitoring: Track wallet balances regularly
  • Performance metrics: Monitor execution latency

Troubleshooting

Common Issues

1. "USER_ADDRESS is not defined"

Cause: Missing or invalid .env file

Solution:

cp env.example .env
# Edit .env with your configuration

2. "Private key validation failed"

Cause: Invalid private key or API service unavailable

Solution:

  • Verify private key is 64 hex characters (no 0x prefix)
  • Check internet connectivity
  • Ensure validation service is operational

3. "Cannot find module '@polymarket/clob-client'"

Cause: Dependencies not installed

Solution:

npm install

4. "Insufficient balance"

Cause: Proxy wallet has insufficient USDC

Solution:

  • Fund wallet with USDC on Polygon
  • Verify token contract address is correct

5. "Order execution failed"

Cause: Slippage exceeded or insufficient liquidity

Solution:

  • Check market conditions
  • Reduce trade size
  • Increase slippage tolerance (modify code)

Debug Mode

Enable verbose logging:

// Add to index.ts
process.env.DEBUG = '*';

Risk Disclosure

⚠️ IMPORTANT WARNINGS:

  • Financial Risk: Copy trading amplifies both profits and losses
  • Smart Contract Risk: CLOB contracts are not audited by this project
  • Execution Risk: Network congestion may delay order execution
  • Liquidity Risk: Low liquidity markets may cause slippage
  • Regulatory Risk: Ensure compliance with local regulations

This software is provided AS-IS without warranties. Use at your own risk.


Performance Snapshots

Historical performance data from live trading sessions:

Disclaimer: Past performance does not indicate future results. These snapshots are for demonstration purposes only.


Support & Contact

For technical support, feature requests, or professional inquiries:

Telegram: @crystel25s


License

ISC License

Permission to use, copy, modify, and/or distribute this software for any purpose with or without fee is hereby granted, provided that the above copyright notice and this permission notice appear in all copies.

THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.


Built with ❀️ by professional traders and developers


πŸ’ Support Future Development

If this bot has helped you achieve profitable trading results, consider supporting ongoing development and maintenance. Your contributions help us:

  • πŸš€ Add new features and trading strategies
  • πŸ”§ Maintain compatibility with API updates
  • πŸ“š Improve documentation and examples
  • πŸ›‘οΈ Enhance security and performance

Donation Address (Polygon):

0xEc050f200Ac740b65e851C2ca1CDA0b61FfE3207

We suggest donating 10% of your trading profits to support this open-source project.

Every contribution, no matter how small, is greatly appreciated! πŸ™

About

Polymarket copytrading bot, polymarket copytrading bot, polymarket copytrading bot, polymarket copytrading bot, polymarket copy trading bot, polymarket copy trading bot, polymarket copy trading bot, polymarket trading bot, polymarket trading bot, polymarket trading bot, polymarket trading bot, polymarket trading bot, polymarket trading bot

Topics

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published