Daniele De NuntiisProjects

Polymarket Trading System

Sep 2024 – Jan 2025PythonPolymarket APIDuckDBscikit-learnXGBoost

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.

FigJam board of notes, charts and experiments for the Polymarket project

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.

Calibration chart: market price against probability for 2024, with Brier 0.1039 and Diff -0.0076
The red line is what perfect calibration would look like and the dots are what I measured. A dot above the line means the market segment is undervalued, a dot below means it's overvalued

Code and architecture

Architecture diagram: scraper and enrichment crawler, DuckDB and Parquet storage, backtesting simulator, live API, API wrapper, bots, data analysis

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.

Log table of strategy metrics with scatter plots for max drawdown, profit, naive ROI and ROI over drawdown
A dump of my logs

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.

Multi-line chart of till now, ROI, max draw, APY, CAGR, APR and PR ratio across 21 runs
x axis: number of runs. On the left you can see how the results straighten out as n gets bigger

Iterating on the strategy

Montage of strategy experiment plots, filters and simulation results

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:

Calibration chart for markets found at 20 (hour), with Brier 0.0247 and Diff -0.0013

18,631 datapoints, with a Diff (a rough measure of the edge) of about -0.001.

From there, I started filtering.

Calibration chart filtered by spread .035, with Brier 0.0182 and Diff +0.0038

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.

Heatmap of calibration by bucket for football, Global Elections and Music across price ranges
For example, here you can see that music markets are overvalued, so if n was big enough I would filter them out

Then, alongside the filters, I would run simulations:

Simulated balance curves for the 0 to 5 and 0 to 2 day filters, with ROI, CAGR and max drawdown

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.

Kernel density and histogram of simulated performance, with VaR and reliability statistics

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.
XGBoost performance table and simulation plots

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.

b79 Polymarket profile: total PnL -$14.62, $33.73 positions value, 1,968 markets traded, $1.31 biggest win
Live balance of the b79 bot from March 2025 to September 2026, with ROI -3.49% and max drawdown 60.87%

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.