Module Algostream_risk_management.Circuit_breaker

Portfolio-level circuit breaker with multiple trigger kinds.

State machine mirrors the proven shape used by Algostream_data_ingestion.Connection_supervisor for network failures: Active → Tripped → Recovering. Cooldown is in event time (not wall clock); strategies that need real-time gates run a separate wall-clock-driven scheduler externally.

All time arithmetic uses caller-supplied ts_ns.

type trigger =
  1. | Drawdown_breach of {
    1. current : float;
    2. limit : float;
    }
  2. | Daily_loss_breach of {
    1. current : float;
    2. limit : float;
    }
  3. | Leverage_breach of {
    1. current : float;
    2. limit : float;
    }
  4. | Vol_spike of {
    1. current : float;
    2. baseline : float;
    3. ratio : float;
    }
  5. | Manual of string
type state =
  1. | Active
  2. | Tripped of {
    1. trigger : trigger;
    2. tripped_at_ns : int64;
    }
  3. | Recovering of {
    1. since_ns : int64;
    }
type config = {
  1. max_drawdown : float;
  2. max_daily_loss : float;
  3. max_leverage : float;
  4. vol_spike_ratio : float;
  5. cooldown_ns : int64;
}
type t
val create : config:config -> t
val state : t -> state
val is_tripped : t -> bool
val evaluate : t -> drawdown:float -> daily_pnl:float -> leverage:float -> realized_vol:float -> baseline_vol:float -> ts_ns:int64 -> state
val trip_manual : t -> reason:string -> ts_ns:int64 -> unit
val reset : t -> ts_ns:int64 -> unit
val trigger_to_string : trigger -> string
val state_to_string : state -> string