Building an Order-Book Imbalance TWAP Bot for Polymarket Crypto Markets
Short-duration Polymarket crypto markets can move quickly when liquidity and order flow change.
Instead of relying only on price, we can look at the order book to estimate short-term buying or selling pressure, then use TWAP (Time-Weighted Average Price) to execute the trade gradually.
This tutorial shows the basic architecture.
What Is Order-Book Imbalance?
Order-Book Imbalance (OBI) compares bid volume with ask volume:
OBI = (bid_volume - ask_volume) / (bid_volume + ask_volume)
The value is approximately between -1 and +1.
- Positive OBI → stronger bid pressure
- Negative OBI → stronger ask pressure
- Near zero → relatively balanced
A simple strategy could be:
OBI > +0.30
↓
UP signal
OBI < -0.30
↓
DOWN signal
Otherwise
↓
No trade
However, using one order-book snapshot is noisy. The original strategy therefore uses multiple rolling windows rather than relying on a single measurement.
Use Multiple OBI Windows
Instead of calculating OBI once, track:
OBI(1s)
OBI(3s)
OBI(5s)
OBI(10s)
Then create a weighted signal:
weighted_obi = (
0.40 * obi_1s +
0.30 * obi_3s +
0.20 * obi_5s +
0.10 * obi_10s
)
The shorter window receives the highest weight so the strategy can react to recent order-flow changes.
For example:
OBI 1s = +0.44
OBI 3s = +0.39
OBI 5s = +0.35
OBI 10s = +0.31
Weighted OBI = +0.392
This indicates persistent UP-side pressure rather than a single OBI spike.
Python OBI Calculator
The core calculation is very small:
def calculate_obi(bid_volume, ask_volume):
total = bid_volume + ask_volume
if total == 0:
return 0.0
return (bid_volume - ask_volume) / total
Then calculate the rolling values:
obi_1s = calculate_obi(bid_1s, ask_1s)
obi_3s = calculate_obi(bid_3s, ask_3s)
obi_5s = calculate_obi(bid_5s, ask_5s)
obi_10s = calculate_obi(bid_10s, ask_10s)
And combine them:
weighted_obi = (
0.40 * obi_1s +
0.30 * obi_3s +
0.20 * obi_5s +
0.10 * obi_10s
)
Generate the Trading Signal
Now turn the weighted OBI into a simple signal:
if weighted_obi > 0.30:
signal = "UP"
elif weighted_obi < -0.30:
signal = "DOWN"
else:
signal = "NEUTRAL"
The important idea is:
Don't trade because of one OBI spike. Look for strong and persistent order-book pressure.
A persistence filter can make this even better:
OBI > +0.30
AND
Weighted OBI > +0.25
AND
signal remains positive for 3+ seconds
Only then should the bot start executing.
Add TWAP Execution
Once the signal is confirmed, don't necessarily buy the entire position at once.
For example, a $500 position can become:
$100 → $100 → $100 → $100 → $100
Instead of:
$500 → immediate execution
A simplified TWAP engine:
def execute_twap(signal, total_size, slices):
slice_size = total_size / slices
for _ in range(slices):
current_signal = get_current_signal()
if current_signal != signal:
break
place_order(
outcome=signal,
size=slice_size
)
wait_for_next_slice()
The important part is checking the signal before every slice.
If the order-book pressure disappears, the bot can stop instead of blindly completing the original order. This feedback loop is one of the key ideas of the strategy.
Simple Strategy Architecture
The complete system can be structured like this:
Market Data
↓
Order Book Collector
↓
OBI Calculator
↓
Rolling OBI
↓
Signal Generator
↓
Liquidity / Price Filters
↓
TWAP Execution
↓
Risk Management
This separation also makes it easier to backtest each component independently.
Add Risk Filters
OBI should not be the only condition.
Useful filters include:
Maximum position size
Maximum TWAP slice
Maximum entry price
Maximum spread
Minimum liquidity
Time-to-expiry cutoff
Signal invalidation
For example:
if spread > max_spread:
return "NO_TRADE"
And stop execution if:
weighted_obi < threshold:
stop_twap()
The original strategy also recommends stopping new entries near expiry and determining the exact cutoff through backtesting.
5-Minute vs 15-Minute Markets
The same framework can be tested on both 5-minute and 15-minute crypto markets.
For 5-minute markets:
1s / 3s / 5s / 10s
can provide faster signals.
For 15-minute markets, slower windows may be worth testing.
The important point is not to assume that the same parameters work everywhere.
Test different:
OBI thresholds
OBI windows
TWAP intervals
Signal persistence
Entry-price limits
The original article specifically recommends comparing these parameters through historical order-book data.
What Should You Backtest?
Record data such as:
Timestamp
BTC price
UP/DOWN price
Bid volume
Ask volume
OBI windows
Weighted OBI
Spread
Volume
Time to expiry
Execution price
Final outcome
Then compare:
Win rate
Average return
Slippage
Fill rate
Maximum drawdown
Profit factor
Average entry price
Don't optimize only for win rate.
For a TWAP strategy, execution quality matters too.
Final Strategy
The complete idea is:
Order Book
↓
Calculate OBI
↓
Rolling OBI
↓
Weighted Signal
↓
Persistence Filter
↓
Price + Liquidity Filters
↓
UP / DOWN
↓
TWAP Execution
↓
Recalculate Signal
↓
Continue / Stop
The core concept is simple:
Use Order-Book Imbalance to detect short-term market pressure, then use TWAP to execute the position gradually while continuously monitoring whether the signal remains valid.
This turns a simple directional rule into an adaptive trading system.
Conclusion
An Order-Book Imbalance TWAP Bot combines two useful ideas:
OBI helps answer:
Which side is showing stronger short-term order-flow pressure?
TWAP helps answer:
How can we execute the position without committing everything at once?
The next step is testing whether the signal actually provides an edge across different Polymarket crypto markets and market conditions.
That's where the real research begins.
🤝 Collaboration & Contact
If you’re interested in building trading bots, buy trading bots, collaborating, exploring strategy improvements, or discussing about this system, feel free to reach out.
I’m especially open to connecting with:
Quant traders
Engineers building trading infrastructure
Researchers in prediction markets
Investors interested in market inefficiencies
📌 GitHub Repository
This repo has some Polymarket several bots in this system.
You can explore the full implementation, strategy logic, and ongoing updates about 5 min crypto market here:
Benjam1nCup
/
Polymarket-trading-bot-python-V2
polymarket trading bot polymarket bot polymarket arbitrage bot
Polymarket Trading Bot | Polymarket Arbitrage Bot | Polymarket TWAP Trading Bot
An open-source and Strong Strategy collection of Polymarket trading bot and Polymarket arbitrage bot and Polymarket TWAP trading bot in Python for high-performance automated trading on polymarket crypto 5min and 15min markets.
Features
-
Explosive growth of Polymarket with surging trading volume and new short-term markets
-
Increasing dominance of automated bots and AI in 5-minute and 15-minute crypto prediction markets
-
Higher profitability potential through advanced arbitrage and market-making strategies
-
Stronger edge for Python-based bots with real-time orderbook intelligence and low-latency execution
-
Continuous evolution of sniper, ladder, stair, momentum, and copy trading strategies
-
Scalable daily profits as prediction markets move toward hundreds of billions in annual volume
-
Full future-proof architecture for new features, contracts, and high-frequency trading environments
Included Trading Bots
Designed for arbitrage, directional strategies, and ultra-short-term markets (including 5-minute and 15-minute rounds), this bot framework provides a robust…
💬 Get in Touch
If you have ideas, questions, or would like to collaborate or want these trading bots, don’t hesitate to reach out directly.
Feedback on your repo (based on your description & strategy)
Contact Info
Telegram
https://t.me/BenjaminCup
tags: #polymarket,#trading,#bot,#architecture,#tutorial,#TWAP


Top comments (0)