DEV Community

Cover image for How I Built a Real-Time Whale Mirroring Bot for Polymarket CLOB

How I Built a Real-Time Whale Mirroring Bot for Polymarket CLOB

Recently, I’ve been exploring the Polymarket CLOB (Central Limit Order Book). While most traders use the web interface, the real alpha is hidden in the API. I decided to build a tool that tracks "Whale" movements and mirrors their trades automatically.

The Challenge

The main issue with Polymarket's API is handling the rate limits while maintaining a high-speed WebSocket connection. If you're too slow, the spread eats your profit. If you're too fast, you get a 429 Too Many Requests error.

The Solution: Cortex Framework

I developed a modular framework called Cortex to handle these issues. It uses an asynchronous architecture to monitor order books without hitting the rate limits too hard.

Here is a snippet of how the mirroring logic calculates the effective execution price:

def calculate_execution_price(order_book, side, target_volume):
    orders = order_book['bids' if side == 'sell' else 'asks']
    total_filled, total_cost = 0, 0

    for price, size in orders:
        needed = target_volume - total_filled
        if needed <= 0: break

        fill = min(float(size), needed)
        total_filled += fill
        total_cost += fill * float(price)

    return total_cost / total_filled if total_filled > 0 else None
Enter fullscreen mode Exit fullscreen mode

Full Source Code
I've decided to make the core of this mirroring module open-source so the community can build upon it. You can find the full implementation, including the Wiki and setup guides, on GitHub:

👉 Polymarket Copy Trading Bot on GitHub

What's next?
Currently, I'm working on integrating a Solana-based liquidity bridge to allow faster rebalancing between chains.

If you have any questions about the CLOB API or the strategy logic, feel free to drop a comment below or join our discussion in the repository!

Top comments (0)