Real-Time Anomaly Detection with Automatic Trading Halts
The Black Swan Detection system continuously monitors market data for anomalies that could indicate impending crisis events. When high-severity events are detected, all live trading runs are automatically halted.
When a black swan warning with severity HIGH or above is detected, all live trading runs are immediately halted. This is a safety measure to protect capital during extreme market conditions.
The detection system analyzes data from the All-Seeing Eye aggregation layer, computing z-scores and correlation changes.
The detectAnomalies() function computes z-scores for each data source against historical means and standard deviations.
| Z-Score Range | Interpretation | Action |
|---|---|---|
| |z| < 2 | Normal range | No action |
| 2 ≤ |z| < 3 | Unusual | Monitor |
| 3 ≤ |z| < 4 | Significant anomaly | Elevated warning |
| |z| ≥ 4 | Extreme anomaly | High+ severity warning |
The system tracks a rolling correlation matrix between data sources. Sudden changes indicate regime shifts:
12 distinct crisis event types are detected based on triggering signal patterns.
| Event Type | Key Signals | Detection Pattern |
|---|---|---|
| Market Crash | VIX spike, volume surge, price collapse | VIX > 35, S&P down > 4% intraday |
| Flash Crash | Extreme volume, rapid price swing, quick reversal | 5%+ move in < 10 min, 50%+ retracement |
| Liquidity Crisis | Bid-ask spread widening, order book depth collapse | Spread 3x normal, MOVE > 150 |
| Currency Crisis | FX volatility, rate differentials, reserve depletion | 5%+ currency move, yield curve inversion |
| Geopolitical | News sentiment, defense stocks, oil spike | Sentiment collapse + energy spike |
| Systemic Risk | Credit spreads, bank CDS, intermarket correlation | Investment grade spread > 200bp |
Four severity levels determine the response action, from monitoring to automatic trading halt.
| Input | Elevated | High | Severe | Extreme |
|---|---|---|---|---|
| Max Z-Score | 2.5 - 3.0 | 3.0 - 4.0 | 4.0 - 5.0 | > 5.0 |
| Correlation Breaks | 1 - 2 | 3 - 4 | 5 - 7 | > 7 |
| Signal Count | 2 - 3 | 4 - 5 | 6 - 8 | > 8 |
The trading executor checks for active black swan warnings on every tick. Live runs are automatically halted when high+ severity warnings exist.
Black swan: {eventType} ({severity})
Example: Black swan: market_crash (high)
Black swan warnings only halt live trading runs. Paper trading continues to operate, allowing users to observe strategy behavior during crisis conditions.
Each warning is matched against 15+ historical crisis events to provide context and precedent.
Historical matching uses cosine similarity on normalized feature vectors:
The BlackSwanWarning object contains comprehensive analysis for each detected event.
| Field | Type | Description |
|---|---|---|
id | string | Unique warning ID (blackswan_xxxxx) |
title | string | Human-readable title with emoji |
eventType | string | One of 12 event types |
severity | string | elevated | high | severe | extreme |
probability | number | Occurrence probability 0-1 |
confidence | number | Detection confidence 0-1 |
anomalyScore | number | Composite anomaly score |
zScores | Record | Per-source z-scores |
correlationBreaks | array | Pairs with correlation changes |
triggeringSignals | array | Data points that triggered detection |
why | string | Root cause analysis |
how | string | Mechanism explanation |
what | string | Impact assessment |
when | string | Timeline prediction |
where | string | Affected sectors/assets |
historicalMatchId | string | Best matching past crisis |
historicalSimilarity | number | Similarity score 0-1 |
suggestedActions | array | Recommended responses |
hedgingStrategies | array | Position protection ideas |
status | string | active | resolved | monitoring |
createdAt | string | Warning timestamp |
Each warning includes event-type-specific hedging recommendations.
| Event Type | Hedging Strategies |
|---|---|
| Market Crash |
|
| Liquidity Crisis |
|
| Currency Crisis |
|
| Systemic Risk |
|
| Endpoint | Method | Description |
|---|---|---|
/api/predict/v1/all-seeing-eye/warnings |
GET | List all warnings (optional status filter) |
/api/predict/v1/all-seeing-eye/warnings/:id |
GET | Get specific warning details |
/api/predict/v1/all-seeing-eye/warnings/active |
GET | Get active warnings only |
/api/predict/v1/all-seeing-eye/warnings/:id/status |
PATCH | Update warning status (admin) |
/api/predict/v1/all-seeing-eye/historical-patterns |
GET | List historical crisis patterns |
/api/predict/v1/all-seeing-eye/status |
GET | System health and warning count |
| File | Purpose |
|---|---|
packages/be/src/all-seeing-eye/black-swan/detector.ts | Core detection logic |
packages/be/src/all-seeing-eye/black-swan/analyzer.ts | Why/How/What/When/Where analysis |
packages/be/src/all-seeing-eye/black-swan/historical-patterns.ts | Historical crisis matching |
packages/be/src/all-seeing-eye/black-swan/index.ts | Module exports, warning storage |
packages/be/src/algorithms/paper/executor.ts | Kill switch integration (line 1438) |
packages/be/src/all-seeing-eye/orchestrator/index.ts | 5-minute detection cycle |