LAMIN.NGOBEHGet Brainwashed
← All books

general

5-Gate Risk Engine — Structured Pre-Trade Validation

Unknown

5-Gate Risk Engine

> Adapted from the OctagonAI Kalshi deep trading bot's 5-gate risk system. Every trade must pass through all 5 gates sequentially. Any gate failure blocks the trade (with exceptions for graceful degradation when data is unavailable).

Architecture

Trade Proposal → Gate 1 → Gate 2 → Gate 3 → Gate 4 → Gate 5 → APPROVED
                   ↓         ↓         ↓         ↓         ↓
                 BLOCK     BLOCK     BLOCK     BLOCK     BLOCK

Each gate returns: { passed: boolean, details: string, warnings: string[] } A trade is APPROVED only when ALL 5 gates pass. Gates with insufficient data PASS WITH WARNINGS (graceful degradation) — never hard-block on missing data.

---

Gate 1: Edge Validation

Purpose: Confirm the trade has sufficient mathematical edge.

Checks:

  1. Model probability vs market price → edge ≥ 4% (inviolable minimum)
  2. Expected value > 0 (net positive after fees)
  3. Ensemble variance < 20% (model agreement)
  4. Z-score on mispricing > 1.5 (statistically significant deviation)

Existing Implementation: This is Rohan's current checkRiskLimits() — the foundation that all other gates build upon.

Pass Criteria: Edge ≥ 4% AND EV > 0 AND ensemble std < 20% Hard Block: Edge < 4% — no exceptions without explicit Lamin override.

---

Gate 2: Liquidity Check

Purpose: Ensure the market has sufficient depth to execute without excessive slippage.

Checks:

  1. Orderbook Depth: Total depth on entry side > 10× trade size
  • Your $500 trade needs $5,000+ of visible depth
  • Thin books amplify slippage and create adverse selection risk
  1. Bid-Ask Spread: < 5 cents (0.05)
  • Wider spreads eat into your edge
  • Effective edge = calculated edge - (spread / 2)
  • If effective edge < 4% after spread adjustment, BLOCK
  1. 24h Volume: > $200
  • Low volume = low interest = potential for stale prices
  1. Slippage Estimation: Based on orderbook shape
  • Flat book: low slippage (uniform depth)
  • Thin book: high slippage (concentrated at best price)
  • Estimated slippage > 2% → BLOCK

Graceful Degradation: If orderbook data is unavailable (API down, market not supported), PASS WITH WARNING: "Liquidity unverified — reduce position size by 50%"

---

Gate 3: Correlation Exposure

Purpose: Prevent portfolio concentration in correlated markets.

Checks:

  1. Theme Classification: Categorize the market:
  • POLITICS (elections, policy, appointments)
  • ECONOMICS (GDP, inflation, employment, Fed)
  • CRYPTO (price predictions, regulatory)
  • SPORTS (game outcomes, championships)
  • SCIENCE_TECH (AI, space, climate)
  • ENTERTAINMENT (awards, media)
  • LEGAL (court cases, regulations)
  • GEOPOLITICS (international relations, conflict)
  1. Concentration Limit: No single theme > 30% of portfolio by risk-weighted exposure
  • Risk-weighted exposure = position size × (1 - model confidence / 100)
  • High-confidence positions contribute less to concentration (they're "safer")
  1. Direct Correlation Check: Is the new market a sub-event of an existing position?
  • Example: "Will Biden win?" and "Will a Democrat win?" are directly correlated
  • Directly correlated markets count as a SINGLE position for concentration purposes
  1. Macro Correlation: Are multiple positions exposed to the same macro catalyst?
  • Example: 5 positions all depend on "Fed raises rates" → single-event risk
  • If a single event resolving would affect > 40% of portfolio, BLOCK

Graceful Degradation: If portfolio is empty (no existing positions), PASS (no correlation possible).

---

Gate 4: Regime Detection

Purpose: Identify the current market regime and adjust strategy accordingly.

Regimes:

| Regime | Characteristics | Kelly Adjustment | |--------|----------------|-----------------| | TRENDING | Persistent directional moves, rising volume | 0.5x Kelly (momentum favors larger size) | | MEAN_REVERTING | Price oscillates around a value, decreasing volatility | 0.35x Kelly (standard) | | VOLATILE | Large swings, high volume, no clear direction | 0.2x Kelly (reduced — uncertainty high) | | QUIET | Low volume, narrow range, minimal activity | 0.25x Kelly (standard, but watch for breakout) |

Detection Method:

  1. Rolling Volatility: Compare 7-day volatility to 30-day average
  • Vol ratio > 1.5: VOLATILE
  • Vol ratio < 0.5: QUIET
  1. Directional Persistence: Count consecutive same-direction moves
  • 5+ in same direction: TRENDING
  1. Mean Reversion Signal: Price has moved > 2 std from 30-day mean
  • Combined with vol ratio < 1.0: MEAN_REVERTING
  1. Default: If no strong signal → QUIET (most conservative)

Kelly Fraction Override:

  • The regime-detected Kelly fraction OVERRIDES the default 0.25x if it's MORE conservative
  • It never increases beyond 0.5x (half-Kelly cap remains inviolable)
  • Formula: adjustedKelly = Math.min(regimeKelly, 0.5)

Graceful Degradation: If insufficient price history (< 7 days of data), PASS WITH WARNING, use default 0.25x Kelly.

---

Gate 5: Position Sizing Validation

Purpose: Final position size calculation and validation using regime-adjusted Kelly.

Calculation Flow:

1. Full Kelly: f* = (p × b - q) / b
2. Regime-adjusted Kelly: f_adj = f* × regime_fraction
3. Position size: size = bankroll × f_adj
4. Cap check: size ≤ bankroll × 0.05 (5% max — inviolable)
5. Effective edge after spread: edge_eff = edge - (spread / 2)
6. Minimum viable size: size ≥ $10 (below this, fees eat the edge)

Checks:

  1. Final position ≤ 5% of bankroll (hard cap)
  2. Adding this position doesn't exceed 15 concurrent positions
  3. Daily loss (realized + unrealized) + new risk ≤ 15% ceiling
  4. Current drawdown + worst-case loss on new position ≤ 8% kill switch threshold
  5. Final position size > $10 (otherwise fees erode edge)

Pass Criteria: All 5 checks pass. Block: Any check fails → position either reduced to comply or blocked entirely.

---

5-Gate Response Format

5-GATE RISK VALIDATION
======================

Gate 1 (Edge):         ✅ PASS | Edge: 6.2% | EV: +$0.031 | Ensemble std: 8.1%
Gate 2 (Liquidity):    ✅ PASS | Depth: $12,400 | Spread: $0.03 | Est. slippage: 0.8%
Gate 3 (Correlation):  ⚠️ PASS | Theme: POLITICS (22% exposure) | Warning: approaching 30% limit
Gate 4 (Regime):       ✅ PASS | Regime: MEAN_REVERTING | Kelly adj: 0.35x
Gate 5 (Sizing):       ✅ PASS | Size: $340 (3.4% bankroll) | Positions: 8/15

VERDICT: APPROVED
Recommended size: $340
Adjusted Kelly: 0.35x (mean-reverting regime)
Warnings: 1 (correlation approaching limit)

---

Override Protocol

Gates can ONLY be overridden by explicit Lamin command AND confirmation:

  1. Lamin sends override request
  2. Rohan presents the gate failure details and risk implications
  3. Lamin confirms with explicit "CONFIRMED: override Gate [N]"
  4. Override is logged with timestamp, rationale, and Lamin confirmation
  5. Trade proceeds with increased monitoring (check every 30 minutes instead of hourly)

Edge threshold (Gate 1, 4% minimum) and drawdown kill switch (Gate 5, 8%) require DOUBLE confirmation.