Polymarket Trading System
An automated trading system for Polymarket: a scraper that collects market data, a backtesting engine that replays real orderbooks, and bots that run the same code live.
- - Why prediction markets: the question that started it and the first calibration check
- - Code and architecture: the data pipeline, the backtesting simulator and the API wrapper
- - Worked example of a strategy: from first experiments and filters to simulated results and trained models
- - Results: what worked and what didn't

Why prediction markets
I chose prediction markets because they were new and had low trading volume, so I expected to find some inefficiencies.
The first thing to check is market calibration: do markets priced at 90% actually resolve to yes 90% of the time? Any predictable deviation is a possible trading opportunity.

Code and architecture

Data pipeline
- - Scraper: Runs 5 times a day and saves all the market data (prices and orderbooks of open markets).
- - Enrichment crawler (Gamma API): The scraper only saves the data that would be lost if it weren't captured live. Everything else, like how a market resolved and its tags, is added later by the crawler.
- - Storage: I started by dumping data straight into files. At Polymarket's volume back then, parsing a file to find a row took a negligible amount of time, which was good enough for a first implementation. As the data grew (about 50GB of text), I switched to DuckDB and Parquet, which was orders of magnitude faster.
- - Backtesting Simulator, the simulator replays each "tick" of the bot as it would have ran live, the bot receives the same input and has the same output for any given tick. (the only thing we can't model is how the market would respond to a given trade, we can use the order book to get a sense of how a big trade would move a market but not of how the other market participants would respond) . Optimizing the sim for high volume improved performance form a couple hundred seconds per tick with tens of thousands of markets to a few hundred milliseconds
I stored all this data so I could run simulations on it and iterate over possible strategies.
API wrapper
The bots call a wrapper around the Polymarket API. A flag tells the wrapper whether to pull live data or serve simulated data from the stored order books.
After testing the same bot both live and in simulation, I trusted the simulator and could start looking for strategies.
Worked example of a strategy
To find strategies, I started by getting a feel for the markets: I ran a bunch of experiments and checked the results.

What stood out first were the wild results, like an ROI over 1000% in some cases. At the time I only had a few hundred datapoints, so a slight change in the setup could produce totally different results.
Synthetic Data
To get more accurate results, I started adding randomness to the simulation and averaging over multiple runs.

Iterating on the strategy

Once I had an idea for a strategy, I iterated on it to refine it. It started really simple and got more complex over time. I didn't have a hard measure for it, but I knew from the start that the more search pressure I applied, the more I would overfit to my limited data, so I only let the search get more complex as my dataset grew.
Here's an example from one of my latest strategies, "b79".
The core of the strategy was "markets that are about to close and are priced over 90 cents"
This was the calibration of that segment:

18,631 datapoints, with a Diff (a rough measure of the edge) of about -0.001.
From there, I started filtering.

Filtering for markets with a spread below 0.035 already gave much better results: we kept 16,599 points, so we didn't lose much data, and the Diff went up to +0.0038.
This is step one: you find a strategy and keep filtering until you're satisfied.
How do you know what to filter for?
Here I would just plot different filters and see how they affected the calibration.

Then, alongside the filters, I would run simulations:

Here "0 to 5" and "0 to 2" refer to how many days away the markets close.
Then I would add randomness and simulate more runs to get more meaningful results. Each step gives you more detailed information but also costs more time and computation.
Here are the results of a series of simulations.

Seems complicated, but most of the value is clear from the first figure: it plots the probability distribution of the APRs, and you can see they're concentrated between 0 and +20%. So we can be fairly confident that the strategy was positive during the backtesting period.
The trained model
Besides the filters I built by hand, I also spent a couple weeks training models to look for patterns.
- - Goal: predict whether a market resolves yes or no, starting from a snapshot of its price and orderbook.
- - Features: price, spread, the distance of the price from 0.5, the log-odds of the price, and time to expiry. I also tried a bunch of others (hour of the day, orderbook depth, price momentum, market tags), but kept these five as the core ones.
- - Performance: I trained on the first 80% of the data in time order (about 73,000 rows) and tested on the last 20% (about 18,000), so the test data always comes after the training data. XGBoost reached about 60% accuracy, with precision and recall around 0.60 for both classes. CatBoost's best result came at iteration 5 (out of up to 1000) and never improved afterwards, which suggests there was very little to learn beyond the price. I also tried logistic regression, random forests and LightGBM.

The performance in simulations were either really good (probably overfitting) or really bad, I stopped working on the project soon afterwards and ended up never putting those online.
Results
So how did it go? Here's the performance of b79 the last bot that I put online.


The live results were positive at first. Then a few markets had the wrong end date: the bot bought them and ended up stuck holding them for a few months (the june-september drop). Once they closed, it recovered, and it has pretty much hovered around the same level since (between 80 and 110).
So were the markets efficient? Yeah kind of. The results were encouraging at the start, but the strategies aren't working as well now. It's probably a combination of two things: the strategies getting older (as a rule of thumb, a strategy's useful lifespan is about half its backtest window, and they're now way way past that point) and the markets getting more accurate as Polymarket gets more volume.
The scraper is still running, and I might come back to this project later on, but for now that's it.