If you've ever tried to build a trading bot for prediction markets, you know the pain: existing libraries are either too slow, too basic, or both. Polyfill-rs fixes that.
The order book can process updates much faster because integer comparisons are fundamentally faster than decimal ones. We only convert back to decimals when you actually need the data.
*Want to see how this works?* Check out `src/book.rs` - every optimization has commented-out "before" code so you can see exactly what changed and why.
// Tells you: average price, total cost, market impact percentage
```
It's smart about slippage protection and won't let you accidentally market-buy at ridiculous prices.
### Real-Time Data Streaming
WebSocket connections that don't give up. When the connection drops (and it will), the library automatically reconnects with exponential backoff. No more babysitting your data feeds.
### HTTP Client
All the boring stuff like authentication, rate limiting, and retry logic. It just works so you don't have to think about it.
## Performance (Benchmarks Coming Soon)
The library is designed around several key optimizations:
### Order Book Operations
- **Fixed-point math**: Integer operations instead of decimal arithmetic
- **Zero allocations**: Reuse data structures in hot paths
- **Efficient lookups**: Optimized data structures for common operations
The core insight is that most trading operations don't need full decimal precision during intermediate calculations. By using fixed-point integers internally and only converting to decimals at the API boundaries, we can:
**Learning from the code**: The performance optimizations are documented with detailed comments explaining the math, memory layout, and algorithmic choices. It's like a mini-course in high-frequency trading optimization.
**Pro tip**: The trading strategy examples in the code include detailed comments about market microstructure, order flow, and risk management techniques.
## Configuration Tips
### Order Book Depth Settings
The most important performance knob is how many price levels to track:
// For analysis/research: could go higher, but memory usage grows
letbook_manager=OrderBookManager::new(500);
```
Why this matters: Each price level takes memory, but 90% of trading happens in the top 10 levels anyway. More levels = more memory usage for diminishing returns.
*The code comments in `src/book.rs` explain the memory layout and why we chose these specific data structures for different use cases.*
### WebSocket Reconnection
The defaults are pretty good, but you can tune them:
```rust
letreconnect_config=ReconnectConfig{
max_retries: 5,// Give up after 5 attempts
base_delay: Duration::from_secs(1),// Start with 1 second delay
max_delay: Duration::from_secs(60),// Cap at 1 minute
Most errors tell you whether they're worth retrying or if you should give up.
## What's Different From Other Libraries?
### Performance
Most trading libraries are built for "demo day" - they work fine for small examples but fall apart under real load. This one is designed for people who actually need to process thousands of updates per second.
### Tick Alignment
The library enforces price tick alignment automatically. If someone sends you a price that doesn't align to the market's tick size (like $0.6543 when the tick size is $0.01), it gets rejected. This prevents weird pricing bugs.
*The tick alignment code includes detailed comments about why this matters for market integrity and how the integer math makes validation nearly free.*
### Memory Management
Order books can grow huge if you're not careful. The library automatically trims them to keep only the relevant price levels, and you can clean up stale books that haven't updated recently.
## Contributing
Found a bug? Have a performance improvement? PRs welcome!
The codebase is designed to be educational as well as functional. Every optimization includes:
- Commented-out "before" code showing the slower approach
- Detailed explanations of why the optimization works
- Performance measurements and memory usage analysis
- References to trading concepts and market microstructure theory
If you're curious about high-frequency trading or high performance Rust, start with `src/book.rs` - it's like a textbook on order book performance engineering.