
🎙 Podcast Version
2-host dialogue — ALEX & SAM discuss this course.
Algorithmic Arbitrage: Building High-Frequency Prediction Market Bots with LLMs
Overview
This course explores the intersection of Large Language Models (LLMs), automated trading, and prediction markets, specifically focusing on a successful case study involving Polymarket. You will learn how a trader leveraged Claude (an AI assistant) to build a bot capable of executing high-frequency trades based on market structure and price discrepancies. This material is critical for finance students and developers because it demonstrates the democratization of quantitative trading, where complex arbitrage strategies can now be coded and deployed without a traditional hedge fund infrastructure.
Background & Context
Prediction markets, such as Polymarket, allow users to bet on the outcome of real-world events. Unlike traditional stock markets, these markets are often fragmented, meaning multiple different contracts may exist that are all tied to the same underlying event or asset price. This fragmentation creates "inefficiencies"—moments where the price of one contract does not align with the price of another, despite them being logically linked.
Historically, exploiting these inefficiencies required deep knowledge of C++ or Python and a team of quantitative analysts. However, the emergence of advanced LLMs like Claude has shifted the landscape. Traders can now describe a mathematical strategy in natural language, and the AI can generate the necessary API integrations and execution logic. This specific case study highlights a trader who achieved a profit of $295,000 by automating the detection of these structural mispricings at a speed and frequency impossible for a human trader.
Core Concepts
Prediction Market Arbitrage
Arbitrage is the simultaneous purchase and sale of the same or similar assets in different markets to profit from tiny differences in the asset's listed price. In the context of Polymarket, this involves identifying "related markets." For example, if there are two different markets betting on the same election outcome but their prices differ by a few cents, a bot can buy the undervalued contract and sell the overvalued one.
Market Structure Analysis
Market structure refers to the organization of the order book and the relationship between different trading pairs. In this case, the trader focused on the "structure of related markets around the same underlying asset price." This means the bot wasn't predicting the outcome of the event (e.g., "Who will win?"), but rather predicting the price relationship between two markets. If Market A and Market B are mathematically tied to the same result, their prices should move in lockstep; when they don't, a structural opportunity exists.
High-Frequency Execution (HFT)
High-frequency trading involves executing a large number of orders at extremely high speeds. The source material notes that the bot performed 28.8 trades per hour, which averages to 1.58 trades per minute. While this is slower than institutional HFT (which operates in microseconds), it is "high frequency" for a retail trader. This frequency allows the bot to capture "micro-inefficiencies" that disappear within minutes, ensuring that the trader captures profit before other bots or humans can react.
LLM-Assisted Development (The "Claude" Workflow)
The use of Claude represents a shift toward "Prompt-to-Production" development. Instead of writing thousands of lines of code manually, the trader used the LLM to handle the heavy lifting of API connectivity, data parsing, and logic implementation. This allows the trader to act as the "Strategist" (defining the logic) while the AI acts as the "Engineer" (writing the code), drastically reducing the time from idea to execution.
How It Works / Step-by-Step
Step 1: Identifying the Underlying Asset Price
The bot first identifies a "source of truth"—the underlying asset price. This could be a real-time price feed from a reputable oracle or a primary market. The bot monitors this price constantly to establish a baseline of what the "fair value" of a contract should be.
Step 2: Comparing Related Markets
The bot scans multiple related markets on Polymarket that are tied to that same underlying price. It looks for a discrepancy where:
Market A Price ≠ Market B Price (despite both being tied to the same event).
The bot specifically "compares the current" price of one market against the others to find a gap that exceeds a certain profit threshold.
Step 3: Automated Execution
Once a discrepancy is detected, the bot executes a trade instantly. It buys the cheaper contract and sells the more expensive one. Because the bot operates at 1.58 trades per minute, it can enter and exit these positions rapidly, compounding small gains into a significant sum.
Step 4: Iterative Refinement via LLM
The trader likely used Claude to refine the bot's logic. If the bot missed a trade or executed a losing trade, the trader could feed the logs back into Claude and ask, "Why did this happen, and how do we adjust the logic to prevent this?" This creates a feedback loop of continuous optimization.
Real-World Examples & Use Cases
Case Study: The $295,000 Polymarket Bot
The primary example is the trader who generated $295,000 in profit. The bot's success was not based on "guessing" the future, but on "mathematical certainty." By trading the structure of related markets, the trader removed the risk of being "wrong" about the event outcome and instead focused on the mathematical relationship between the markets.
Scenario A: Election Market Divergence
Imagine two markets: one betting on "Candidate X wins the Popular Vote" and another on "Candidate X wins the Presidency." While different, they are highly correlated. If the Popular Vote market spikes but the Presidency market lags behind for 30 seconds, a bot can buy the Presidency market, anticipating it will catch up to the Popular Vote price.
Scenario B: Sports Betting Correlation
In a sports prediction market, there might be a market for "Team A wins the game" and another for "Team A wins the championship." If Team A wins a crucial game, the championship market should rise. A bot can monitor the game-win market and execute trades in the championship market the millisecond the game result is confirmed, beating human traders to the punch.
Key Insights & Takeaways
- Focus on Structure, Not Prediction: The most profitable strategy was not predicting the event outcome, but trading the relationship between markets.
- Speed is a Competitive Advantage: Executing 1.58 trades per minute allows a trader to capture fleeting opportunities that are invisible to manual traders.
- AI as a Force Multiplier: Using Claude allows non-professional developers to build sophisticated financial tools that were previously the sole domain of quant firms.
- Arbitrage Reduces Directional Risk: By trading the "structure" rather than the "outcome," the trader minimizes the risk of losing money due to an unexpected event result.
- Consistency Over Home Runs: Making hundreds of small, high-probability trades is more sustainable than making one large, risky bet.
Common Pitfalls / What to Watch Out For
- API Rate Limits: Beginners often get their accounts banned or throttled by platforms like Polymarket for sending too many requests per second.
- Slippage: In low-liquidity markets, the act of buying can push the price up, erasing the arbitrage profit before the trade is completed.
- Over-Reliance on AI Code: LLMs can introduce "hallucinations" or bugs into financial code. A single logic error in a trading bot can drain an entire account in minutes if there are no "circuit breakers" (stop-losses) in place.
- Latency Issues: If the bot's connection to the server is slow, other faster bots will take the arbitrage opportunity first, leaving the slower bot with a losing position.
Review Questions
- Explain the difference between "predicting an outcome" and "trading the structure of related markets." Which is lower risk and why?
- How does the trade frequency (28.8 trades per hour) contribute to the total profit of $295,000 compared to a manual trader?
- If you were using Claude to build a similar bot, what specific instructions would you give the AI to ensure the bot handles "slippage" and "API rate limits"?
Further Learning
- Quantitative Finance: Study "Statistical Arbitrage" (StatArb) to understand how correlations between assets are calculated.
- API Integration: Learn how to use WebSockets for real-time data streaming, which is faster than standard REST APIs.
- Smart Contract Logic: Explore how Polymarket's underlying blockchain infrastructure handles trades to understand the settlement process.
- Risk Management: Study the concept of "Kelly Criterion" to determine how much capital to allocate to each arbitrage trade to maximize growth while avoiding ruin.