Status: ✅ Complete (November 8, 2025). Since #331 the signal is VotingSignal, not an agent, and the CLI's tools are plain functions in src/cli/tools/; nothing here opens trades automatically.
Type: Human-in-Loop Trading Interface
Priority: P0 - Critical
The CLI Trade Assistant is a conversational trading interface that allows users to request trade analysis using natural language. The system uses OpenAI's GPT models for parsing user input and the production VotingSignal for MACD+RSI technical analysis.
- Natural Language Understanding: Parse queries like "is SPY at 600 a good entry?" or "buy 10 AAPL"
- Real Technical Analysis: Production VotingSignal with MACD+RSI voting system
- Portfolio Risk Management: Automatic position sizing based on portfolio percentage
- Interactive Confirmation: Review suggestions before execution
- Two Autonomy Modes: Confirm (default) or Auto-execute
The implementation follows a clean plugin architecture with dependency injection:
┌─────────────────────────────────────────────────────────┐
│ CLI Presentation Layer │
│ (Interactive REPL with commands) │
└──────────────────┬──────────────────────────────────────┘
│
▼
┌─────────────────────────────────────────────────────────┐
│ TradingOrchestrator │
│ (Central coordinator for all UIs) │
└──┬────────┬──────────┬──────────┬────────────────────┬──┘
│ │ │ │ │
▼ ▼ ▼ ▼ ▼
┌──────┐ ┌─────┐ ┌──────┐ ┌──────┐ ┌─────────────┐
│Input │ │Strat│ │Risk │ │Exec │ │Session Store│
│Parser│ │Anlyz│ │Mgr │ │Mgr │ │ (Optional) │
└──────┘ └─────┘ └──────┘ └──────┘ └─────────────┘
│ │ │ │
│ │ │ │
▼ ▼ ▼ ▼
LLMParser Real Simple Alpaca
(OpenAI) Voter Risk Execution
Agent Manager Manager
TradingOrchestrator (src/core/trading_orchestrator.py, the CLI's request pipeline; not an agent):
- Central coordinator for complete trading workflow
- Manages: parse → analyze → risk → suggest → execute
- Used by all presentation layers (CLI, GUI, API)
- Session state management (optional)
InputParser Interface (src/core/interfaces/input_parser.py):
- Abstract interface for parsing user input
- Implementations: LLMParser (OpenAI), RegexParser (future), GUIParser (future)
StrategyAnalyzer Interface (src/core/interfaces/strategy_analyzer.py):
- Abstract interface for trade analysis strategies
- Implementations: RealVoterStrategy (MACD+RSI), OptionsStrategy (future #330)
RiskManager Interface (src/core/interfaces/risk_manager.py):
- Abstract interface for risk assessment
- Implementations: SimpleRiskManager (MVP), PortfolioManager (future #333)
ExecutionManager Interface (src/core/interfaces/execution_manager.py):
- Abstract interface for order execution
- Implementations: AlpacaExecutionManager, SimulatedExecutor (future)
OpenAIService - Dual model configuration:
- gpt-4o-mini: Tool calling and function extraction (fast, cheap)
- o4-mini: Reasoning and analysis (configured in config.json)
llm_service = OpenAIService(
tool_calling_model="gpt-4o-mini",
reasoning_model="o4-mini",
api_key=config.get("OPEN_AI_KEY")
)LLMParser - Natural language parsing:
- Extracts ticker, action, quantity, price from user input
- Auto-correction for common typos ("spy at 60" → "SPY at 600")
- Validation (ticker format, quantity, price ranges)
RealVoterStrategy - Production MACD+RSI analysis:
- Runs the production
VotingSignal(src/trading/instruments/voting_signal.py) - Fetches real market data (Alpaca, Alpha Vantage)
- MACD(13/34/8) + RSI(14) with Fibonacci parameters
- Voting logic: Strong consensus, weak signal, conflict detection
Market Data Fetching:
- 60-day lookback period (configurable)
- Fallback (Alpaca → Alpha Vantage)
- Graceful error handling with HOLD signal on failure
Signal Generation:
- Strong Consensus: MACD and RSI agree → 85% confidence
- Weak Signal: Only one indicator active → 65% confidence
- Conflict: MACD and RSI disagree → HOLD
- Neutral: Both indicators neutral → HOLD
SimpleRiskManager - Portfolio percentage based:
- Default position size: 5% of portfolio
- Max position size: 15% of portfolio (warning)
- Buying power check with fallback ($100k portfolio, 50% buying power)
- Risk/reward ratio calculation
- Warning generation (no blocking)
Position Sizing Logic:
if user_specified_quantity:
quantity = user_quantity
else:
target_value = portfolio_value * (5.0 / 100.0) # 5% default
quantity = int(target_value / entry_price)AlpacaExecutionManager - Bracket order execution:
- Integrates with existing OrderManager
- Places entry + stop-loss + take-profit orders
- Enforces GTC (Good-Til-Canceled) time in force
- Stub mode for testing without broker connection
Order Tracking:
- Returns entry_order_id, stop_order_id, target_order_id
- Order status queries
- Cancel order support
CLISession - Interactive REPL:
- Async-based input loop
- Command handling: /help, /exit, /auto, /confirm
- Two autonomy modes:
- confirm: Review each trade before execution (default)
- auto: Auto-execute approved trades
- Formatted suggestion display
- User confirmation workflow
Display Format:
======================================================================
📊 SPY @ $670.97
======================================================================
⬇️ SELL SUGGESTED
Confidence: 65.0%
📈 Analysis:
• Weak signal: Only MACD signals SELL
• MACD: SELL (histogram: -1.195184)
• RSI: HOLD (value: 49.7)
• ⚠️ Weak signal from single indicator
💰 Entry Plan:
Entry: $670.97
Stop: $684.39 (+2.0%)
Target: $647.49 (-3.5%)
Qty: 7 shares
Order: GTC
📊 Portfolio Impact:
Trade Value: $4,696.79
Portfolio %: 4.7% (after transaction)
Max Loss: $93.94
Risk/Reward: 1.75
Continue? [yes/no]: _
OrchestratorFactory - Component wiring:
- Loads configuration from config.json
- Creates all components with correct dependencies
- Supports stub vs real VotingSignal selection
- Hardcoded configuration (YAML deferred to iteration 2)
factory = OrchestratorFactory()
orchestrator = factory.create(
order_manager=None, # None = stub mode
use_real_voter=True # True = production VotingSignal
)python main.py trade-assistExample Session:
> is SPY at 600 a good entry?
[System fetches market data, analyzes with MACD+RSI]
[Displays full analysis with technical indicators]
Continue? [yes/no]: yes
[Executes bracket order with entry/stop/target]
✅ ORDER PLACED SUCCESSFULLY
7 shares SPY
Entry Order: stub_entry_123
Stop Order: stub_stop_123
Target Order: stub_target_123
/help- Show help and examples/exit- Exit the CLI/auto- Enable auto-execute mode (no confirmation)/confirm- Enable confirm mode (default, requires yes/no)
Review queries:
- "is SPY at 600 a good entry?"
- "should I buy AAPL?"
- "analyze TSLA"
- "what do you think about MSFT?"
Buy orders:
- "buy 10 AAPL"
- "buy 10 AAPL at 200"
- "enter AAPL with 10 shares"
Sell orders:
- "sell 5 TSLA"
- "sell 5 TSLA at 250"
- "exit my AAPL position"
Input: "is SPY at 600 a good entry?"
✅ Parsed: SPY, review action, $600 price
✅ Generated: Entry/stop/target, 8 shares, 75% confidence
Input: "buy 10 AAPL at 200"
✅ Parsed: AAPL, buy action, 10 shares, $200 price
✅ Executed: Bracket order with stub (entry/stop/target IDs)
Input: "sell 5 TSLA"
✅ Parsed: TSLA, sell action, 5 shares
✅ Generated: Full suggestion with analysis
Market Data: Fetched 60 days from Alpaca
Signal: SELL (65% confidence)
MACD Histogram: -1.195184 (bearish)
RSI: 49.7 (neutral)
Verdict: Weak sell signal from MACD
Current Price: $670.97
Market Data: Fetched 60 days from Alpaca
Signal: BUY (65% confidence)
MACD Histogram: 0.111064 (bullish)
RSI: 51.6 (neutral)
Verdict: Weak buy signal from MACD
Current Price: $258.45
Stub VoterStrategy:
Signal: BUY (75% confidence)
Reasoning: "MACD: BUY (bullish crossover) [MVP STUB]"
Real VotingSignal:
Signal: SELL (65% confidence)
Reasoning: "Weak signal: Only MACD signals SELL"
✅ Signals differ based on actual market data
{
"OPEN_AI_KEY": "sk-proj-...",
"OPENAI_TOOL_MODEL": "gpt-4o-mini",
"OPENAI_PROMPT_MODEL": "o4-mini",
"ALPACA_ENDPOINT": "https://paper-api.alpaca.markets/v2",
"ALPACA_PAPER_API_KEY": "...",
"ALPACA_PAPER_SECRET": "..."
}macd_params = {
'fast': 13, # Fibonacci parameter (validated)
'slow': 34, # Fibonacci parameter (validated)
'signal': 8 # Signal line period
}
rsi_params = {
'period': 14, # RSI calculation period
'oversold': 30, # Oversold threshold
'overbought': 70 # Overbought threshold
}src/core/models.py- Data structures (TradeRequest, AnalysisResult, etc.)src/core/trading_orchestrator.py- Central coordinatorsrc/core/interfaces/- Abstract interfaces for all pluginssrc/core/factory.py- Component wiring with config.json
src/services/llm/llm_service.py- LLM abstractionsrc/services/llm/openai_service.py- OpenAI implementationsrc/parsers/llm_parser.py- Natural language parsersrc/strategies/voter_strategy.py- Stub for testingsrc/strategies/real_voter_strategy.py- Runs VotingSignal for CLI requestssrc/risk/simple_risk_manager.py- Portfolio % risk managementsrc/execution/alpaca_execution_manager.py- Alpaca execution
src/presentation/cli/__init__.py- Package initializationsrc/presentation/cli/cli_session.py- Interactive REPL
tests/test_basic.py- Foundation tests (4/4 passing)tests/test_end_to_end.py- Integration tests (4/4 passing)test_real_api.py- Real OpenAI API tests (3/3 passing)test_real_voter.py- Real VotingSignal tests (3/3 passing)
docs/sessions/2025-01-08_plugin_architecture_implementation.md- Implementation trackingdocs/04_development/03_cli_trade_assistant.md- This document
-
Real Alpaca OrderManager Integration (1 hour)
- Connect to actual paper trading account
- Test with real bracket order placement
- Validate order lifecycle management
-
Portfolio Manager (#333) (5-7 hours)
- Replace SimpleRiskManager with PortfolioManager
- Risk-based position sizing
- Sector limits and correlation analysis
- Existing position conflict detection
-
YAML Configuration (2 hours)
- Replace hardcoded factory with YAML-based DI
- Allow component swapping via config files
- orchestrator_config.yaml for plugin selection
- llm_config.yaml for provider settings
-
Rich Formatting (2-3 hours)
- Add colors and tables via
richlibrary - Better visual presentation
- Progress indicators
- Syntax highlighting
- Add colors and tables via
-
Options Analysis (#330) (5-7 hours)
- OptionsStrategy plugin for calls/puts
- Greeks calculation (Delta, Gamma, Theta, Vega)
- Implied volatility analysis
- Open interest and volume analysis
-
Multi-Agent Debate (#331) (7-10 hours)
- Implement agent coordination patterns
- Sequential, group chat, voting modes
- Consensus building with dissenting opinions
- Integration with event bus (#316)
-
Autonomy Expansion (#332) (3-5 hours)
- Per-ticker whitelists
- Conditional auto-execute rules
- Risk-based autonomy levels
- Rule-based delegation
- Plugin Architecture: Clean separation of concerns, easy to test with mocks
- Stub First, Real Later: VoterStrategy stub allowed fast iteration, then swapped for real
- Interface-Driven Design: All components implement interfaces, swappable via factory
- Config.json Integration: Simple configuration without YAML complexity
- Dual Model Strategy: gpt-4o-mini for tool calling, o4-mini for reasoning (cost-effective)
- Import Path Issues: Resolved with sys.path manipulation and absolute imports
- Circular Dependencies: VoterStrategy stub avoided complex data pipeline integration
- Testing Without APIs: Mock-based testing allowed validation without external calls
- Market Data Fetching: Graceful fallback when data unavailable or insufficient
- Always read files before editing: Prevents data loss
- Test incrementally: Foundation tests first, then integration tests
- Document as you build:
docs/sessions/2025-01-08_plugin_architecture_implementation.mdtracked progress - Commit frequently: Small, focused commits with descriptive messages
- Use stubs for MVP: Defer complex integrations until architecture validated
- Natural language parsing: 500-800ms (OpenAI API call)
- Market data fetch: 1-2 seconds (Alpaca API)
- MACD+RSI calculation: <50ms (local computation)
- Total workflow: 2-3 seconds from input to suggestion
- Memory: ~100MB (baseline Python + dependencies)
- API Calls: 1 OpenAI call per request (tool calling)
- Market Data: 60 days cached, reused for analysis
- OpenAI API: $0.0001-0.0002 per request (gpt-4o-mini)
- Alpaca Market Data: Free (included with account)
- Total per trade: <$0.001 (OpenAI + minimal compute)
The CLI Trade Assistant (#308) delivers a human-in-loop trading interface with:
✅ Natural language understanding (OpenAI gpt-4o-mini + o4-mini) ✅ Real technical analysis (VotingSignal MACD+RSI voting) ✅ Portfolio risk management (5% default position sizing) ✅ Interactive confirmation workflow (confirm/auto modes) ✅ Plugin architecture (easy to extend and test) ✅ Complete integration (8/8 tests passing)
Used with: Real API keys and a paper trading account
Next steps: Connect real Alpaca OrderManager for actual order placement
Last Updated: November 8, 2025 Status: ✅ Complete
Status: ✅ Complete (November 27, 2025)
The CLI now includes an interactive help system with categorized commands, keyword search, and detailed examples.
# Show all commands (categorized)
/help
# Get help for specific command
/help buy
/help morning-routine
# Search by keyword
/help search order
/help search stop
# Show trading tips for beginners
/tips- Categorized Commands: workflow, trading, status, config, system
- Keyword Search: Find commands by searching descriptions and tags
- Command Aliases: Multiple ways to invoke commands
- Related Commands: Suggestions for related functionality
- Detailed Examples: Usage patterns for each command
- File:
src/cli/help_system.py - Class:
HelpSystem- Centralized command documentation - Integration:
CLISession._handle_command()routes/helprequests
WORKFLOW - Daily trading routines
morning-routine- Market scan and analysisapprove/reject- Human-in-loop trade approvalmonitor- Position monitoringevening-summary- End-of-day report
TRADING - Execute trades
buy- Place buy orderssell- Close positionscancel- Cancel pending orders
STATUS - Portfolio and positions
show portfolio- Account overviewshow positions- Active holdingsshow orders- Open and recent orderscheck my alerts- Stop/target alerts
CONFIG - Settings
set execution-mode- CONFIRM/AUTO/PAPER/DISABLEDshow execution-mode- Current modeshow config- Trading configuration
SYSTEM - CLI controls
/help- Help system/exit- Exit CLI/toggle- Toggle execution mode/tips- Trading education
- Context-aware help (show help for current workflow step)
- Command history with arrow keys (#362)
- Interactive tutorials
- Command completion/suggestions