feat(guardrails): investment guardrails system for trade discipline#11
Merged
ShuhaoQing merged 8 commits intomainfrom Apr 6, 2026
Merged
feat(guardrails): investment guardrails system for trade discipline#11ShuhaoQing merged 8 commits intomainfrom
ShuhaoQing merged 8 commits intomainfrom
Conversation
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>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
guardrails configguardrails pre-trade-datacommand returns everything the Claude Code agent needs for 5-dimension trade review (valuation, position, sector, signals, emotion)New files
haoinvest/guardrails/— rules.py, alerts.py, emotion.py, pre_trade_data.pyhaoinvest/cli/guardrails.py— 4 CLI commands (health-check, alerts, config, pre-trade-data)Modified files
models.py— 11 new Pydantic modelsdb.py— guardrails_config table + 3 new methodsconfig.py— default guardrail constantscli/portfolio.py— advisory pre-trade validation in add-tradeSKILL.md— 3 new workflows for trade review and emotion detectionTest plan
guardrails config,health-check,alerts,pre-trade-data🤖 Generated with Claude Code