Class GlobalRiskAggregator

java.lang.Object
com.quantfinlib.trading.GlobalRiskAggregator
All Implemented Interfaces:
AutoCloseable

public final class GlobalRiskAggregator extends Object implements AutoCloseable
Firm-wide risk across shards — the piece sharding deliberately doesn't solve: each shard's HftRiskGate sees only its own symbols, so a "total gross notional across the firm" cap needs someone who can see all of them. This aggregator is that someone, built so the hot paths never pay for it:
  • a monitor thread polls every gate's positions and reference prices (both already published with release/acquire semantics — the VarHandle work done for the gate's own correctness is exactly what makes cross-thread aggregation free);
  • gross notional = Σ |position| × referencePrice over all gates and symbols (symbols without a reference price contribute zero — set references from the market data thread, as the quoting path already does);
  • breach → HftRiskGate.kill(boolean) on EVERY gate: one released boolean, read by each shard's checks as a single acquire load. Recovery is hysteretic: trading resumes only below cap × resumeFraction, so the firm doesn't flap around the limit.

The detection latency is the poll interval (default 1 ms) — a deliberate trade: pre-trade per-order checks stay per-shard and nanosecond-cheap, while the firm-wide cap is a circuit breaker, not a per-order gate. That is how real risk stacks layer it.

  • Constructor Details

    • GlobalRiskAggregator

      public GlobalRiskAggregator(List<HftRiskGate> gates, double maxGrossNotional, double resumeFraction, long pollIntervalNanos)
      Parameters:
      gates - every shard's gate (see ShardedTradingEngine.gates())
      maxGrossNotional - firm-wide cap on Σ |position| × referencePrice
      resumeFraction - resume trading below cap × this (e.g. 0.9)
      pollIntervalNanos - monitor cadence (1_000_000 = 1 ms detection)
  • Method Details

    • grossNotional

      public double grossNotional()
      One sweep over every gate's positions — acquire reads only.
    • isTripped

      public boolean isTripped()
      Whether the firm-wide breaker is currently tripped.
    • lastGrossNotional

      public double lastGrossNotional()
      Gross notional from the most recent monitor sweep.
    • tripCount

      public long tripCount()
      Times the breaker has tripped over this aggregator's life.
    • close

      public void close()
      Stops the monitor; gates keep whatever kill state they last had.
      Specified by:
      close in interface AutoCloseable