feat: integrate scenario engine and playbook into main trading loop (issue #84) #110

Merged
jihoson merged 3 commits from feature/issue-84-main-integration into main 2026-02-09 23:18:24 +09:00
Collaborator

Summary

  • Replace reactive brain.decide() with proactive scenario_engine.evaluate() in the main trading loop
  • Initialize V2 components in run(): ContextSelector, ScenarioEngine, PlaybookStore, PreMarketPlanner
  • Enrich market_data with scanner metrics (RSI, volume_ratio) for ScenarioEngine condition matching
  • Build portfolio_data dict with portfolio_pnl_pct, total_cash, total_eval for global rule evaluation
  • Wrap ScenarioMatch in TradeDecision for backward compatibility with downstream code (order execution, logging, risk)
  • Add scenario match notifications via Telegram when conditions are met
  • Update run_daily_session() to generate playbook via PreMarketPlanner then evaluate per-stock
  • Update realtime loop: playbook generation on first scan per market, periodic rescan, playbook cleanup on market close
  • 6 new integration tests in TestScenarioEngineIntegration:
    • Market data enrichment with/without scanner data
    • Scenario matched notification sent/not sent
    • Decision logger receives scenario match details
    • REDUCE_ALL action doesn't execute orders

Test Plan

  • All 22 tests in tests/test_main.py pass
  • Full test suite: 486 passed (1 pre-existing failure in test_context.py)
  • Ruff lint clean (only pre-existing F841 market_scanner unused variable)
  • Manual verification: paper trading mode with scenario engine

Closes #84

🤖 Generated with Claude Code

## Summary - **Replace reactive `brain.decide()` with proactive `scenario_engine.evaluate()`** in the main trading loop - **Initialize V2 components** in `run()`: `ContextSelector`, `ScenarioEngine`, `PlaybookStore`, `PreMarketPlanner` - **Enrich market_data** with scanner metrics (RSI, volume_ratio) for ScenarioEngine condition matching - **Build portfolio_data** dict with `portfolio_pnl_pct`, `total_cash`, `total_eval` for global rule evaluation - **Wrap ScenarioMatch in TradeDecision** for backward compatibility with downstream code (order execution, logging, risk) - **Add scenario match notifications** via Telegram when conditions are met - **Update run_daily_session()** to generate playbook via PreMarketPlanner then evaluate per-stock - **Update realtime loop**: playbook generation on first scan per market, periodic rescan, playbook cleanup on market close - **6 new integration tests** in `TestScenarioEngineIntegration`: - Market data enrichment with/without scanner data - Scenario matched notification sent/not sent - Decision logger receives scenario match details - REDUCE_ALL action doesn't execute orders ## Test Plan - [x] All 22 tests in `tests/test_main.py` pass - [x] Full test suite: 486 passed (1 pre-existing failure in `test_context.py`) - [x] Ruff lint clean (only pre-existing `F841 market_scanner` unused variable) - [ ] Manual verification: paper trading mode with scenario engine Closes #84 🤖 Generated with [Claude Code](https://claude.com/claude-code)
agentson added 2 commits 2026-02-09 22:47:30 +09:00
Replace brain.decide() with scenario_engine.evaluate() in trading_cycle
and brain.decide_batch() with per-stock scenario evaluation in
run_daily_session. Initialize PreMarketPlanner, ScenarioEngine, and
PlaybookStore in run(). Add pre-market playbook generation on market
open (1 Gemini call per market per day), market_data enrichment from
scanner metrics (rsi, volume_ratio), portfolio_data for global rules,
scenario match notifications, and playbook lifecycle management.

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
fix: resolve lint issues in main.py and test_main.py
Some checks failed
CI / test (pull_request) Has been cancelled
b2312fbe01
Remove unused imports (sys, ScenarioMatch, asyncio, StockPlaybook),
fix import ordering, and split long lines for ruff compliance.

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
agentson added 1 commit 2026-02-09 23:00:11 +09:00
fix: PR review — DB reload, market-local date, market-scoped scan_candidates
Some checks failed
CI / test (pull_request) Has been cancelled
d64e072f06
Address PR #110 review findings:

1. High — Realtime mode now loads playbook from DB before calling Gemini,
   preventing duplicate API calls on process restart (4/day budget).
2. Medium — Pass market-local date (via market.timezone) to
   generate_playbook() and _empty_playbook() instead of date.today().
3. Medium — scan_candidates restructured from {stock_code: candidate}
   to {market_code: {stock_code: candidate}} to prevent KR/US symbol
   collision.

New test: test_scan_candidates_market_scoped verifies cross-market
isolation.

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
jihoson merged commit 9171e54652 into main 2026-02-09 23:18:24 +09:00
Sign in to join this conversation.
No Reviewers
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: jihoson/The-Ouroboros#110