Class HftRiskGate

java.lang.Object
com.quantfinlib.trading.HftRiskGate

public final class HftRiskGate extends Object
Zero-allocation pre-trade risk gate for the HFT order path — the fast-lane counterpart of PreTradeLimitChecker. All state is primitive arrays indexed by dense symbol id; check(int, com.quantfinlib.orderbook.Side, long, double) performs no allocation, no hashing and no string formatting, and returns an int reason code. Rejection counts are kept per reason for observability.

Threading

The production wiring is inherently multi-threaded: check(int, com.quantfinlib.orderbook.Side, long, double) runs on the trading/quoting thread (often the bus consumer), onFill(int, com.quantfinlib.orderbook.Side, long) on the venue-ack thread, halt(int, boolean) from ops/dashboards, and setReferencePrice(int, double) from the market-data thread. Cross-thread element access therefore uses VarHandle acquire/release ordering: on x86 an acquire load is a plain load and a release store a plain store, so the ≈1 ns/check cost is unchanged (verified by re-running HftOrderBenchmark after this change), while readers are guaranteed fresh, untorn values — a plain long[] would let the JIT serve a stale position to the quoter's skew and the position limit forever. onFill uses an atomic add, so multiple fill sources are safe too. Limit configuration remains setup-time single-threaded.

  • Field Details

  • Constructor Details

    • HftRiskGate

      public HftRiskGate(int maxSymbols)
  • Method Details

    • maxOrderQuantity

      public HftRiskGate maxOrderQuantity(long qty)
    • maxOrderNotional

      public HftRiskGate maxOrderNotional(double notional)
    • maxPositionQuantity

      public HftRiskGate maxPositionQuantity(long qty)
    • priceCollarPct

      public HftRiskGate priceCollarPct(double pct)
      Fat-finger guard versus the reference price (0.02 = 2%).
    • halt

      public void halt(int symbolId, boolean isHalted)
      Halts/unhalts a symbol — callable from any thread (ops, dashboards).
    • kill

      public void kill(boolean isKilled)
      Gate-wide kill switch: every check rejects with REJECT_KILLED until cleared. Callable from any thread — this is the lever a cross-shard GlobalRiskAggregator pulls when firm-wide exposure breaches its cap.
    • isKilled

      public boolean isKilled()
      Whether the gate-wide kill switch is currently engaged.
    • referencePrice

      public double referencePrice(int symbolId)
      The collar reference for a symbol (NaN = unset) — readable anywhere.
    • symbolCapacity

      public int symbolCapacity()
      Symbol capacity this gate was sized for (aggregators iterate to it).
    • setReferencePrice

      public void setReferencePrice(int symbolId, double price)
      Updates the collar reference (e.g. from the market data bus).
    • check

      public int check(int symbolId, Side side, long quantity, double price)
      Validates one order. Returns OK or a rejection reason code. Zero allocation; acquire loads only (free on x86).
    • onFill

      public void onFill(int symbolId, Side side, long quantity)
      Applies a fill to the position book — callable from the venue-ack thread (atomic add: concurrent fill sources cannot lose updates).
    • position

      public long position(int symbolId)
      Live position — readable from any thread (quoter skew, hedger, dashboards).
    • rejectionCount

      public long rejectionCount(int reasonCode)
    • reasonName

      public static String reasonName(int code)