Class InternalizationEngine

java.lang.Object
com.quantfinlib.crb.InternalizationEngine

public final class InternalizationEngine extends Object
The internalize-or-route decision — the economics that justify a central risk book's existence. Every internalized unit of flow saves the street's spread and market impact twice over (the client's execution and the eventual hedge), so:
  • Risk-reducing flow (opposite sign to the book's net) is internalized up to the offsetting inventory, and the client is given back a share of the saved spread as price improvement — the book was going to pay to shed that risk anyway;
  • Risk-adding flow is warehoused (internalized without improvement) only while the post-trade inventory stays inside the warehouse limit — beyond that it routes out, because a warehouse limit that yields to one more trade is not a limit.

Flows and exposures are in the same factor units (currency notional for EQ:/CCY: factors). Counters accumulate for the internalizationRate() the desk reports. Sign convention matches CentralRiskBook: the flow is what the book absorbs if it internalizes. Deterministic, single-threaded.

  • Constructor Details

    • InternalizationEngine

      public InternalizationEngine(double warehouseLimit, double improvementShare)
      Creates an engine with an empty warehouse.
      Parameters:
      warehouseLimit - max |inventory| the book will hold on a factor after absorbing risk-adding flow, > 0
      improvementShare - fraction of the half spread returned to a risk-reducing client, in [0, 1]
  • Method Details

    • decide

      public InternalizationEngine.Decision decide(double bookNet, double flow, double halfSpreadBps)
      Decides one flow against the book's current net on that factor.
      Parameters:
      bookNet - the book's net exposure on the factor (signed)
      flow - exposure change the book absorbs if it internalizes (signed, non-zero)
      halfSpreadBps - the street's half spread for this risk — what internalizing saves, > 0
    • internalize

      public double internalize(double bookNet, double flow, double halfSpreadBps)
      The same decision as decide(double, double, double) without the result object: returns the internalized notional and leaves the routed notional and the price improvement in lastRouted() and lastImprovementBps(). Allocates nothing, for a per-flow loop.
      Parameters:
      bookNet - the book's current net on the flow's factor (signed)
      flow - incoming client flow (signed, non-zero)
      halfSpreadBps - the street's half spread for this risk, > 0
      Returns:
      signed notional internalized
    • lastRouted

      public double lastRouted()
      Returns:
      signed notional sent to the street
    • lastImprovementBps

      public double lastImprovementBps()
      Returns:
      improvement in bps
    • internalizationRate

      public double internalizationRate()
      Fraction of decided notional the book kept (0 before any flow).
    • internalizedNotional

      public double internalizedNotional()
    • routedNotional

      public double routedNotional()
    • writeState

      public void writeState(DataOutput out) throws IOException
      Serializes the counters plus the configuration they were earned under.
      Throws:
      IOException
    • readState

      public void readState(DataInput in) throws IOException
      Restores counters written by writeState(DataOutput). The configuration must match: an internalization rate earned under one warehouse limit says nothing about a different one, so a mismatch throws rather than silently blending regimes.
      Throws:
      IOException