Skip to content

feat(guardrails): investment guardrails system for trade discipline#11

Merged
ShuhaoQing merged 8 commits intomainfrom
feat/guardrails
Apr 6, 2026
Merged

feat(guardrails): investment guardrails system for trade discipline#11
ShuhaoQing merged 8 commits intomainfrom
feat/guardrails

Conversation

@ShuhaoQing
Copy link
Copy Markdown
Owner

Summary

  • Rules engine: position size limits (15%), sector concentration (35%), max positions (8), cash reserve (10%) — all configurable via guardrails config
  • Threshold alerts: gain review (+30%), loss review (-10%), rapid change (±10%/week) with original thesis recall
  • Emotion-trade stats: correlates journal emotions with profitability for behavioral feedback
  • Pre-trade data aggregation: single guardrails pre-trade-data command returns everything the Claude Code agent needs for 5-dimension trade review (valuation, position, sector, signals, emotion)
  • Skill enhancement: Workflow 3 (pre-trade review), 3b (implicit emotion detection from language + data), 3c (stop-loss/take-profit guidance)
  • Advisory only: all guardrails warn but never block trades

New files

  • haoinvest/guardrails/ — rules.py, alerts.py, emotion.py, pre_trade_data.py
  • haoinvest/cli/guardrails.py — 4 CLI commands (health-check, alerts, config, pre-trade-data)
  • 4 test files (27 tests total)

Modified files

  • models.py — 11 new Pydantic models
  • db.py — guardrails_config table + 3 new methods
  • config.py — default guardrail constants
  • cli/portfolio.py — advisory pre-trade validation in add-trade
  • SKILL.md — 3 new workflows for trade review and emotion detection

Test plan

  • 305 tests passing (27 new guardrails tests)
  • ruff check + format clean
  • Manual CLI test: guardrails config, health-check, alerts, pre-trade-data

🤖 Generated with Claude Code

Shuhao Qing and others added 8 commits April 6, 2026 16:15
Add Pydantic models for the guardrails system: Severity, AlertType,
GuardrailsConfig, RuleViolation, HealthCheckResult, PositionAlert,
RecentPriceChange, EmotionTradeStats, PortfolioContext,
CurrentPositionInfo, PreTradeData.

Add guardrails_config table (key-value store) and three new DB
methods: get/set guardrails config, get journal entries by emotion.

Add conservative default values in config.py suitable for beginners.

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
…validation

Add guardrails/rules.py with:
- load_config(): load from DB with fallback to defaults
- health_check(): check portfolio against 4 rules (single position,
  sector concentration, total positions, cash reserve)
- validate_trade(): simulate a trade and return post-trade violations

Allocation computed from market value (not cost basis). Sector info
cached with 7-day TTL. All checks advisory-only.

12 unit tests covering all rules and edge cases.

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
Add guardrails/alerts.py with:
- scan_alerts(): check all positions against gain/loss/rapid-change thresholds
- get_recent_price_change(): calculate 1-week and 1-month price movement
- Includes holding days, original thesis from journal, graceful None on missing data

7 unit tests covering all alert types and edge cases.

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
Add guardrails/emotion.py with:
- get_emotion_trade_stats(): basic stats without current prices
- get_emotion_trade_stats_with_prices(): accurate profitability using
  current prices (preferred for pre-trade-data aggregation)

Simplified profitability rule: symbol's current price > avg_cost.
4 unit tests.

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
Add guardrails/pre_trade_data.py — single-call aggregation of all data
needed for agent pre-trade review:
- Simulated rule violations (what-if)
- Portfolio context (total positions, market value, sector allocations)
- Current position info (quantity, PnL, holding days)
- Symbol alerts, recent price change, emotion stats, original thesis

Agent needs only 2 CLI calls: analyze report + guardrails pre-trade-data.
4 unit tests including empty portfolio and missing data edge cases.

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
Add cli/guardrails.py with 4 commands:
- health-check: check portfolio against rules (human-friendly)
- alerts: scan positions for threshold violations
- config: view/set guardrail configuration
- pre-trade-data: aggregated data for agent review (--json)

Register guardrails sub-app in cli/__init__.py.
Add advisory pre-trade validation to portfolio add-trade (warnings
only, never blocks trades).

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
…ection workflows

Add Workflow 3 (5-dimension pre-trade review), 3b (implicit emotion detection),
3c (stop-loss/take-profit guidance), and guardrails command reference to SKILL.md.

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
Remove unused imports and apply ruff formatting to all guardrails files.

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
@ShuhaoQing ShuhaoQing merged commit 078e1ce into main Apr 6, 2026
1 check passed
@ShuhaoQing ShuhaoQing deleted the feat/guardrails branch April 6, 2026 09:12
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant