Class QueuePositionEstimator

java.lang.Object
com.quantfinlib.microstructure.QueuePositionEstimator

public final class QueuePositionEstimator extends Object
Queue position estimation from L2 data — for when you don't have the L3 feed that marketdata.L3BookBuilder needs to track position exactly. With only aggregated level sizes you can't see individual orders, so this estimator maintains a probabilistic band on where a passive order sits, updated with the standard assumptions:
  • On join — you rest at the back: shares-ahead = the level's current displayed size;
  • Executions at your level — trades hit the front (price-time priority), so they reduce shares-ahead one-for-one;
  • Cancels at your level — the hard part: a decrease in level size that isn't a trade is a cancel, and it could be ahead of you or behind. The literature's workable assumption is pro-rata: a cancel removes shares-ahead in proportion to the fraction of the queue that is ahead of you. That gives an unbiased estimate without L3 — the ahead field is the expected value, and fillProbability(double) turns it into a fill likelihood via QueueModel.

One passive order per instance (cheap — make one per working child). Feed it the level's size changes split into the trade part and the total part; it infers cancels as the residual. Feed ordering contract: report each trade via onTrade(long) BEFORE the depth update that reflects it, and give onLevelResize(long) sizes net of trades already reported — otherwise the same execution is counted once as a trade and again as a cancel, and shares-ahead falls twice per fill. Zero allocation, single writer.

Cross-asset: applies to any price-time-priority level — equity exchange books and FX ECN/matching books alike (sizes are just longs; an FX "share" is a unit of base currency). It does NOT apply to FX LP quote streams, which have no queues — that side is fx.LpRouter's last-look world.

  • Constructor Summary

    Constructors
    Constructor
    Description
     
  • Method Summary

    Modifier and Type
    Method
    Description
    boolean
     
    void
    Order left the book (filled/cancelled).
    double
    fillProbability(double expectedTradedQty)
    Fill probability over a horizon in which expectedTradedQty shares are expected to execute at this level — QueueModel applied to the estimated position.
    void
    join(long levelSize, long ownQty)
    Join the back of a level currently displaying levelSize shares (before our order is added).
    void
    onLevelResize(long newLevelSize)
    The level's displayed size changed to newLevelSize for a reason other than a trade — i.e. cancels (net of any adds behind us).
    void
    onTrade(long tradedQty)
    A trade executed at our level: it consumed tradedQty from the front, so shares-ahead drops by that much (clamped at 0 — once the front reaches us we start filling).
    long
     
    double
    Queue progress since joining: 0 right after join(long, long), 1 when the whole queue that was ahead of us has drained.
    double
    Expected shares ahead of our order right now.

    Methods inherited from class java.lang.Object

    clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
  • Constructor Details

    • QueuePositionEstimator

      public QueuePositionEstimator()
  • Method Details

    • join

      public void join(long levelSize, long ownQty)
      Join the back of a level currently displaying levelSize shares (before our order is added).
    • onTrade

      public void onTrade(long tradedQty)
      A trade executed at our level: it consumed tradedQty from the front, so shares-ahead drops by that much (clamped at 0 — once the front reaches us we start filling).
    • onLevelResize

      public void onLevelResize(long newLevelSize)
      The level's displayed size changed to newLevelSize for a reason other than a trade — i.e. cancels (net of any adds behind us). The removed quantity is attributed pro-rata: the fraction of the queue ahead of us is ahead / (levelSize − ownQty), so that fraction of the cancel came from ahead.
    • sharesAhead

      public double sharesAhead()
      Expected shares ahead of our order right now.
    • fillProbability

      public double fillProbability(double expectedTradedQty)
      Fill probability over a horizon in which expectedTradedQty shares are expected to execute at this level — QueueModel applied to the estimated position.
    • queueProgress

      public double queueProgress()
      Queue progress since joining: 0 right after join(long, long), 1 when the whole queue that was ahead of us has drained. Measured against the shares-ahead AT JOIN — the only meaningful baseline.
    • active

      public boolean active()
    • ownQty

      public long ownQty()
    • close

      public void close()
      Order left the book (filled/cancelled).