Queue Position Modeling
Horizon provides a queue position model for estimating fill probabilities and expected fill times for limit orders in prediction market order books. The model tracks your position in the queue, accounts for order arrivals, cancellations, and executions ahead of you, and provides real-time probability estimates. All computation runs in Rust.Queue Tracking
Track your position in the limit order book queue with order arrival and cancellation updates.
Fill Probability
Estimate the probability of your order being filled within a time horizon.
Expected Fill Time
Compute the expected time until your order reaches the front of the queue and gets filled.
Pipeline Integration
hz.queue_tracker() provides live queue analytics within hz.run().Why Queue Position Matters
In prediction markets with limit order books, fill probability depends critically on your position in the queue. Market makers who post limit orders need to know:- Fill probability: Will this order execute before I need to cancel it?
- Expected fill time: How long will I wait? Is it worth the spread?
- Queue dynamics: Are cancellations ahead of me improving my position, or are new arrivals pushing me back?
Queue position modeling is most valuable on markets with deep order books and significant queue depth. On thin markets where you are at or near the front of the queue, the model reduces to simple arrival-rate estimation.
API
hz.QueueModel
Create a queue position model for a specific price level.| Parameter | Type | Description |
|---|---|---|
initial_queue | float | Total queue depth at the price level (positive) |
orders_ahead | float | Volume ahead of your order in the queue (non-negative, must not exceed initial_queue) |
QueueModel Methods
update_book(total_queue, orders_ahead)
Update the model with a new book snapshot. Call this each time the order book changes at your price level.| Parameter | Type | Description |
|---|---|---|
total_queue | float | New total queue depth (positive) |
orders_ahead | float | New volume ahead of your order (non-negative) |
fill_probability(time_horizon)
Estimate the probability of your order being filled within a given time horizon, based on observed arrival and cancellation rates.| Parameter | Type | Description |
|---|---|---|
time_horizon | float | Time horizon in seconds (positive) |
FillProbResult.
expected_fill_time()
Compute the expected time until your order is filled, based on the current arrival rate and queue position.float: expected fill time in seconds. Returns float('inf') if the arrival rate is zero or the queue is stalled.
arrival_rate()
Return the estimated order arrival rate (fills per second) at your price level, computed from observed book updates.float.
cancel_rate()
Return the estimated cancellation rate (cancels per second) ahead of your position.float.
queue_position()
Return your current position in the queue (volume ahead of you).float.
FillProbResult Type
Returned byfill_probability().
| Field | Type | Description |
|---|---|---|
probability | float | Estimated fill probability in [0, 1] |
expected_fills | float | Expected number of fills at this price level in the time horizon |
queue_position | float | Current queue position (orders ahead) |
arrival_rate | float | Estimated arrival rate used in the calculation |
cancel_rate | float | Estimated cancellation rate used in the calculation |
Standalone Fill Probability
hz.queue_fill_prob
One-shot fill probability calculation without maintaining a stateful model.| Parameter | Type | Description |
|---|---|---|
queue_ahead | float | Volume ahead of your order (non-negative) |
arrival_rate | float | Fill arrival rate at this price level (positive) |
cancel_rate | float | Cancellation rate ahead of you (non-negative) |
time_horizon | float | Time horizon in seconds (positive) |
FillProbResult.
The model uses a Poisson process for fill arrivals and an independent Poisson process for cancellations. The effective queue depletion rate is
arrival_rate + cancel_rate, and the probability of reaching the front of the queue is computed via the CDF of the Poisson distribution.Pipeline Integration
hz.queue_tracker
Pipeline function that maintains queue position models for all active orders and injects analytics intoctx.params["queue"].
ctx.params["queue"] dict contains:
| Key | Type | Description |
|---|---|---|
orders | dict[str, dict] | Per-order queue analytics keyed by order ID |
avg_expected_fill_time | float | Average expected fill time across all active orders |
total_queue_ahead | float | Sum of queue depth ahead across all active orders |
best_fill_prob | float | Highest fill probability (60s) among active orders |
orders contains:
| Key | Type | Description |
|---|---|---|
queue_position | float | Volume ahead of this order |
fill_probability_60s | float | Fill probability over 60 seconds |
expected_fill_time | float | Expected time to fill in seconds |
arrival_rate | float | Estimated fill arrival rate |
Mathematical Background
Poisson Queue Model
Poisson Queue Model
The model assumes fills arrive at the price level according to a Poisson process with rate mu, and cancellations ahead of you occur with rate delta. The effective queue depletion rate is lambda = mu + delta.Your order fills when the cumulative depletion exceeds your queue position Q. The probability of this in time T is:
P(fill) = P(Poisson(lambda * T) >= Q)This is computed via the regularized incomplete gamma function for numerical stability.Rate Estimation
Rate Estimation
Arrival and cancellation rates are estimated from observed book updates using exponentially weighted moving averages. This adapts to changing market conditions while filtering out noise from individual book updates.The model requires at least 2 book updates with non-zero time deltas to produce meaningful rate estimates. Before that,
fill_probability() returns conservative estimates.Limitations
Limitations
The Poisson assumption treats arrivals as independent and memoryless. In practice, order flow exhibits clustering (modeled separately by ACD duration models). The queue model provides a first-order approximation that is most accurate over medium time horizons (30-300 seconds) and less reliable for very short or very long horizons.