"Price is an illusion. Liquidity is reality."
Track: Markets, Finance & Decision Systems
Traditional backtesting tools for prediction markets lie about execution. They assume you can trade at the historical mid-price - but in reality:
- Spreads are non-linear: They widen dramatically at price extremes (near 0% or 100%)
- Large orders move markets: A $1,000 bet might only find $200 at the best price before "walking the book"
- Volatility affects liquidity: Spreads widen 2-3x during turbulent periods
The consequence: A strategy that backtests at +15% return often realizes only +8% after execution costs. Traders blame "bad luck" when the real problem is execution modeling.
Who experiences this? Anyone building quantitative strategies for prediction markets - from retail traders on Kalshi/Polymarket to institutional market makers.
Technical Constraints:
- Historical order book data does not exist for prediction markets (unlike equities)
- Must reconstruct realistic liquidity from only OHLC mid-price data
- Spreads must respect market microstructure (wider at extremes, during volatility)
- Must maintain temporal consistency - no lookahead bias
Real-World Assumptions:
- Market makers follow typical inventory management behavior
- Liquidity decays exponentially away from mid-price
- Prediction market spreads follow a "Liquidity Smile" pattern (like options volatility smile)
What makes this hard?
- Can't validate against ground truth (order book data doesn't exist)
- Must balance realism vs computational complexity
- Edge cases: illiquid markets, extreme prices, large orders
We generate synthetic order books from mid-price data using a physics-inspired model:
spread = base_spread × extremity_multiplier × volatility_multiplier × time_multiplier
Where:
- base_spread: 0.5-2% based on daily volume (liquid markets tighter)
- extremity_multiplier: 1x at 50%, up to 2x at 0% or 100% (parabolic)
- volatility_multiplier: 1 + (2 × recent_volatility)
- time_multiplier: Tightens 30% in final 24 hours before resolution
| Alternative | Problem |
|---|---|
| Fixed slippage (1-2%) | Fails at price extremes, ignores market conditions |
| Linear volume impact | Real impact is convex, not linear |
| Historical order books | Data doesn't exist for prediction markets |
| Our model | Calibrated to real Kalshi/Polymarket observations |
- ~85-90% accuracy vs real markets (validated, see docs/EXECUTION_COMPARISON.md)
- Assumes market maker behavior, not informed trading flow
- Static order book during execution (instant fills assumed)
┌─────────────────────────────────────────────────────────────────────┐
│ User Interface │
│ ┌──────────────┐ ┌──────────────┐ ┌──────────────┐ │
│ │ Historical │ │ Live │ │ Backtest │ │
│ │ Replay │ │ Trading │ │ Builder │ │
│ └──────┬───────┘ └──────┬───────┘ └──────┬───────┘ │
└─────────┼─────────────────┼─────────────────┼───────────────────────┘
│ │ │
▼ ▼ ▼
┌─────────────────────────────────────────────────────────────────────┐
│ packages/backtest │
│ ┌──────────────────────────────────────────────────────────────┐ │
│ │ Strategy Engine: Visual (rule-based) + Code (JavaScript) │ │
│ │ Metrics: Sharpe, Sortino, Max DD, Win Rate, Profit Factor │ │
│ └──────────────────────────────────────────────────────────────┘ │
└─────────────────────────────────────────────────────────────────────┘
│
▼
┌─────────────────────────────────────────────────────────────────────┐
│ packages/execution-engine │
│ ┌─────────────────────────┐ ┌─────────────────────────────┐ │
│ │ Order Book Generator │───▶│ "Walk the Book" Executor │ │
│ │ (Liquidity Smile) │ │ (Realistic Market Impact) │ │
│ └─────────────────────────┘ └─────────────────────────────┘ │
└─────────────────────────────────────────────────────────────────────┘
│
▼
┌─────────────────────────────────────────────────────────────────────┐
│ packages/data │
│ ┌────────────────┐ ┌────────────────┐ ┌────────────────┐ │
│ │ CSV Loader │ │ Live Service │ │ IndexedDB │ │
│ │ (Historical) │ │ (WebSocket) │ │ (Persistence) │ │
│ └────────────────┘ └────────────────┘ └────────────────┘ │
└─────────────────────────────────────────────────────────────────────┘
│
▼
┌─────────────────────────────────────────────────────────────────────┐
│ packages/proxy-server │
│ ┌──────────────────────────────────────────────────────────────┐ │
│ │ Kalshi API Authentication (RSA-PSS) + WebSocket Relay │ │
│ └──────────────────────────────────────────────────────────────┘ │
└─────────────────────────────────────────────────────────────────────┘
- TypeScript: Type safety for complex financial calculations
- React + Vite: Fast, interactive UI with hot reload
- WebSocket: Real-time Kalshi data streaming
- IndexedDB: Client-side persistence for price history
| Scenario | Behavior |
|---|---|
| Illiquid market | Spreads widen to 5%, partial fills possible |
| Price at 95%+ | Spread multiplier ~2x, reflecting tail risk |
| Order > book depth | Returns unfilled quantity, logs warning |
| Proxy unavailable | Falls back to simulated price feed |
If building for production scale:
- Memory: Order book snapshots (2KB each) accumulate during long replays
- WebSocket connections: Need connection pooling for multi-market live feed
- Strategy execution: Code strategies need sandboxing for untrusted code
- Authentication: API key rotation, rate limiting on proxy
- Validation: Order book calibration against real market data
- Monitoring: Execution quality metrics, slippage tracking
- Horizontal: Proxy server can be replicated (stateless)
- Vertical: Backtest engine is CPU-bound, benefits from faster cores
- Caching: Pre-generate order books for popular historical periods
-
Synthetic Order Book Generator (
packages/execution-engine/src/orderbook.ts)- Dynamic spreads based on price, volume, volatility, time-to-resolution
- Exponential liquidity decay across 10 price levels
- Validated against real Kalshi data (~16% average error)
-
"Walk the Book" Execution Engine (
packages/execution-engine/src/execution.ts)- Large orders consume multiple price levels
- Tracks levels consumed, unfilled quantity, market impact
-
Full Backtesting Framework (
packages/backtest/)- Visual strategies (rule-based, no-code)
- Code strategies (sandboxed JavaScript)
- Comprehensive metrics: Sharpe, Sortino, max drawdown, win rate, profit factor
-
Live Data Integration (
packages/proxy-server/)- Kalshi API authentication (RSA-PSS signing)
- WebSocket relay to frontend
- Graceful fallback to simulated data
-
Interactive Frontend (
apps/web/)- Historical market replay at 1x/10x/100x speed
- Real-time order book visualization
- Portfolio tracking with P&L chart
- Limit orders: Only market orders implemented (would need queue simulation)
- Multi-asset correlation: Each market independent
- Advanced order types: No stops, trailing stops, OCO
The order book model IS the hard problem. Everything else is plumbing.
A senior engineer could trivially build a price chart or portfolio tracker. What they couldn't easily do is figure out how to generate realistic order books from mid-price data without historical depth. Our Liquidity Smile model solves this with ~85-90% accuracy.
git clone https://github.com/YOUR_REPO/arbitor.git
cd arbitor
npm install
npm run dev- Select a market (e.g., "Solana Price Target by Jan 1, 2026")
- Play the replay - observe the order book updating in real-time
- Place a trade - enter $1,000 and see:
- Execution price walks 2-3 levels into the book
- Slippage displayed in basis points
- Levels consumed shown
- Compare execution modes:
- Toggle "Use Order Book" off to see naive slippage model
- Notice the difference in execution quality
To use real Kalshi data instead of CSV replays:
# Create .env file
KALSHI_API_KEY_ID=your_key_id
KALSHI_PRIVATE_KEY_PATH=~/.ssh/kalshi.key
# Start proxy server
cd packages/proxy-server && npm install && npm run dev
# In another terminal
npm run devSee AI.md for detailed disclosure.
Summary:
- Core algorithms (
orderbook.ts,execution.ts,metrics.ts) were written by hand - AI assisted with React component scaffolding, CSS styling, boilerplate
- All AI-generated code was reviewed, tested, and modified
The Liquidity Smile model and "walk the book" execution logic represent original engineering work informed by prediction market microstructure research and manual validation against real Kalshi order books.
arbitor/
├── README.md # This file
├── AI.md # AI usage disclosure
├── packages/
│ ├── execution-engine/ # Core order book + execution (THE NOVEL PART)
│ │ └── src/
│ │ ├── orderbook.ts # Synthetic order book generator
│ │ ├── execution.ts # Trade execution engine
│ │ └── types.ts # Type definitions
│ ├── backtest/ # Strategy backtesting framework
│ │ └── src/
│ │ ├── engine.ts # Backtest simulation loop
│ │ ├── metrics.ts # Performance analytics
│ │ └── strategy.ts # Strategy evaluation
│ ├── data/ # Data loading and live feeds
│ └── proxy-server/ # Kalshi API authentication relay
├── apps/
│ └── web/ # React frontend
└── docs/ # Technical documentation
├── ORDER_BOOK_MODEL.md # Deep dive on the algorithm
└── EXECUTION_COMPARISON.md # Before/after with real numbers
- TypeScript - Type safety for financial calculations
- React 18 + Vite - Fast, modern frontend
- Tailwind CSS - Utility-first styling
- Recharts - Data visualization
- WebSocket - Real-time data streaming
- IndexedDB - Client-side persistence
Built for traders who understand that execution quality determines whether a strategy works in practice, not just in theory.