Skip to main content
Ultra Feature. Requires an Ultra subscription. Get started at api.mathematicalcompany.com
What is this? When you place a limit order, you join a queue. The queue model estimates your probability of getting filled by tracking how many orders are ahead of you and how fast orders arrive, cancel, and fill. Use it to decide whether to stay in the queue or cross the spread, and to set realistic fill expectations for your limit orders.

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:
  1. Fill probability: Will this order execute before I need to cancel it?
  2. Expected fill time: How long will I wait? Is it worth the spread?
  3. Queue dynamics: Are cancellations ahead of me improving my position, or are new arrivals pushing me back?
The queue model answers these questions by combining Poisson arrival/cancellation processes with your observed queue position.
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.

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.

fill_probability(time_horizon)

Estimate the probability of your order being filled within a given time horizon, based on observed arrival and cancellation rates.
Returns a FillProbResult.

expected_fill_time()

Compute the expected time until your order is filled, based on the current arrival rate and queue position.
Returns 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.
Returns float.

cancel_rate()

Return the estimated cancellation rate (cancels per second) ahead of your position.
Returns float.

queue_position()

Return your current position in the queue (volume ahead of you).
Returns float.

FillProbResult Type

Returned by fill_probability().

Standalone Fill Probability

hz.queue_fill_prob

One-shot fill probability calculation without maintaining a stateful model.
Returns a 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 into ctx.params["queue"].
The ctx.params["queue"] dict contains: Each order dict in orders contains:

Mathematical Background

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.
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.
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.
Queue position estimates are only as accurate as the book data fed to update_book(). On markets with infrequent book snapshots, the arrival and cancellation rate estimates may be noisy. Increase the update frequency or use larger time horizons for more stable estimates.