Compare commits
24 Commits
feature/is
...
93e31cf667
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
93e31cf667 | ||
|
|
cc1489fd7c | ||
| 31b4d0bf1e | |||
|
|
e2275a23b1 | ||
| 7522bb7e66 | |||
|
|
63fa6841a2 | ||
| ece3c5597b | |||
|
|
63f4e49d88 | ||
|
|
e0a6b307a2 | ||
| 75320eb587 | |||
|
|
afb31b7f4b | ||
| a429a9f4da | |||
|
|
d9763def85 | ||
| ab7f0444b2 | |||
|
|
6b3960a3a4 | ||
| 6cad8e74e1 | |||
|
|
86c94cff62 | ||
| 692cb61991 | |||
|
|
392422992b | ||
| cc637a9738 | |||
|
|
8c27473fed | ||
| bde54c7487 | |||
|
|
a14f944fcc | ||
| 56f7405baa |
182
README.md
182
README.md
@@ -1,154 +1,126 @@
|
|||||||
# The Ouroboros — 자가 진화형 AI 투자 시스템
|
# The Ouroboros — 자가 진화형 AI 투자 시스템
|
||||||
|
|
||||||
KIS(한국투자증권) API로 매매하고, Google Gemini로 판단하며, 자체 전략 코드를 TDD 기반으로 진화시키는 자율 주식 트레이딩 에이전트.
|
KIS API 기반 자동매매 + Gemini 기반 장전 전략 생성 + 장중 로컬 시나리오 실행 + 장후 리뷰/진화 루프를 결합한 시스템입니다.
|
||||||
|
|
||||||
## 아키텍처
|
## 현재 상태 (2026-02-16)
|
||||||
|
|
||||||
```
|
- V2 계획 기준 완료: **18/20**
|
||||||
┌─────────────┐ ┌─────────────┐ ┌─────────────┐
|
- 부분 완료: **1/20** (`1-7` 일부 항목)
|
||||||
│ KIS Broker │◄───►│ Main │◄───►│ Gemini Brain│
|
- 미완료: **1/20** (`4-1` Telegram 확장 명령어)
|
||||||
│ (매매 실행) │ │ (거래 루프) │ │ (의사결정) │
|
|
||||||
└─────────────┘ └──────┬──────┘ └─────────────┘
|
|
||||||
│
|
|
||||||
┌──────┴──────┐
|
|
||||||
│Risk Manager │
|
|
||||||
│ (안전장치) │
|
|
||||||
└──────┬──────┘
|
|
||||||
│
|
|
||||||
┌──────┴──────┐
|
|
||||||
│ Evolution │
|
|
||||||
│ (전략 진화) │
|
|
||||||
└─────────────┘
|
|
||||||
```
|
|
||||||
|
|
||||||
## 핵심 모듈
|
핵심 전환은 이미 반영되었습니다.
|
||||||
|
|
||||||
| 모듈 | 파일 | 설명 |
|
- 기존: 장중 `brain.decide()` 실시간 의존
|
||||||
|------|------|------|
|
- 현재: 장전 `DayPlaybook` 생성 + 장중 `ScenarioEngine` 로컬 매칭
|
||||||
| 설정 | `src/config.py` | Pydantic 기반 환경변수 로딩 및 타입 검증 |
|
|
||||||
| 브로커 | `src/broker/kis_api.py` | KIS API 비동기 래퍼 (토큰 갱신, 레이트 리미터, 해시키) |
|
|
||||||
| 두뇌 | `src/brain/gemini_client.py` | Gemini 프롬프트 구성 및 JSON 응답 파싱 |
|
|
||||||
| 방패 | `src/core/risk_manager.py` | 서킷 브레이커 + 팻 핑거 체크 |
|
|
||||||
| 알림 | `src/notifications/telegram_client.py` | 텔레그램 실시간 거래 알림 (선택사항) |
|
|
||||||
| 진화 | `src/evolution/optimizer.py` | 실패 패턴 분석 → 새 전략 생성 → 테스트 → PR |
|
|
||||||
| DB | `src/db.py` | SQLite 거래 로그 기록 |
|
|
||||||
|
|
||||||
## 안전장치
|
## 핵심 구성
|
||||||
|
|
||||||
| 규칙 | 내용 |
|
- `src/main.py`: 시장 루프, 플레이북 생성/적용, EOD 집계, 리뷰/진화 연결
|
||||||
|------|------|
|
- `src/strategy/`: `models`, `pre_market_planner`, `scenario_engine`, `playbook_store`
|
||||||
| 서킷 브레이커 | 일일 손실률 -3.0% 초과 시 전체 매매 중단 (`SystemExit`) |
|
- `src/context/`: `store`, `aggregator`, `scheduler` (L1~L7)
|
||||||
| 팻 핑거 방지 | 주문 금액이 보유 현금의 30% 초과 시 주문 거부 |
|
- `src/evolution/daily_review.py`: 시장별 scorecard/lessons 생성
|
||||||
| 신뢰도 임계값 | Gemini 신뢰도 80 미만이면 강제 HOLD |
|
- `src/dashboard/app.py`: FastAPI 관측 API
|
||||||
| 레이트 리미터 | Leaky Bucket 알고리즘으로 API 호출 제한 |
|
- `src/notifications/telegram_client.py`: 알림 및 명령 핸들러
|
||||||
| 토큰 자동 갱신 | 만료 1분 전 자동으로 Access Token 재발급 |
|
|
||||||
|
|
||||||
## 빠른 시작
|
## Quick Start
|
||||||
|
|
||||||
### 1. 환경 설정
|
### 1. 환경 설정
|
||||||
|
|
||||||
```bash
|
```bash
|
||||||
cp .env.example .env
|
cp .env.example .env
|
||||||
# .env 파일에 KIS API 키와 Gemini API 키 입력
|
|
||||||
```
|
```
|
||||||
|
|
||||||
|
필수 값:
|
||||||
|
|
||||||
|
- `KIS_APP_KEY`
|
||||||
|
- `KIS_APP_SECRET`
|
||||||
|
- `KIS_ACCOUNT_NO`
|
||||||
|
- `GEMINI_API_KEY`
|
||||||
|
|
||||||
### 2. 의존성 설치
|
### 2. 의존성 설치
|
||||||
|
|
||||||
```bash
|
```bash
|
||||||
pip install ".[dev]"
|
pip install -e ".[dev]"
|
||||||
```
|
```
|
||||||
|
|
||||||
### 3. 테스트 실행
|
### 3. 테스트
|
||||||
|
|
||||||
```bash
|
```bash
|
||||||
pytest -v --cov=src --cov-report=term-missing
|
pytest -v --cov=src
|
||||||
|
ruff check src/ tests/
|
||||||
|
mypy src/ --strict
|
||||||
```
|
```
|
||||||
|
|
||||||
### 4. 실행 (모의투자)
|
## 실행
|
||||||
|
|
||||||
|
### 기본 실행
|
||||||
|
|
||||||
```bash
|
```bash
|
||||||
python -m src.main --mode=paper
|
python -m src.main --mode=paper
|
||||||
```
|
```
|
||||||
|
|
||||||
### 5. Docker 실행
|
### 대시보드 포함 실행
|
||||||
|
|
||||||
```bash
|
```bash
|
||||||
docker compose up -d ouroboros
|
python -m src.main --mode=paper --dashboard
|
||||||
```
|
```
|
||||||
|
|
||||||
## 텔레그램 알림 (선택사항)
|
또는 환경변수:
|
||||||
|
|
||||||
거래 실행, 서킷 브레이커 발동, 시스템 상태 등을 텔레그램으로 실시간 알림 받을 수 있습니다.
|
```bash
|
||||||
|
DASHBOARD_ENABLED=true
|
||||||
|
DASHBOARD_HOST=127.0.0.1
|
||||||
|
DASHBOARD_PORT=8080
|
||||||
|
```
|
||||||
|
|
||||||
### 빠른 설정
|
## 주요 API/기능
|
||||||
|
|
||||||
1. **봇 생성**: 텔레그램에서 [@BotFather](https://t.me/BotFather) 메시지 → `/newbot` 명령
|
- 플레이북 저장: `playbooks` 테이블 (`date + market` UNIQUE)
|
||||||
2. **채팅 ID 확인**: [@userinfobot](https://t.me/userinfobot) 메시지 → `/start` 명령
|
- 의사결정/결과 연결: `trades.decision_id` + `DecisionLogger.update_outcome()`
|
||||||
3. **환경변수 설정**: `.env` 파일에 추가
|
- 시장별 scorecard 컨텍스트: `scorecard_KR`, `scorecard_US`
|
||||||
```bash
|
- 컨텍스트 스케줄러: weekly/monthly/quarterly/annual/legacy + cleanup
|
||||||
TELEGRAM_BOT_TOKEN=1234567890:ABCdefGHIjklMNOpqrsTUVwxyz
|
- 대시보드 API:
|
||||||
TELEGRAM_CHAT_ID=123456789
|
- `/api/status`
|
||||||
TELEGRAM_ENABLED=true
|
- `/api/playbook/{date}?market=KR`
|
||||||
```
|
- `/api/scorecard/{date}?market=KR`
|
||||||
4. **테스트**: 봇과 대화 시작 (`/start` 전송) 후 에이전트 실행
|
- `/api/performance?market=all`
|
||||||
|
- `/api/context/{layer}`
|
||||||
|
- `/api/decisions?market=KR`
|
||||||
|
- `/api/scenarios/active?market=US`
|
||||||
|
|
||||||
**상세 문서**: [src/notifications/README.md](src/notifications/README.md)
|
## 현재 갭 (코드 기준)
|
||||||
|
|
||||||
### 알림 종류
|
- `Issue 4-1` 미구현: `/report`, `/scenarios`, `/review`, `/dashboard` Telegram 명령 미등록
|
||||||
|
- `Issue 1-7` 일부 미완:
|
||||||
- 🟢 거래 체결 알림 (BUY/SELL + 신뢰도)
|
- `price_change_pct` 정규화 어댑터 명시 구현 없음
|
||||||
- 🚨 서킷 브레이커 발동 (자동 거래 중단)
|
- 영향: `price_change_pct_above/below` 조건을 사용하는 시나리오는 사실상 매칭 불가(dead path)
|
||||||
- ⚠️ 팻 핑거 차단 (과도한 주문 차단)
|
- HOLD 시 별도 손절 모니터링 플래그 처리 분리 미흡
|
||||||
- ℹ️ 장 시작/종료 알림
|
- 시장 코드 정합성 이슈:
|
||||||
- 📝 시스템 시작/종료 상태
|
- 설정 기본값은 `ENABLED_MARKETS="KR,US"`
|
||||||
|
- 스케줄 정의는 `US_NASDAQ`, `US_NYSE` 중심
|
||||||
**안전장치**: 알림 실패해도 거래는 계속 진행됩니다. 텔레그램 API 오류나 설정 누락이 있어도 거래 시스템은 정상 작동합니다.
|
- 영향: `get_open_markets(["KR", "US"])`에서 `US` 미정의로 US 시장이 누락될 수 있음(런타임 영향)
|
||||||
|
|
||||||
## 테스트
|
## 테스트
|
||||||
|
|
||||||
35개 테스트가 TDD 방식으로 구현 전에 먼저 작성되었습니다.
|
로컬 수집 기준:
|
||||||
|
|
||||||
```
|
```bash
|
||||||
tests/test_risk.py — 서킷 브레이커, 팻 핑거, 통합 검증 (11개)
|
pytest --collect-only -q
|
||||||
tests/test_broker.py — 토큰 관리, 타임아웃, HTTP 에러, 해시키 (6개)
|
# 538 tests collected
|
||||||
tests/test_brain.py — JSON 파싱, 신뢰도 임계값, 비정상 응답 처리 (15개)
|
|
||||||
```
|
```
|
||||||
|
|
||||||
## 기술 스택
|
권장 검증:
|
||||||
|
|
||||||
- **언어**: Python 3.11+ (asyncio 기반)
|
```bash
|
||||||
- **브로커**: KIS Open API (REST)
|
pytest -v --cov=src
|
||||||
- **AI**: Google Gemini Pro
|
ruff check src/ tests/
|
||||||
- **DB**: SQLite
|
mypy src/ --strict
|
||||||
- **검증**: pytest + coverage
|
|
||||||
- **CI/CD**: GitHub Actions
|
|
||||||
- **배포**: Docker + Docker Compose
|
|
||||||
|
|
||||||
## 프로젝트 구조
|
|
||||||
|
|
||||||
```
|
|
||||||
The-Ouroboros/
|
|
||||||
├── .github/workflows/ci.yml # CI 파이프라인
|
|
||||||
├── docs/
|
|
||||||
│ ├── agents.md # AI 에이전트 페르소나 정의
|
|
||||||
│ └── skills.md # 사용 가능한 도구 목록
|
|
||||||
├── src/
|
|
||||||
│ ├── config.py # Pydantic 설정
|
|
||||||
│ ├── logging_config.py # JSON 구조화 로깅
|
|
||||||
│ ├── db.py # SQLite 거래 기록
|
|
||||||
│ ├── main.py # 비동기 거래 루프
|
|
||||||
│ ├── broker/kis_api.py # KIS API 클라이언트
|
|
||||||
│ ├── brain/gemini_client.py # Gemini 의사결정 엔진
|
|
||||||
│ ├── core/risk_manager.py # 리스크 관리
|
|
||||||
│ ├── notifications/telegram_client.py # 텔레그램 알림
|
|
||||||
│ ├── evolution/optimizer.py # 전략 진화 엔진
|
|
||||||
│ └── strategies/base.py # 전략 베이스 클래스
|
|
||||||
├── tests/ # TDD 테스트 스위트
|
|
||||||
├── Dockerfile # 멀티스테이지 빌드
|
|
||||||
├── docker-compose.yml # 서비스 오케스트레이션
|
|
||||||
└── pyproject.toml # 의존성 및 도구 설정
|
|
||||||
```
|
```
|
||||||
|
|
||||||
## 라이선스
|
## 문서
|
||||||
|
|
||||||
이 프로젝트의 라이선스는 [LICENSE](LICENSE) 파일을 참조하세요.
|
- 아키텍처: `docs/architecture.md`
|
||||||
|
- 컨텍스트 트리: `docs/context-tree.md`
|
||||||
|
- 워크플로우: `docs/workflow.md`
|
||||||
|
- 요구사항 로그: `docs/requirements-log.md`
|
||||||
|
- 명령 레퍼런스: `docs/commands.md`
|
||||||
|
|||||||
@@ -2,342 +2,140 @@
|
|||||||
|
|
||||||
## Overview
|
## Overview
|
||||||
|
|
||||||
Self-evolving AI trading agent for global stock markets via KIS (Korea Investment & Securities) API. The main loop in `src/main.py` orchestrates four components across multiple markets with two trading modes: daily (batch API calls) or realtime (per-stock decisions).
|
The Ouroboros V2는 `Proactive` 구조를 중심으로 동작합니다.
|
||||||
|
|
||||||
## Trading Modes
|
- 장전: Gemini 1회 호출로 시장별 `DayPlaybook` 생성
|
||||||
|
- 장중: `ScenarioEngine`이 로컬 조건 매칭으로 의사결정
|
||||||
|
- 장후: `ContextAggregator` + `DailyReviewer`로 성과 집계/교훈 생성
|
||||||
|
|
||||||
The system supports two trading frequency modes controlled by the `TRADE_MODE` environment variable:
|
`main.py`가 아래 컴포넌트를 오케스트레이션합니다.
|
||||||
|
|
||||||
### Daily Mode (default)
|
- `KISBroker` / `OverseasBroker`
|
||||||
|
- `PreMarketPlanner` / `ScenarioEngine` / `PlaybookStore`
|
||||||
|
- `ContextStore` / `ContextAggregator` / `ContextScheduler`
|
||||||
|
- `DailyReviewer` / `EvolutionOptimizer`
|
||||||
|
- `TelegramClient` / `TelegramCommandHandler`
|
||||||
|
|
||||||
Optimized for Gemini Free tier API limits (20 calls/day):
|
안전/운영 컴포넌트도 핵심입니다.
|
||||||
|
|
||||||
- **Batch decisions**: 1 API call per market per session
|
- `RiskManager`: circuit breaker, fat-finger 검증
|
||||||
- **Fixed schedule**: 4 sessions per day at 6-hour intervals (configurable)
|
- `PriorityTaskQueue` + `CriticalityAssessor`: 우선순위/지연 제어
|
||||||
- **API efficiency**: Processes all stocks in a market simultaneously
|
|
||||||
- **Use case**: Free tier users, cost-conscious deployments
|
|
||||||
- **Configuration**:
|
|
||||||
```bash
|
|
||||||
TRADE_MODE=daily
|
|
||||||
DAILY_SESSIONS=4 # Sessions per day (1-10)
|
|
||||||
SESSION_INTERVAL_HOURS=6 # Hours between sessions (1-24)
|
|
||||||
```
|
|
||||||
|
|
||||||
**Example**: With 2 markets (US, KR) and 4 sessions/day = 8 API calls/day (within 20 call limit)
|
## Market Scope
|
||||||
|
|
||||||
### Realtime Mode
|
V2 기본 설정은 `ENABLED_MARKETS="KR,US"` 입니다.
|
||||||
|
|
||||||
High-frequency trading with individual stock analysis:
|
현재 코드 기준 주의점(런타임 영향):
|
||||||
|
|
||||||
- **Per-stock decisions**: 1 API call per stock per cycle
|
- 설정은 `KR,US`를 기본값으로 사용
|
||||||
- **60-second interval**: Continuous monitoring
|
- 스케줄 레이어(`src/markets/schedule.py`)는 `US_NASDAQ`, `US_NYSE` 구조를 아직 유지
|
||||||
- **Use case**: Production deployments with Gemini paid tier
|
- `US` 코드가 스케줄에 직접 정의되지 않아 US 시장 누락 가능성이 있음
|
||||||
- **Configuration**:
|
|
||||||
```bash
|
|
||||||
TRADE_MODE=realtime
|
|
||||||
```
|
|
||||||
|
|
||||||
**Note**: Realtime mode requires Gemini API subscription due to high call volume.
|
## Decision Flow
|
||||||
|
|
||||||
## Core Components
|
### 1) Pre-market
|
||||||
|
|
||||||
### 1. Broker (`src/broker/`)
|
1. `SmartVolatilityScanner.scan()`으로 후보 종목 수집
|
||||||
|
2. `PreMarketPlanner.generate_playbook(market, candidates)` 호출
|
||||||
|
3. 결과를 `PlaybookStore.save()`로 DB 저장
|
||||||
|
4. 실패 시 empty/defensive playbook 사용
|
||||||
|
|
||||||
**KISBroker** (`kis_api.py`) — Async KIS API client for domestic Korean market
|
### 2) In-market
|
||||||
|
|
||||||
- Automatic OAuth token refresh (valid for 24 hours)
|
1. 시장 데이터 + 스캐너 메트릭(`rsi`, `volume_ratio`) 구성
|
||||||
- Leaky-bucket rate limiter (10 requests per second)
|
2. `ScenarioEngine.evaluate(playbook, stock_code, market_data, portfolio_data)`
|
||||||
- POST body hash-key signing for order authentication
|
3. `TradeDecision` 변환 후 주문/로그/알림 처리
|
||||||
- Custom SSL context with disabled hostname verification for VTS (virtual trading) endpoint due to known certificate mismatch
|
4. `decision_logs`와 `trades`를 `decision_id`로 연결
|
||||||
|
|
||||||
**OverseasBroker** (`overseas.py`) — KIS overseas stock API wrapper
|
### 3) End-of-day
|
||||||
|
|
||||||
- Reuses KISBroker infrastructure (session, token, rate limiter) via composition
|
1. `ContextAggregator.aggregate_daily_from_trades(date, market)`
|
||||||
- Supports 9 global markets: US (NASDAQ/NYSE/AMEX), Japan, Hong Kong, China (Shanghai/Shenzhen), Vietnam (Hanoi/HCM)
|
2. `DailyReviewer.generate_scorecard(date, market)`
|
||||||
- Different API endpoints for overseas price/balance/order operations
|
3. `store_scorecard_in_context()`로 `scorecard_{market}` 저장
|
||||||
|
4. `generate_lessons()`로 장후 교훈 생성
|
||||||
|
5. (US 종료 시) `EvolutionOptimizer.evolve()` 실행
|
||||||
|
|
||||||
**Market Schedule** (`src/markets/schedule.py`) — Timezone-aware market management
|
## Risk Policy
|
||||||
|
|
||||||
- `MarketInfo` dataclass with timezone, trading hours, lunch breaks
|
- `RiskManager`는 주문 전 검증을 강제합니다.
|
||||||
- Automatic DST handling via `zoneinfo.ZoneInfo`
|
- circuit breaker: 손실 임계치 하회 시 거래 중단
|
||||||
- `is_market_open()` checks weekends, trading hours, lunch breaks
|
- fat-finger: 주문 금액 과대 시 주문 차단
|
||||||
- `get_open_markets()` returns currently active markets
|
- 실패 시 알림은 보내되, 예외 처리로 루프 안정성 유지
|
||||||
- `get_next_market_open()` finds next market to open and when
|
|
||||||
|
|
||||||
**New API Methods** (added in v0.9.0):
|
## Error Handling Strategy
|
||||||
- `fetch_market_rankings()` — Fetch volume surge rankings from KIS API
|
|
||||||
- `get_daily_prices()` — Fetch OHLCV history for technical analysis
|
|
||||||
|
|
||||||
### 2. Analysis (`src/analysis/`)
|
- API 호출 실패: 재시도(지수 백오프) 후 종목/사이클 스킵
|
||||||
|
- 시나리오/플래너 실패: empty 또는 defensive playbook으로 안전 폴백
|
||||||
|
- Telegram 실패: warning 로깅 후 거래 루프 지속
|
||||||
|
- 대시보드 스레드 실패: warning 로깅 후 메인 트레이딩 루프와 분리 유지
|
||||||
|
|
||||||
**VolatilityAnalyzer** (`volatility.py`) — Technical indicator calculations
|
## Configuration Reference
|
||||||
|
|
||||||
- ATR (Average True Range) for volatility measurement
|
상세 설정은 `src/config.py`를 기준으로 합니다.
|
||||||
- RSI (Relative Strength Index) using Wilder's smoothing method
|
|
||||||
- Price change percentages across multiple timeframes
|
- 거래 모드: `TRADE_MODE`, `DAILY_SESSIONS`, `SESSION_INTERVAL_HOURS`
|
||||||
- Volume surge ratios and price-volume divergence
|
- 전략: `PRE_MARKET_MINUTES`, `MAX_SCENARIOS_PER_STOCK`, `RESCAN_INTERVAL_SECONDS`
|
||||||
- Momentum scoring (0-100 scale)
|
- 시장: `ENABLED_MARKETS`
|
||||||
- Breakout/breakdown pattern detection
|
- 대시보드: `DASHBOARD_ENABLED`, `DASHBOARD_HOST`, `DASHBOARD_PORT`
|
||||||
|
- 알림: `TELEGRAM_*`
|
||||||
**SmartVolatilityScanner** (`smart_scanner.py`) — Python-first filtering pipeline
|
|
||||||
|
## Context Tree
|
||||||
- **Step 1**: Fetch volume rankings from KIS API (top 30 stocks)
|
|
||||||
- **Step 2**: Calculate RSI and volume ratio for each stock
|
레이어 전략:
|
||||||
- **Step 3**: Apply filters:
|
|
||||||
- Volume ratio >= `VOL_MULTIPLIER` (default 2.0x previous day)
|
- `L7~L5`: 시장별 키
|
||||||
- RSI < `RSI_OVERSOLD_THRESHOLD` (30) OR RSI > `RSI_MOMENTUM_THRESHOLD` (70)
|
- `L4~L1`: 글로벌 통합 롤업
|
||||||
- **Step 4**: Score candidates by RSI extremity (60%) + volume surge (40%)
|
|
||||||
- **Step 5**: Return top N candidates (default 3) for AI analysis
|
구현 포인트:
|
||||||
- **Fallback**: Uses static watchlist if ranking API unavailable
|
|
||||||
- **Realtime mode only**: Daily mode uses batch processing for API efficiency
|
- `L7` 쓰기: `volatility_{market}_{stock}` 등
|
||||||
|
- `L6` 집계: `total_pnl_KR`, `trade_count_US` 등
|
||||||
**Benefits:**
|
- `ContextScheduler.run_if_due()`:
|
||||||
- Reduces Gemini API calls from 20-30 stocks to 1-3 qualified candidates
|
- 주간/월간/분기/연간/legacy 집계
|
||||||
- Fast Python-based filtering before expensive AI judgment
|
- 일 1회 `cleanup_expired_contexts()` 호출
|
||||||
- Logs selection context (RSI, volume_ratio, signal, score) for Evolution system
|
|
||||||
|
## Data Model (핵심)
|
||||||
### 3. Brain (`src/brain/gemini_client.py`)
|
|
||||||
|
### `trades`
|
||||||
**GeminiClient** — AI decision engine powered by Google Gemini
|
|
||||||
|
- `market`, `exchange_code`, `selection_context`, `decision_id` 포함
|
||||||
- Constructs structured prompts from market data
|
- SELL 시 `get_latest_buy_trade()`를 통해 원본 BUY `decision_id`를 찾아 결과 업데이트
|
||||||
- Parses JSON responses into `TradeDecision` objects (`action`, `confidence`, `rationale`)
|
|
||||||
- Forces HOLD when confidence < threshold (default 80)
|
### `decision_logs`
|
||||||
- Falls back to safe HOLD on any parse/API error
|
|
||||||
- Handles markdown-wrapped JSON, malformed responses, invalid actions
|
- 의사결정 입력/컨텍스트 스냅샷 저장
|
||||||
|
- `outcome_pnl`, `outcome_accuracy` 업데이트 가능
|
||||||
### 4. Risk Manager (`src/core/risk_manager.py`)
|
|
||||||
|
### `playbooks`
|
||||||
**RiskManager** — Safety circuit breaker and order validation
|
|
||||||
|
- `UNIQUE(date, market)`
|
||||||
⚠️ **READ-ONLY by policy** (see [`docs/agents.md`](./agents.md))
|
- `status`, `token_count`, `scenario_count`, `match_count` 관리
|
||||||
|
|
||||||
- **Circuit Breaker**: Halts all trading via `SystemExit` when daily P&L drops below -3.0%
|
## Dashboard
|
||||||
- Threshold may only be made stricter, never relaxed
|
|
||||||
- Calculated as `(total_eval - purchase_total) / purchase_total * 100`
|
`src/dashboard/app.py`의 FastAPI 앱이 SQLite를 직접 조회합니다.
|
||||||
- **Fat-Finger Protection**: Rejects orders exceeding 30% of available cash
|
|
||||||
- Must always be enforced, cannot be disabled
|
엔드포인트:
|
||||||
|
|
||||||
### 5. Notifications (`src/notifications/telegram_client.py`)
|
- `GET /api/status`
|
||||||
|
- `GET /api/playbook/{date}?market=KR`
|
||||||
**TelegramClient** — Real-time event notifications via Telegram Bot API
|
- `GET /api/scorecard/{date}?market=KR`
|
||||||
|
- `GET /api/performance?market=all`
|
||||||
- Sends alerts for trades, circuit breakers, fat-finger rejections, system events
|
- `GET /api/context/{layer}`
|
||||||
- Non-blocking: failures are logged but never crash trading
|
- `GET /api/decisions?market=KR`
|
||||||
- Rate-limited: 1 message/second default to respect Telegram API limits
|
- `GET /api/scenarios/active?market=US`
|
||||||
- Auto-disabled when credentials missing
|
|
||||||
- Gracefully handles API errors, network timeouts, invalid tokens
|
실행 통합:
|
||||||
|
|
||||||
**Notification Types:**
|
- CLI `--dashboard`
|
||||||
- Trade execution (BUY/SELL with confidence)
|
- 또는 `DASHBOARD_ENABLED=true`
|
||||||
- Circuit breaker trips (critical alert)
|
- `main.py`에서 daemon thread로 uvicorn 실행
|
||||||
- Fat-finger protection triggers (order rejection)
|
|
||||||
- Market open/close events
|
## Known Gaps (2026-02-16)
|
||||||
- System startup/shutdown status
|
|
||||||
|
- `Issue 4-1` Telegram 확장 명령 미구현 (`/report`, `/scenarios`, `/review`, `/dashboard`)
|
||||||
**Setup:** See [src/notifications/README.md](../src/notifications/README.md) for bot creation and configuration.
|
- `Issue 1-7` 일부 미완:
|
||||||
|
- `price_change_pct` 정규화 계층 명시 미흡
|
||||||
### 6. Evolution (`src/evolution/optimizer.py`)
|
- 영향: `price_change_pct` 기반 조건은 현재 사실상 매칭되지 않음
|
||||||
|
- HOLD 시 별도 손절 모니터링 플래그 처리 미완
|
||||||
**StrategyOptimizer** — Self-improvement loop
|
- US 스캐닝 확장(`fetch_overseas_rankings`) 미구현
|
||||||
|
|
||||||
- Analyzes high-confidence losing trades from SQLite
|
|
||||||
- Asks Gemini to generate new `BaseStrategy` subclasses
|
|
||||||
- Validates generated strategies by running full pytest suite
|
|
||||||
- Simulates PR creation for human review
|
|
||||||
- Only activates strategies that pass all tests
|
|
||||||
|
|
||||||
## Data Flow
|
|
||||||
|
|
||||||
### Realtime Mode (with Smart Scanner)
|
|
||||||
|
|
||||||
```
|
|
||||||
┌─────────────────────────────────────────────────────────────┐
|
|
||||||
│ Main Loop (60s cycle per market) │
|
|
||||||
└─────────────────────────────────────────────────────────────┘
|
|
||||||
│
|
|
||||||
▼
|
|
||||||
┌──────────────────────────────────┐
|
|
||||||
│ Market Schedule Check │
|
|
||||||
│ - Get open markets │
|
|
||||||
│ - Filter by enabled markets │
|
|
||||||
│ - Wait if all closed │
|
|
||||||
└──────────────────┬────────────────┘
|
|
||||||
│
|
|
||||||
▼
|
|
||||||
┌──────────────────────────────────┐
|
|
||||||
│ Smart Scanner (Python-first) │
|
|
||||||
│ - Fetch volume rankings (KIS) │
|
|
||||||
│ - Get 20d price history per stock│
|
|
||||||
│ - Calculate RSI(14) + vol ratio │
|
|
||||||
│ - Filter: vol>2x AND RSI extreme │
|
|
||||||
│ - Return top 3 qualified stocks │
|
|
||||||
└──────────────────┬────────────────┘
|
|
||||||
│
|
|
||||||
▼
|
|
||||||
┌──────────────────────────────────┐
|
|
||||||
│ For Each Qualified Candidate │
|
|
||||||
└──────────────────┬────────────────┘
|
|
||||||
│
|
|
||||||
▼
|
|
||||||
┌──────────────────────────────────┐
|
|
||||||
│ Broker: Fetch Market Data │
|
|
||||||
│ - Domestic: orderbook + balance │
|
|
||||||
│ - Overseas: price + balance │
|
|
||||||
└──────────────────┬────────────────┘
|
|
||||||
│
|
|
||||||
▼
|
|
||||||
┌──────────────────────────────────┐
|
|
||||||
│ Calculate P&L │
|
|
||||||
│ pnl_pct = (eval - cost) / cost │
|
|
||||||
└──────────────────┬────────────────┘
|
|
||||||
│
|
|
||||||
▼
|
|
||||||
┌──────────────────────────────────┐
|
|
||||||
│ Brain: Get Decision (AI) │
|
|
||||||
│ - Build prompt with market data │
|
|
||||||
│ - Call Gemini API │
|
|
||||||
│ - Parse JSON response │
|
|
||||||
│ - Return TradeDecision │
|
|
||||||
└──────────────────┬────────────────┘
|
|
||||||
│
|
|
||||||
▼
|
|
||||||
┌──────────────────────────────────┐
|
|
||||||
│ Risk Manager: Validate Order │
|
|
||||||
│ - Check circuit breaker │
|
|
||||||
│ - Check fat-finger limit │
|
|
||||||
│ - Raise if validation fails │
|
|
||||||
└──────────────────┬────────────────┘
|
|
||||||
│
|
|
||||||
▼
|
|
||||||
┌──────────────────────────────────┐
|
|
||||||
│ Broker: Execute Order │
|
|
||||||
│ - Domestic: send_order() │
|
|
||||||
│ - Overseas: send_overseas_order() │
|
|
||||||
└──────────────────┬────────────────┘
|
|
||||||
│
|
|
||||||
▼
|
|
||||||
┌──────────────────────────────────┐
|
|
||||||
│ Notifications: Send Alert │
|
|
||||||
│ - Trade execution notification │
|
|
||||||
│ - Non-blocking (errors logged) │
|
|
||||||
│ - Rate-limited to 1/sec │
|
|
||||||
└──────────────────┬────────────────┘
|
|
||||||
│
|
|
||||||
▼
|
|
||||||
┌──────────────────────────────────┐
|
|
||||||
│ Database: Log Trade │
|
|
||||||
│ - SQLite (data/trades.db) │
|
|
||||||
│ - Track: action, confidence, │
|
|
||||||
│ rationale, market, exchange │
|
|
||||||
│ - NEW: selection_context (JSON) │
|
|
||||||
│ - RSI, volume_ratio, signal │
|
|
||||||
│ - For Evolution optimization │
|
|
||||||
└───────────────────────────────────┘
|
|
||||||
```
|
|
||||||
|
|
||||||
## Database Schema
|
|
||||||
|
|
||||||
**SQLite** (`src/db.py`)
|
|
||||||
|
|
||||||
```sql
|
|
||||||
CREATE TABLE trades (
|
|
||||||
id INTEGER PRIMARY KEY AUTOINCREMENT,
|
|
||||||
timestamp TEXT NOT NULL,
|
|
||||||
stock_code TEXT NOT NULL,
|
|
||||||
action TEXT NOT NULL, -- BUY | SELL | HOLD
|
|
||||||
confidence INTEGER NOT NULL, -- 0-100
|
|
||||||
rationale TEXT,
|
|
||||||
quantity INTEGER,
|
|
||||||
price REAL,
|
|
||||||
pnl REAL DEFAULT 0.0,
|
|
||||||
market TEXT DEFAULT 'KR', -- KR | US_NASDAQ | JP | etc.
|
|
||||||
exchange_code TEXT DEFAULT 'KRX', -- KRX | NASD | NYSE | etc.
|
|
||||||
selection_context TEXT -- JSON: {rsi, volume_ratio, signal, score}
|
|
||||||
);
|
|
||||||
```
|
|
||||||
|
|
||||||
**Selection Context** (new in v0.9.0): Stores scanner selection criteria as JSON:
|
|
||||||
```json
|
|
||||||
{
|
|
||||||
"rsi": 28.5,
|
|
||||||
"volume_ratio": 2.7,
|
|
||||||
"signal": "oversold",
|
|
||||||
"score": 85.2
|
|
||||||
}
|
|
||||||
```
|
|
||||||
|
|
||||||
Enables Evolution system to analyze correlation between selection criteria and trade outcomes.
|
|
||||||
|
|
||||||
Auto-migration: Adds `market`, `exchange_code`, and `selection_context` columns if missing for backward compatibility.
|
|
||||||
|
|
||||||
## Configuration
|
|
||||||
|
|
||||||
**Pydantic Settings** (`src/config.py`)
|
|
||||||
|
|
||||||
Loaded from `.env` file:
|
|
||||||
|
|
||||||
```bash
|
|
||||||
# Required
|
|
||||||
KIS_APP_KEY=your_app_key
|
|
||||||
KIS_APP_SECRET=your_app_secret
|
|
||||||
KIS_ACCOUNT_NO=XXXXXXXX-XX
|
|
||||||
GEMINI_API_KEY=your_gemini_key
|
|
||||||
|
|
||||||
# Optional
|
|
||||||
MODE=paper # paper | live
|
|
||||||
DB_PATH=data/trades.db
|
|
||||||
CONFIDENCE_THRESHOLD=80
|
|
||||||
MAX_LOSS_PCT=3.0
|
|
||||||
MAX_ORDER_PCT=30.0
|
|
||||||
ENABLED_MARKETS=KR,US_NASDAQ # Comma-separated market codes
|
|
||||||
|
|
||||||
# Trading Mode (API efficiency)
|
|
||||||
TRADE_MODE=daily # daily | realtime
|
|
||||||
DAILY_SESSIONS=4 # Sessions per day (daily mode only)
|
|
||||||
SESSION_INTERVAL_HOURS=6 # Hours between sessions (daily mode only)
|
|
||||||
|
|
||||||
# Telegram Notifications (optional)
|
|
||||||
TELEGRAM_BOT_TOKEN=1234567890:ABCdefGHIjklMNOpqrsTUVwxyz
|
|
||||||
TELEGRAM_CHAT_ID=123456789
|
|
||||||
TELEGRAM_ENABLED=true
|
|
||||||
|
|
||||||
# Smart Scanner (optional, realtime mode only)
|
|
||||||
RSI_OVERSOLD_THRESHOLD=30 # 0-50, oversold threshold
|
|
||||||
RSI_MOMENTUM_THRESHOLD=70 # 50-100, momentum threshold
|
|
||||||
VOL_MULTIPLIER=2.0 # Minimum volume ratio (2.0 = 200%)
|
|
||||||
SCANNER_TOP_N=3 # Max qualified candidates per scan
|
|
||||||
```
|
|
||||||
|
|
||||||
Tests use in-memory SQLite (`DB_PATH=":memory:"`) and dummy credentials via `tests/conftest.py`.
|
|
||||||
|
|
||||||
## Error Handling
|
|
||||||
|
|
||||||
### Connection Errors (Broker API)
|
|
||||||
- Retry with exponential backoff (2^attempt seconds)
|
|
||||||
- Max 3 retries per stock
|
|
||||||
- After exhaustion, skip stock and continue with next
|
|
||||||
|
|
||||||
### API Quota Errors (Gemini)
|
|
||||||
- Return safe HOLD decision with confidence=0
|
|
||||||
- Log error but don't crash
|
|
||||||
- Agent continues trading on next cycle
|
|
||||||
|
|
||||||
### Circuit Breaker Tripped
|
|
||||||
- Immediately halt via `SystemExit`
|
|
||||||
- Log critical message
|
|
||||||
- Requires manual intervention to restart
|
|
||||||
|
|
||||||
### Market Closed
|
|
||||||
- Wait until next market opens
|
|
||||||
- Use `get_next_market_open()` to calculate wait time
|
|
||||||
- Sleep until market open time
|
|
||||||
|
|
||||||
### Telegram API Errors
|
|
||||||
- Log warning but continue trading
|
|
||||||
- Missing credentials → auto-disable notifications
|
|
||||||
- Network timeout → skip notification, no retry
|
|
||||||
- Invalid token → log error, trading unaffected
|
|
||||||
- Rate limit exceeded → queued via rate limiter
|
|
||||||
|
|
||||||
**Guarantee**: Notification failures never interrupt trading operations.
|
|
||||||
|
|||||||
216
docs/commands.md
216
docs/commands.md
@@ -1,156 +1,82 @@
|
|||||||
# Command Reference
|
# Command Reference
|
||||||
|
|
||||||
## Common Command Failures
|
## Core Runtime Commands
|
||||||
|
|
||||||
**Critical: Learn from failures. Never repeat the same failed command without modification.**
|
|
||||||
|
|
||||||
### tea CLI (Gitea Command Line Tool)
|
|
||||||
|
|
||||||
#### ❌ TTY Error - Interactive Confirmation Fails
|
|
||||||
```bash
|
|
||||||
~/bin/tea issues create --repo X --title "Y" --description "Z"
|
|
||||||
# Error: huh: could not open a new TTY: open /dev/tty: no such device or address
|
|
||||||
```
|
|
||||||
**💡 Reason:** tea tries to open `/dev/tty` for interactive confirmation prompts, which is unavailable in non-interactive environments.
|
|
||||||
|
|
||||||
**✅ Solution:** Use `YES=""` environment variable to bypass confirmation
|
|
||||||
```bash
|
|
||||||
YES="" ~/bin/tea issues create --repo jihoson/The-Ouroboros --title "Title" --description "Body"
|
|
||||||
YES="" ~/bin/tea issues edit <number> --repo jihoson/The-Ouroboros --description "Updated body"
|
|
||||||
YES="" ~/bin/tea pulls create --repo jihoson/The-Ouroboros --head feature-branch --base main --title "Title" --description "Body"
|
|
||||||
```
|
|
||||||
|
|
||||||
**📝 Notes:**
|
|
||||||
- Always set default login: `~/bin/tea login default local`
|
|
||||||
- Use `--repo jihoson/The-Ouroboros` when outside repo directory
|
|
||||||
- tea is preferred over direct Gitea API calls for consistency
|
|
||||||
|
|
||||||
#### ❌ Wrong Parameter Name
|
|
||||||
```bash
|
|
||||||
tea issues create --body "text"
|
|
||||||
# Error: flag provided but not defined: -body
|
|
||||||
```
|
|
||||||
**💡 Reason:** Parameter is `--description`, not `--body`.
|
|
||||||
|
|
||||||
**✅ Solution:** Use correct parameter name
|
|
||||||
```bash
|
|
||||||
YES="" ~/bin/tea issues create --description "text"
|
|
||||||
```
|
|
||||||
|
|
||||||
### Gitea API (Direct HTTP Calls)
|
|
||||||
|
|
||||||
#### ❌ Wrong Hostname
|
|
||||||
```bash
|
|
||||||
curl http://gitea.local:3000/api/v1/...
|
|
||||||
# Error: Could not resolve host: gitea.local
|
|
||||||
```
|
|
||||||
**💡 Reason:** Gitea instance runs on `localhost:3000`, not `gitea.local`.
|
|
||||||
|
|
||||||
**✅ Solution:** Use correct hostname (but prefer tea CLI)
|
|
||||||
```bash
|
|
||||||
curl http://localhost:3000/api/v1/repos/jihoson/The-Ouroboros/issues \
|
|
||||||
-H "Authorization: token $GITEA_TOKEN" \
|
|
||||||
-H "Content-Type: application/json" \
|
|
||||||
-d '{"title":"...", "body":"..."}'
|
|
||||||
```
|
|
||||||
|
|
||||||
**📝 Notes:**
|
|
||||||
- Prefer `tea` CLI over direct API calls
|
|
||||||
- Only use curl for operations tea doesn't support
|
|
||||||
|
|
||||||
### Git Commands
|
|
||||||
|
|
||||||
#### ❌ User Not Configured
|
|
||||||
```bash
|
|
||||||
git commit -m "message"
|
|
||||||
# Error: Author identity unknown
|
|
||||||
```
|
|
||||||
**💡 Reason:** Git user.name and user.email not set.
|
|
||||||
|
|
||||||
**✅ Solution:** Configure git user
|
|
||||||
```bash
|
|
||||||
git config user.name "agentson"
|
|
||||||
git config user.email "agentson@localhost"
|
|
||||||
```
|
|
||||||
|
|
||||||
#### ❌ Permission Denied on Push
|
|
||||||
```bash
|
|
||||||
git push origin branch
|
|
||||||
# Error: User permission denied for writing
|
|
||||||
```
|
|
||||||
**💡 Reason:** Repository access token lacks write permissions or user lacks repo write access.
|
|
||||||
|
|
||||||
**✅ Solution:**
|
|
||||||
1. Verify user has write access to repository (admin grants this)
|
|
||||||
2. Ensure git credential has correct token with `write:repository` scope
|
|
||||||
3. Check remote URL uses correct authentication
|
|
||||||
|
|
||||||
### Python/Pytest
|
|
||||||
|
|
||||||
#### ❌ Module Import Error
|
|
||||||
```bash
|
|
||||||
pytest tests/test_foo.py
|
|
||||||
# ModuleNotFoundError: No module named 'src'
|
|
||||||
```
|
|
||||||
**💡 Reason:** Package not installed in development mode.
|
|
||||||
|
|
||||||
**✅ Solution:** Install package with dev dependencies
|
|
||||||
```bash
|
|
||||||
pip install -e ".[dev]"
|
|
||||||
```
|
|
||||||
|
|
||||||
#### ❌ Async Test Hangs
|
|
||||||
```python
|
|
||||||
async def test_something(): # Hangs forever
|
|
||||||
result = await async_function()
|
|
||||||
```
|
|
||||||
**💡 Reason:** Missing pytest-asyncio or wrong configuration.
|
|
||||||
|
|
||||||
**✅ Solution:** Already configured in pyproject.toml
|
|
||||||
```toml
|
|
||||||
[tool.pytest.ini_options]
|
|
||||||
asyncio_mode = "auto"
|
|
||||||
```
|
|
||||||
No decorator needed for async tests.
|
|
||||||
|
|
||||||
## Build & Test Commands
|
|
||||||
|
|
||||||
```bash
|
```bash
|
||||||
# Install all dependencies (production + dev)
|
# run (paper)
|
||||||
pip install -e ".[dev]"
|
|
||||||
|
|
||||||
# Run full test suite with coverage
|
|
||||||
pytest -v --cov=src --cov-report=term-missing
|
|
||||||
|
|
||||||
# Run a single test file
|
|
||||||
pytest tests/test_risk.py -v
|
|
||||||
|
|
||||||
# Run a single test by name
|
|
||||||
pytest tests/test_brain.py -k "test_parse_valid_json" -v
|
|
||||||
|
|
||||||
# Lint
|
|
||||||
ruff check src/ tests/
|
|
||||||
|
|
||||||
# Type check (strict mode, non-blocking in CI)
|
|
||||||
mypy src/ --strict
|
|
||||||
|
|
||||||
# Run the trading agent
|
|
||||||
python -m src.main --mode=paper
|
python -m src.main --mode=paper
|
||||||
|
|
||||||
# Docker
|
# run with dashboard thread
|
||||||
docker compose up -d ouroboros # Run agent
|
python -m src.main --mode=paper --dashboard
|
||||||
docker compose --profile test up test # Run tests in container
|
|
||||||
|
# tests
|
||||||
|
pytest -v --cov=src
|
||||||
|
|
||||||
|
# lint
|
||||||
|
ruff check src/ tests/
|
||||||
|
|
||||||
|
# type-check
|
||||||
|
mypy src/ --strict
|
||||||
```
|
```
|
||||||
|
|
||||||
## Environment Setup
|
## Dashboard Runtime Controls
|
||||||
|
|
||||||
|
`Issue 4-3` 기준 반영:
|
||||||
|
|
||||||
|
- CLI: `--dashboard`
|
||||||
|
- ENV: `DASHBOARD_ENABLED=true`
|
||||||
|
- Host/Port:
|
||||||
|
- `DASHBOARD_HOST` (default `127.0.0.1`)
|
||||||
|
- `DASHBOARD_PORT` (default `8080`)
|
||||||
|
|
||||||
|
## Telegram Commands (현재 구현)
|
||||||
|
|
||||||
|
`main.py` 등록 기준:
|
||||||
|
|
||||||
|
- `/help`
|
||||||
|
- `/status`
|
||||||
|
- `/positions`
|
||||||
|
- `/stop`
|
||||||
|
- `/resume`
|
||||||
|
|
||||||
|
## Telegram Commands (미구현 상태)
|
||||||
|
|
||||||
|
V2 플랜 `Issue 4-1` 항목은 아직 미구현:
|
||||||
|
|
||||||
|
- `/report [KR|US]`
|
||||||
|
- `/scenarios [KR|US]`
|
||||||
|
- `/review [KR|US]`
|
||||||
|
- `/dashboard`
|
||||||
|
|
||||||
|
## Gitea / tea Workflow Commands
|
||||||
|
|
||||||
|
이슈 선등록 후 작업 시작:
|
||||||
|
|
||||||
```bash
|
```bash
|
||||||
# Create .env file from example
|
YES="" ~/bin/tea issues create \
|
||||||
cp .env.example .env
|
--repo jihoson/The-Ouroboros \
|
||||||
|
--title "..." \
|
||||||
# Edit .env with your credentials
|
--description "..."
|
||||||
# Required: KIS_APP_KEY, KIS_APP_SECRET, KIS_ACCOUNT_NO, GEMINI_API_KEY
|
|
||||||
|
|
||||||
# Verify configuration
|
|
||||||
python -c "from src.config import Settings; print(Settings())"
|
|
||||||
```
|
```
|
||||||
|
|
||||||
|
작업은 `worktree` 기준 권장:
|
||||||
|
|
||||||
|
```bash
|
||||||
|
git worktree add ../The-Ouroboros-issue-<N> feature/issue-<N>-<slug>
|
||||||
|
```
|
||||||
|
|
||||||
|
PR 생성:
|
||||||
|
|
||||||
|
```bash
|
||||||
|
YES="" ~/bin/tea pulls create \
|
||||||
|
--repo jihoson/The-Ouroboros \
|
||||||
|
--head feature/issue-<N>-<slug> \
|
||||||
|
--base main \
|
||||||
|
--title "..." \
|
||||||
|
--description "..."
|
||||||
|
```
|
||||||
|
|
||||||
|
## Known tea CLI Gotcha
|
||||||
|
|
||||||
|
TTY 없는 환경에서는 `tea` 확인 프롬프트가 실패할 수 있습니다.
|
||||||
|
항상 `YES=""`를 붙여 비대화식으로 실행하세요.
|
||||||
|
|||||||
@@ -1,243 +1,81 @@
|
|||||||
# Context Tree: Multi-Layered Memory Management
|
# Context Tree: Multi-Layered Memory Management
|
||||||
|
|
||||||
The context tree implements **Pillar 2** of The Ouroboros: hierarchical memory management across 7 time horizons, from real-time market data to generational trading wisdom.
|
## Summary
|
||||||
|
|
||||||
## Overview
|
컨텍스트 트리는 L7(실시간)부터 L1(레거시)까지 계층화된 메모리 구조입니다.
|
||||||
|
|
||||||
Instead of a flat memory structure, The Ouroboros maintains a **7-tier context tree** where each layer represents a different time horizon and level of abstraction:
|
- L7~L5: 시장별 독립 데이터 중심
|
||||||
|
- L4~L1: 글로벌 포트폴리오 통합 데이터
|
||||||
|
|
||||||
```
|
## Layer Policy
|
||||||
L1 (Legacy) ← Cumulative wisdom across generations
|
|
||||||
↑
|
|
||||||
L2 (Annual) ← Yearly performance metrics
|
|
||||||
↑
|
|
||||||
L3 (Quarterly) ← Quarterly strategy adjustments
|
|
||||||
↑
|
|
||||||
L4 (Monthly) ← Monthly portfolio rebalancing
|
|
||||||
↑
|
|
||||||
L5 (Weekly) ← Weekly stock selection
|
|
||||||
↑
|
|
||||||
L6 (Daily) ← Daily trade logs
|
|
||||||
↑
|
|
||||||
L7 (Real-time) ← Live market data
|
|
||||||
```
|
|
||||||
|
|
||||||
Data flows **bottom-up**: real-time trades aggregate into daily summaries, which roll up to weekly, then monthly, quarterly, annual, and finally into permanent legacy knowledge.
|
### L7_REALTIME (시장+종목 스코프)
|
||||||
|
|
||||||
## The 7 Layers
|
- 주요 키 패턴:
|
||||||
|
- `volatility_{market}_{stock_code}`
|
||||||
|
- `price_{market}_{stock_code}`
|
||||||
|
- `rsi_{market}_{stock_code}`
|
||||||
|
- `volume_ratio_{market}_{stock_code}`
|
||||||
|
|
||||||
### L7: Real-time
|
`trading_cycle()`에서 실시간으로 기록합니다.
|
||||||
**Retention**: 7 days
|
|
||||||
**Timeframe format**: `YYYY-MM-DD` (same-day)
|
|
||||||
**Content**: Current positions, live quotes, orderbook snapshots, tick-by-tick volatility
|
|
||||||
|
|
||||||
**Use cases**:
|
### L6_DAILY (시장 스코프)
|
||||||
- Immediate execution decisions
|
|
||||||
- Stop-loss triggers
|
|
||||||
- Real-time P&L tracking
|
|
||||||
|
|
||||||
**Example keys**:
|
EOD 집계 결과를 시장별 키로 저장합니다.
|
||||||
- `current_position_{stock_code}`: Current holdings
|
|
||||||
- `live_price_{stock_code}`: Latest quote
|
|
||||||
- `volatility_5m_{stock_code}`: 5-minute rolling volatility
|
|
||||||
|
|
||||||
### L6: Daily
|
- `trade_count_KR`, `buys_KR`, `sells_KR`, `holds_KR`
|
||||||
**Retention**: 90 days
|
- `avg_confidence_US`, `total_pnl_US`, `win_rate_US`
|
||||||
**Timeframe format**: `YYYY-MM-DD`
|
- scorecard 저장 키: `scorecard_KR`, `scorecard_US`
|
||||||
**Content**: Daily trade logs, end-of-day P&L, market summaries, decision accuracy
|
|
||||||
|
|
||||||
**Use cases**:
|
### L5_WEEKLY
|
||||||
- Daily performance review
|
|
||||||
- Identify patterns in recent trading
|
|
||||||
- Backtest strategy adjustments
|
|
||||||
|
|
||||||
**Example keys**:
|
L6 일일 데이터에서 시장별 주간 합계를 생성합니다.
|
||||||
- `total_pnl`: Daily profit/loss
|
|
||||||
- `trade_count`: Number of trades
|
|
||||||
- `win_rate`: Percentage of profitable trades
|
|
||||||
- `avg_confidence`: Average Gemini confidence
|
|
||||||
|
|
||||||
### L5: Weekly
|
- `weekly_pnl_KR`, `weekly_pnl_US`
|
||||||
**Retention**: 1 year
|
- `avg_confidence_KR`, `avg_confidence_US`
|
||||||
**Timeframe format**: `YYYY-Www` (ISO week, e.g., `2026-W06`)
|
|
||||||
**Content**: Weekly stock selection, sector rotation, volatility regime classification
|
|
||||||
|
|
||||||
**Use cases**:
|
### L4_MONTHLY 이상
|
||||||
- Weekly strategy adjustment
|
|
||||||
- Sector momentum tracking
|
|
||||||
- Identify hot/cold markets
|
|
||||||
|
|
||||||
**Example keys**:
|
글로벌 통합 롤업입니다.
|
||||||
- `weekly_pnl`: Week's total P&L
|
|
||||||
- `top_performers`: Best-performing stocks
|
|
||||||
- `sector_focus`: Dominant sectors
|
|
||||||
- `avg_confidence`: Weekly average confidence
|
|
||||||
|
|
||||||
### L4: Monthly
|
- L5 → L4: `monthly_pnl`
|
||||||
**Retention**: 2 years
|
- L4 → L3: `quarterly_pnl`
|
||||||
**Timeframe format**: `YYYY-MM`
|
- L3 → L2: `annual_pnl`
|
||||||
**Content**: Monthly portfolio rebalancing, risk exposure analysis, drawdown recovery
|
- L2 → L1: `total_pnl`, `years_traded`, `avg_annual_pnl`
|
||||||
|
|
||||||
**Use cases**:
|
## Aggregation Flow
|
||||||
- Monthly performance reporting
|
|
||||||
- Risk exposure adjustment
|
|
||||||
- Correlation analysis
|
|
||||||
|
|
||||||
**Example keys**:
|
- EOD: `ContextAggregator.aggregate_daily_from_trades(date, market)`
|
||||||
- `monthly_pnl`: Month's total P&L
|
- 주기 롤업: `ContextScheduler.run_if_due()`
|
||||||
- `sharpe_ratio`: Risk-adjusted return
|
|
||||||
- `max_drawdown`: Largest peak-to-trough decline
|
|
||||||
- `rebalancing_notes`: Manual insights
|
|
||||||
|
|
||||||
### L3: Quarterly
|
`ContextScheduler`는 다음을 처리합니다.
|
||||||
**Retention**: 3 years
|
|
||||||
**Timeframe format**: `YYYY-Qn` (e.g., `2026-Q1`)
|
|
||||||
**Content**: Quarterly strategy pivots, market phase detection (bull/bear/sideways), macro regime changes
|
|
||||||
|
|
||||||
**Use cases**:
|
- weekly/monthly/quarterly/annual/legacy 집계
|
||||||
- Strategic pivots (e.g., growth → value)
|
- 일 1회 `ContextStore.cleanup_expired_contexts()` 실행
|
||||||
- Macro regime classification
|
- 동일 날짜 중복 실행 방지(`_last_run`)
|
||||||
- Long-term pattern recognition
|
|
||||||
|
|
||||||
**Example keys**:
|
|
||||||
- `quarterly_pnl`: Quarter's total P&L
|
|
||||||
- `market_phase`: Bull/Bear/Sideways
|
|
||||||
- `strategy_adjustments`: Major changes made
|
|
||||||
- `lessons_learned`: Key insights
|
|
||||||
|
|
||||||
### L2: Annual
|
|
||||||
**Retention**: 10 years
|
|
||||||
**Timeframe format**: `YYYY`
|
|
||||||
**Content**: Yearly returns, Sharpe ratio, max drawdown, win rate, strategy effectiveness
|
|
||||||
|
|
||||||
**Use cases**:
|
|
||||||
- Annual performance review
|
|
||||||
- Multi-year trend analysis
|
|
||||||
- Strategy benchmarking
|
|
||||||
|
|
||||||
**Example keys**:
|
|
||||||
- `annual_pnl`: Year's total P&L
|
|
||||||
- `sharpe_ratio`: Annual risk-adjusted return
|
|
||||||
- `win_rate`: Yearly win percentage
|
|
||||||
- `best_strategy`: Most successful strategy
|
|
||||||
- `worst_mistake`: Biggest lesson learned
|
|
||||||
|
|
||||||
### L1: Legacy
|
|
||||||
**Retention**: Forever
|
|
||||||
**Timeframe format**: `LEGACY` (single timeframe)
|
|
||||||
**Content**: Cumulative trading history, core principles, generational wisdom
|
|
||||||
|
|
||||||
**Use cases**:
|
|
||||||
- Long-term philosophy
|
|
||||||
- Foundational rules
|
|
||||||
- Lessons that transcend market cycles
|
|
||||||
|
|
||||||
**Example keys**:
|
|
||||||
- `total_pnl`: All-time profit/loss
|
|
||||||
- `years_traded`: Trading longevity
|
|
||||||
- `avg_annual_pnl`: Long-term average return
|
|
||||||
- `core_principles`: Immutable trading rules
|
|
||||||
- `greatest_trades`: Hall of fame
|
|
||||||
- `never_again`: Permanent warnings
|
|
||||||
|
|
||||||
## Usage
|
## Usage
|
||||||
|
|
||||||
### Setting Context
|
|
||||||
|
|
||||||
```python
|
```python
|
||||||
from src.context import ContextLayer, ContextStore
|
from datetime import UTC, datetime
|
||||||
from src.db import init_db
|
|
||||||
|
|
||||||
conn = init_db("data/ouroboros.db")
|
|
||||||
store = ContextStore(conn)
|
|
||||||
|
|
||||||
# Store daily P&L
|
|
||||||
store.set_context(
|
|
||||||
layer=ContextLayer.L6_DAILY,
|
|
||||||
timeframe="2026-02-04",
|
|
||||||
key="total_pnl",
|
|
||||||
value=1234.56
|
|
||||||
)
|
|
||||||
|
|
||||||
# Store weekly insight
|
|
||||||
store.set_context(
|
|
||||||
layer=ContextLayer.L5_WEEKLY,
|
|
||||||
timeframe="2026-W06",
|
|
||||||
key="top_performers",
|
|
||||||
value=["005930", "000660", "035720"] # JSON-serializable
|
|
||||||
)
|
|
||||||
|
|
||||||
# Store legacy wisdom
|
|
||||||
store.set_context(
|
|
||||||
layer=ContextLayer.L1_LEGACY,
|
|
||||||
timeframe="LEGACY",
|
|
||||||
key="core_principles",
|
|
||||||
value=[
|
|
||||||
"Cut losses fast",
|
|
||||||
"Let winners run",
|
|
||||||
"Never average down on losing positions"
|
|
||||||
]
|
|
||||||
)
|
|
||||||
```
|
|
||||||
|
|
||||||
### Retrieving Context
|
|
||||||
|
|
||||||
```python
|
|
||||||
# Get a specific value
|
|
||||||
pnl = store.get_context(ContextLayer.L6_DAILY, "2026-02-04", "total_pnl")
|
|
||||||
# Returns: 1234.56
|
|
||||||
|
|
||||||
# Get all keys for a timeframe
|
|
||||||
daily_summary = store.get_all_contexts(ContextLayer.L6_DAILY, "2026-02-04")
|
|
||||||
# Returns: {"total_pnl": 1234.56, "trade_count": 10, "win_rate": 60.0, ...}
|
|
||||||
|
|
||||||
# Get all data for a layer (any timeframe)
|
|
||||||
all_daily = store.get_all_contexts(ContextLayer.L6_DAILY)
|
|
||||||
# Returns: {"total_pnl": 1234.56, "trade_count": 10, ...} (latest timeframes first)
|
|
||||||
|
|
||||||
# Get the latest timeframe
|
|
||||||
latest = store.get_latest_timeframe(ContextLayer.L6_DAILY)
|
|
||||||
# Returns: "2026-02-04"
|
|
||||||
```
|
|
||||||
|
|
||||||
### Automatic Aggregation
|
|
||||||
|
|
||||||
The `ContextAggregator` rolls up data from lower to higher layers:
|
|
||||||
|
|
||||||
```python
|
|
||||||
from src.context.aggregator import ContextAggregator
|
from src.context.aggregator import ContextAggregator
|
||||||
|
from src.context.scheduler import ContextScheduler
|
||||||
|
|
||||||
aggregator = ContextAggregator(conn)
|
aggregator = ContextAggregator(conn)
|
||||||
|
scheduler = ContextScheduler(aggregator=aggregator, store=context_store)
|
||||||
|
|
||||||
# Aggregate daily metrics from trades
|
# EOD market-scoped daily aggregation
|
||||||
aggregator.aggregate_daily_from_trades("2026-02-04")
|
aggregator.aggregate_daily_from_trades(date="2026-02-16", market="KR")
|
||||||
|
|
||||||
# Roll up weekly from daily
|
# Run scheduled rollups when due
|
||||||
aggregator.aggregate_weekly_from_daily("2026-W06")
|
scheduler.run_if_due(now=datetime.now(UTC))
|
||||||
|
|
||||||
# Roll up all layers at once (bottom-up)
|
|
||||||
aggregator.run_all_aggregations()
|
|
||||||
```
|
```
|
||||||
|
|
||||||
**Aggregation schedule** (recommended):
|
## Retention
|
||||||
- **L7 → L6**: Every midnight (daily rollup)
|
|
||||||
- **L6 → L5**: Every Sunday (weekly rollup)
|
|
||||||
- **L5 → L4**: First day of each month (monthly rollup)
|
|
||||||
- **L4 → L3**: First day of quarter (quarterly rollup)
|
|
||||||
- **L3 → L2**: January 1st (annual rollup)
|
|
||||||
- **L2 → L1**: On demand (major milestones)
|
|
||||||
|
|
||||||
### Context Cleanup
|
`src/context/layer.py` 기준:
|
||||||
|
|
||||||
Expired contexts are automatically deleted based on retention policies:
|
|
||||||
|
|
||||||
```python
|
|
||||||
# Manual cleanup
|
|
||||||
deleted = store.cleanup_expired_contexts()
|
|
||||||
# Returns: {ContextLayer.L7_REALTIME: 42, ContextLayer.L6_DAILY: 15, ...}
|
|
||||||
```
|
|
||||||
|
|
||||||
**Retention policies** (defined in `src/context/layer.py`):
|
|
||||||
- L1: Forever
|
- L1: Forever
|
||||||
- L2: 10 years
|
- L2: 10 years
|
||||||
- L3: 3 years
|
- L3: 3 years
|
||||||
@@ -246,93 +84,8 @@ deleted = store.cleanup_expired_contexts()
|
|||||||
- L6: 90 days
|
- L6: 90 days
|
||||||
- L7: 7 days
|
- L7: 7 days
|
||||||
|
|
||||||
## Integration with Gemini Brain
|
## Current Notes (2026-02-16)
|
||||||
|
|
||||||
The context tree provides hierarchical memory for decision-making:
|
- L7 쓰기와 L6 시장별 집계는 `main.py`에 연결됨
|
||||||
|
- scheduler 기반 cleanup/rollup도 연결됨
|
||||||
```python
|
- cross-market scorecard 조회는 `PreMarketPlanner`에서 사용 중
|
||||||
from src.brain.gemini_client import GeminiClient
|
|
||||||
|
|
||||||
# Build prompt with multi-layer context
|
|
||||||
def build_enhanced_prompt(stock_code: str, store: ContextStore) -> str:
|
|
||||||
# L7: Real-time data
|
|
||||||
current_price = store.get_context(ContextLayer.L7_REALTIME, "2026-02-04", f"live_price_{stock_code}")
|
|
||||||
|
|
||||||
# L6: Recent daily performance
|
|
||||||
yesterday_pnl = store.get_context(ContextLayer.L6_DAILY, "2026-02-03", "total_pnl")
|
|
||||||
|
|
||||||
# L5: Weekly trend
|
|
||||||
weekly_data = store.get_all_contexts(ContextLayer.L5_WEEKLY, "2026-W06")
|
|
||||||
|
|
||||||
# L1: Core principles
|
|
||||||
principles = store.get_context(ContextLayer.L1_LEGACY, "LEGACY", "core_principles")
|
|
||||||
|
|
||||||
return f"""
|
|
||||||
Analyze {stock_code} for trading decision.
|
|
||||||
|
|
||||||
Current price: {current_price}
|
|
||||||
Yesterday's P&L: {yesterday_pnl}
|
|
||||||
This week: {weekly_data}
|
|
||||||
|
|
||||||
Core principles:
|
|
||||||
{chr(10).join(f'- {p}' for p in principles)}
|
|
||||||
|
|
||||||
Decision (BUY/SELL/HOLD):
|
|
||||||
"""
|
|
||||||
```
|
|
||||||
|
|
||||||
## Database Schema
|
|
||||||
|
|
||||||
```sql
|
|
||||||
-- Context storage
|
|
||||||
CREATE TABLE contexts (
|
|
||||||
id INTEGER PRIMARY KEY AUTOINCREMENT,
|
|
||||||
layer TEXT NOT NULL, -- L1_LEGACY, L2_ANNUAL, ..., L7_REALTIME
|
|
||||||
timeframe TEXT NOT NULL, -- "LEGACY", "2026", "2026-Q1", "2026-02", "2026-W06", "2026-02-04"
|
|
||||||
key TEXT NOT NULL, -- "total_pnl", "win_rate", "core_principles", etc.
|
|
||||||
value TEXT NOT NULL, -- JSON-serialized value
|
|
||||||
created_at TEXT NOT NULL, -- ISO 8601 timestamp
|
|
||||||
updated_at TEXT NOT NULL, -- ISO 8601 timestamp
|
|
||||||
UNIQUE(layer, timeframe, key)
|
|
||||||
);
|
|
||||||
|
|
||||||
-- Layer metadata
|
|
||||||
CREATE TABLE context_metadata (
|
|
||||||
layer TEXT PRIMARY KEY,
|
|
||||||
description TEXT NOT NULL,
|
|
||||||
retention_days INTEGER, -- NULL = keep forever
|
|
||||||
aggregation_source TEXT -- Parent layer for rollup
|
|
||||||
);
|
|
||||||
|
|
||||||
-- Indices for fast queries
|
|
||||||
CREATE INDEX idx_contexts_layer ON contexts(layer);
|
|
||||||
CREATE INDEX idx_contexts_timeframe ON contexts(timeframe);
|
|
||||||
CREATE INDEX idx_contexts_updated ON contexts(updated_at);
|
|
||||||
```
|
|
||||||
|
|
||||||
## Best Practices
|
|
||||||
|
|
||||||
1. **Write to leaf layers only** — Never manually write to L1-L5; let aggregation populate them
|
|
||||||
2. **Aggregate regularly** — Schedule aggregation jobs to keep higher layers fresh
|
|
||||||
3. **Query specific timeframes** — Use `get_context(layer, timeframe, key)` for precise retrieval
|
|
||||||
4. **Clean up periodically** — Run `cleanup_expired_contexts()` weekly to free space
|
|
||||||
5. **Preserve L1 forever** — Legacy wisdom should never expire
|
|
||||||
6. **Use JSON-serializable values** — Store dicts, lists, strings, numbers (not custom objects)
|
|
||||||
|
|
||||||
## Testing
|
|
||||||
|
|
||||||
See `tests/test_context.py` for comprehensive test coverage (18 tests, 100% coverage on context modules).
|
|
||||||
|
|
||||||
```bash
|
|
||||||
pytest tests/test_context.py -v
|
|
||||||
```
|
|
||||||
|
|
||||||
## References
|
|
||||||
|
|
||||||
- **Implementation**: `src/context/`
|
|
||||||
- `layer.py`: Layer definitions and metadata
|
|
||||||
- `store.py`: CRUD operations
|
|
||||||
- `aggregator.py`: Bottom-up aggregation logic
|
|
||||||
- **Database**: `src/db.py` (table initialization)
|
|
||||||
- **Tests**: `tests/test_context.py`
|
|
||||||
- **Related**: Pillar 2 (Multi-layered Context Management)
|
|
||||||
|
|||||||
@@ -86,3 +86,48 @@
|
|||||||
- Plan Consistency (필수), Safety & Constraints, Quality, Workflow 4개 카테고리
|
- Plan Consistency (필수), Safety & Constraints, Quality, Workflow 4개 카테고리
|
||||||
|
|
||||||
**이슈/PR:** #114
|
**이슈/PR:** #114
|
||||||
|
|
||||||
|
---
|
||||||
|
|
||||||
|
## 2026-02-16
|
||||||
|
|
||||||
|
### V2 진행상태 재정렬 + 문서 동기화
|
||||||
|
|
||||||
|
**배경:**
|
||||||
|
- V2 이슈 다수가 병렬로 진행되며 구현/문서 간 상태 불일치가 발생
|
||||||
|
- 사용자 요청으로 "현재 코드 기준 사실"에 맞춘 전면 문서 갱신 필요
|
||||||
|
|
||||||
|
**확인된 상태(코드 기준):**
|
||||||
|
- 완료: 18/20
|
||||||
|
- 부분 완료: `1-7`
|
||||||
|
- 미완료: `4-1`
|
||||||
|
|
||||||
|
**핵심 반영 사항:**
|
||||||
|
1. 대시보드 실행 통합(`Issue 4-3`) 반영
|
||||||
|
- `--dashboard` 플래그
|
||||||
|
- `DASHBOARD_ENABLED`, `DASHBOARD_HOST`, `DASHBOARD_PORT`
|
||||||
|
2. 컨텍스트 스케줄러 및 시장 스코프 키 정책 반영
|
||||||
|
3. scorecard/review/evolution 연결 상태 반영
|
||||||
|
4. 미완료 갭 명시
|
||||||
|
- Telegram 확장 명령어(`4-1`) 미구현
|
||||||
|
- `1-7` 잔여 항목(키 정규화/HOLD 손절 모니터링/US 코드 정합성)
|
||||||
|
|
||||||
|
**프로세스 요구사항 강화:**
|
||||||
|
- 모든 문서 작업도 Gitea 이슈 선등록 후 진행
|
||||||
|
- 병렬 작업 후 상태 정합성 점검 결과를 `requirements-log`에 기록
|
||||||
|
|
||||||
|
**이슈/브랜치:**
|
||||||
|
- Issue: #131
|
||||||
|
- Branch(worktree): `feature/issue-131-docs-v2-status-sync`
|
||||||
|
|
||||||
|
### 문서 보강 2차 (리뷰 반영)
|
||||||
|
|
||||||
|
**리뷰 피드백 반영:**
|
||||||
|
- README에 Quick Start(환경설정/설치/검증) 복원
|
||||||
|
- architecture에 RiskManager/에러 처리/설정 레퍼런스 복원
|
||||||
|
- testing 문서에 기존 핵심 테스트 파일 설명 복원
|
||||||
|
- 시장 코드 불일치(`KR,US` vs `US_NASDAQ/US_NYSE`)를 "런타임 영향"으로 격상 명시
|
||||||
|
- `price_change_pct` 누락 영향(조건 dead path)을 명시
|
||||||
|
|
||||||
|
**의도:**
|
||||||
|
- V2 상태 반영과 기존 온보딩/운영 문서 가치를 동시에 유지
|
||||||
|
|||||||
256
docs/testing.md
256
docs/testing.md
@@ -1,213 +1,63 @@
|
|||||||
# Testing Guidelines
|
# Testing Guidelines
|
||||||
|
|
||||||
## Test Structure
|
## Current Test Baseline (2026-02-16)
|
||||||
|
|
||||||
**54 tests** across four files. `asyncio_mode = "auto"` in pyproject.toml — async tests need no special decorator.
|
수집 기준:
|
||||||
|
|
||||||
The `settings` fixture in `conftest.py` provides safe defaults with test credentials and in-memory DB.
|
|
||||||
|
|
||||||
### Test Files
|
|
||||||
|
|
||||||
#### `tests/test_risk.py` (11 tests)
|
|
||||||
- Circuit breaker boundaries
|
|
||||||
- Fat-finger edge cases
|
|
||||||
- P&L calculation edge cases
|
|
||||||
- Order validation logic
|
|
||||||
|
|
||||||
**Example:**
|
|
||||||
```python
|
|
||||||
def test_circuit_breaker_exact_threshold(risk_manager):
|
|
||||||
"""Circuit breaker should trip at exactly -3.0%."""
|
|
||||||
with pytest.raises(CircuitBreakerTripped):
|
|
||||||
risk_manager.validate_order(
|
|
||||||
current_pnl_pct=-3.0,
|
|
||||||
order_amount=1000,
|
|
||||||
total_cash=10000
|
|
||||||
)
|
|
||||||
```
|
|
||||||
|
|
||||||
#### `tests/test_broker.py` (6 tests)
|
|
||||||
- OAuth token lifecycle
|
|
||||||
- Rate limiting enforcement
|
|
||||||
- Hash key generation
|
|
||||||
- Network error handling
|
|
||||||
- SSL context configuration
|
|
||||||
|
|
||||||
**Example:**
|
|
||||||
```python
|
|
||||||
async def test_rate_limiter(broker):
|
|
||||||
"""Rate limiter should delay requests to stay under 10 RPS."""
|
|
||||||
start = time.monotonic()
|
|
||||||
for _ in range(15): # 15 requests
|
|
||||||
await broker._rate_limiter.acquire()
|
|
||||||
elapsed = time.monotonic() - start
|
|
||||||
assert elapsed >= 1.0 # Should take at least 1 second
|
|
||||||
```
|
|
||||||
|
|
||||||
#### `tests/test_brain.py` (18 tests)
|
|
||||||
- Valid JSON parsing
|
|
||||||
- Markdown-wrapped JSON handling
|
|
||||||
- Malformed JSON fallback
|
|
||||||
- Missing fields handling
|
|
||||||
- Invalid action validation
|
|
||||||
- Confidence threshold enforcement
|
|
||||||
- Empty response handling
|
|
||||||
- Prompt construction for different markets
|
|
||||||
|
|
||||||
**Example:**
|
|
||||||
```python
|
|
||||||
async def test_confidence_below_threshold_forces_hold(brain):
|
|
||||||
"""Decisions below confidence threshold should force HOLD."""
|
|
||||||
decision = brain.parse_response('{"action":"BUY","confidence":70,"rationale":"test"}')
|
|
||||||
assert decision.action == "HOLD"
|
|
||||||
assert decision.confidence == 70
|
|
||||||
```
|
|
||||||
|
|
||||||
#### `tests/test_market_schedule.py` (19 tests)
|
|
||||||
- Market open/close logic
|
|
||||||
- Timezone handling (UTC, Asia/Seoul, America/New_York, etc.)
|
|
||||||
- DST (Daylight Saving Time) transitions
|
|
||||||
- Weekend handling
|
|
||||||
- Lunch break logic
|
|
||||||
- Multiple market filtering
|
|
||||||
- Next market open calculation
|
|
||||||
|
|
||||||
**Example:**
|
|
||||||
```python
|
|
||||||
def test_is_market_open_during_trading_hours():
|
|
||||||
"""Market should be open during regular trading hours."""
|
|
||||||
# KRX: 9:00-15:30 KST, no lunch break
|
|
||||||
market = MARKETS["KR"]
|
|
||||||
trading_time = datetime(2026, 2, 3, 10, 0, tzinfo=ZoneInfo("Asia/Seoul")) # Monday 10:00
|
|
||||||
assert is_market_open(market, trading_time) is True
|
|
||||||
```
|
|
||||||
|
|
||||||
## Coverage Requirements
|
|
||||||
|
|
||||||
**Minimum coverage: 80%**
|
|
||||||
|
|
||||||
Check coverage:
|
|
||||||
```bash
|
|
||||||
pytest -v --cov=src --cov-report=term-missing
|
|
||||||
```
|
|
||||||
|
|
||||||
Expected output:
|
|
||||||
```
|
|
||||||
Name Stmts Miss Cover Missing
|
|
||||||
-----------------------------------------------------------
|
|
||||||
src/brain/gemini_client.py 85 5 94% 165-169
|
|
||||||
src/broker/kis_api.py 120 12 90% ...
|
|
||||||
src/core/risk_manager.py 35 2 94% ...
|
|
||||||
src/db.py 25 1 96% ...
|
|
||||||
src/main.py 150 80 47% (excluded from CI)
|
|
||||||
src/markets/schedule.py 95 3 97% ...
|
|
||||||
-----------------------------------------------------------
|
|
||||||
TOTAL 510 103 80%
|
|
||||||
```
|
|
||||||
|
|
||||||
**Note:** `main.py` has lower coverage as it contains the main loop which is tested via integration/manual testing.
|
|
||||||
|
|
||||||
## Test Configuration
|
|
||||||
|
|
||||||
### `pyproject.toml`
|
|
||||||
```toml
|
|
||||||
[tool.pytest.ini_options]
|
|
||||||
asyncio_mode = "auto"
|
|
||||||
testpaths = ["tests"]
|
|
||||||
python_files = ["test_*.py"]
|
|
||||||
```
|
|
||||||
|
|
||||||
### `tests/conftest.py`
|
|
||||||
```python
|
|
||||||
@pytest.fixture
|
|
||||||
def settings() -> Settings:
|
|
||||||
"""Provide test settings with safe defaults."""
|
|
||||||
return Settings(
|
|
||||||
KIS_APP_KEY="test_key",
|
|
||||||
KIS_APP_SECRET="test_secret",
|
|
||||||
KIS_ACCOUNT_NO="12345678-01",
|
|
||||||
GEMINI_API_KEY="test_gemini_key",
|
|
||||||
MODE="paper",
|
|
||||||
DB_PATH=":memory:", # In-memory SQLite
|
|
||||||
CONFIDENCE_THRESHOLD=80,
|
|
||||||
ENABLED_MARKETS="KR",
|
|
||||||
)
|
|
||||||
```
|
|
||||||
|
|
||||||
## Writing New Tests
|
|
||||||
|
|
||||||
### Naming Convention
|
|
||||||
- Test files: `test_<module>.py`
|
|
||||||
- Test functions: `test_<feature>_<scenario>()`
|
|
||||||
- Use descriptive names that explain what is being tested
|
|
||||||
|
|
||||||
### Good Test Example
|
|
||||||
```python
|
|
||||||
async def test_send_order_with_market_price(broker, settings):
|
|
||||||
"""Market orders should use price=0 and ORD_DVSN='01'."""
|
|
||||||
# Arrange
|
|
||||||
stock_code = "005930"
|
|
||||||
order_type = "BUY"
|
|
||||||
quantity = 10
|
|
||||||
|
|
||||||
# Act
|
|
||||||
with patch.object(broker._session, 'post') as mock_post:
|
|
||||||
mock_post.return_value.__aenter__.return_value.status = 200
|
|
||||||
mock_post.return_value.__aenter__.return_value.json = AsyncMock(
|
|
||||||
return_value={"rt_cd": "0", "msg1": "OK"}
|
|
||||||
)
|
|
||||||
|
|
||||||
await broker.send_order(stock_code, order_type, quantity, price=0)
|
|
||||||
|
|
||||||
# Assert
|
|
||||||
call_args = mock_post.call_args
|
|
||||||
body = call_args.kwargs['json']
|
|
||||||
assert body['ORD_DVSN'] == '01' # Market order
|
|
||||||
assert body['ORD_UNPR'] == '0' # Price 0
|
|
||||||
```
|
|
||||||
|
|
||||||
### Test Checklist
|
|
||||||
- [ ] Test passes in isolation (`pytest tests/test_foo.py::test_bar -v`)
|
|
||||||
- [ ] Test has clear docstring explaining what it tests
|
|
||||||
- [ ] Arrange-Act-Assert structure
|
|
||||||
- [ ] Uses appropriate fixtures from conftest.py
|
|
||||||
- [ ] Mocks external dependencies (API calls, network)
|
|
||||||
- [ ] Tests edge cases and error conditions
|
|
||||||
- [ ] Doesn't rely on test execution order
|
|
||||||
|
|
||||||
## Running Tests
|
|
||||||
|
|
||||||
```bash
|
```bash
|
||||||
# All tests
|
pytest --collect-only -q
|
||||||
pytest -v
|
# 538 tests collected
|
||||||
|
|
||||||
# Specific file
|
|
||||||
pytest tests/test_risk.py -v
|
|
||||||
|
|
||||||
# Specific test
|
|
||||||
pytest tests/test_brain.py::test_parse_valid_json -v
|
|
||||||
|
|
||||||
# With coverage
|
|
||||||
pytest -v --cov=src --cov-report=term-missing
|
|
||||||
|
|
||||||
# Stop on first failure
|
|
||||||
pytest -x
|
|
||||||
|
|
||||||
# Verbose output with print statements
|
|
||||||
pytest -v -s
|
|
||||||
```
|
```
|
||||||
|
|
||||||
## CI/CD Integration
|
V2 핵심 영역 테스트가 포함되어 있습니다.
|
||||||
|
|
||||||
Tests run automatically on:
|
- `tests/test_strategy_models.py`
|
||||||
- Every commit to feature branches
|
- `tests/test_pre_market_planner.py`
|
||||||
- Every PR to main
|
- `tests/test_scenario_engine.py`
|
||||||
- Scheduled daily runs
|
- `tests/test_playbook_store.py`
|
||||||
|
- `tests/test_context_scheduler.py`
|
||||||
|
- `tests/test_daily_review.py`
|
||||||
|
- `tests/test_scorecard.py`
|
||||||
|
- `tests/test_dashboard.py`
|
||||||
|
- `tests/test_main.py`
|
||||||
|
|
||||||
**Blocking conditions:**
|
기존 핵심 영역 테스트도 유지됩니다.
|
||||||
- Test failures → PR blocked
|
|
||||||
- Coverage < 80% → PR blocked (warning only for main.py)
|
|
||||||
|
|
||||||
**Non-blocking:**
|
- `tests/test_risk.py`: circuit breaker/fat-finger 안전장치 검증
|
||||||
- `mypy --strict` errors (type hints encouraged but not enforced)
|
- `tests/test_broker.py`: KIS API 호출/에러 처리/인증 흐름 검증
|
||||||
- `ruff check` warnings (must be acknowledged)
|
- `tests/test_brain.py`: Gemini 응답 파싱/신뢰도 게이트 검증
|
||||||
|
- `tests/test_market_schedule.py`: 시장 오픈/클로즈/타임존 로직 검증
|
||||||
|
|
||||||
|
## Required Checks
|
||||||
|
|
||||||
|
```bash
|
||||||
|
pytest -v --cov=src
|
||||||
|
ruff check src/ tests/
|
||||||
|
mypy src/ --strict
|
||||||
|
```
|
||||||
|
|
||||||
|
## FastAPI Note
|
||||||
|
|
||||||
|
대시보드 테스트(`tests/test_dashboard.py`)는 `fastapi`가 환경에 없으면 skip될 수 있습니다.
|
||||||
|
의도된 동작이며 CI/개발환경에서 의존성 설치 여부를 확인하세요.
|
||||||
|
|
||||||
|
## Targeted Smoke Commands
|
||||||
|
|
||||||
|
```bash
|
||||||
|
# dashboard integration
|
||||||
|
pytest -q tests/test_main.py -k "dashboard"
|
||||||
|
|
||||||
|
# planner/scenario/review paths
|
||||||
|
pytest -q tests/test_pre_market_planner.py tests/test_scenario_engine.py tests/test_daily_review.py
|
||||||
|
|
||||||
|
# context rollup/scheduler
|
||||||
|
pytest -q tests/test_context.py tests/test_context_scheduler.py
|
||||||
|
```
|
||||||
|
|
||||||
|
## Review Checklist (테스트 관점)
|
||||||
|
|
||||||
|
- 플랜 항목별 테스트 존재 여부 확인
|
||||||
|
- 시장 스코프 키(`*_KR`, `*_US`) 검증 확인
|
||||||
|
- EOD 흐름(`aggregate_daily_from_trades`, `scorecard_{market}` 저장) 검증
|
||||||
|
- decision outcome 연결(`decision_id`) 검증
|
||||||
|
- 대시보드 API market filter 검증
|
||||||
|
|||||||
@@ -8,8 +8,9 @@
|
|||||||
2. **Create Feature Branch** — Branch from `main` using format `feature/issue-{N}-{short-description}`
|
2. **Create Feature Branch** — Branch from `main` using format `feature/issue-{N}-{short-description}`
|
||||||
- After creating the branch, run `git pull origin main` and rebase to ensure the branch is up to date
|
- After creating the branch, run `git pull origin main` and rebase to ensure the branch is up to date
|
||||||
3. **Implement Changes** — Write code, tests, and documentation on the feature branch
|
3. **Implement Changes** — Write code, tests, and documentation on the feature branch
|
||||||
4. **Create Pull Request** — Submit PR to `main` branch referencing the issue number
|
4. **Sync Status Docs** — Before PR, update `README.md` and relevant `docs/*.md` so implementation status/gaps are explicit
|
||||||
5. **Review & Merge** — After approval, merge via PR (squash or merge commit)
|
5. **Create Pull Request** — Submit PR to `main` branch referencing the issue number
|
||||||
|
6. **Review & Merge** — After approval, merge via PR (squash or merge commit)
|
||||||
|
|
||||||
**Never commit directly to `main`.** This policy applies to all changes, no exceptions.
|
**Never commit directly to `main`.** This policy applies to all changes, no exceptions.
|
||||||
|
|
||||||
|
|||||||
@@ -9,6 +9,8 @@ dependencies = [
|
|||||||
"pydantic-settings>=2.1,<3",
|
"pydantic-settings>=2.1,<3",
|
||||||
"google-genai>=1.0,<2",
|
"google-genai>=1.0,<2",
|
||||||
"scipy>=1.11,<2",
|
"scipy>=1.11,<2",
|
||||||
|
"fastapi>=0.110,<1",
|
||||||
|
"uvicorn>=0.29,<1",
|
||||||
]
|
]
|
||||||
|
|
||||||
[project.optional-dependencies]
|
[project.optional-dependencies]
|
||||||
|
|||||||
@@ -83,6 +83,11 @@ class Settings(BaseSettings):
|
|||||||
TELEGRAM_COMMANDS_ENABLED: bool = True
|
TELEGRAM_COMMANDS_ENABLED: bool = True
|
||||||
TELEGRAM_POLLING_INTERVAL: float = 1.0 # seconds
|
TELEGRAM_POLLING_INTERVAL: float = 1.0 # seconds
|
||||||
|
|
||||||
|
# Dashboard (optional)
|
||||||
|
DASHBOARD_ENABLED: bool = False
|
||||||
|
DASHBOARD_HOST: str = "127.0.0.1"
|
||||||
|
DASHBOARD_PORT: int = Field(default=8080, ge=1, le=65535)
|
||||||
|
|
||||||
model_config = {"env_file": ".env", "env_file_encoding": "utf-8"}
|
model_config = {"env_file": ".env", "env_file_encoding": "utf-8"}
|
||||||
|
|
||||||
@property
|
@property
|
||||||
|
|||||||
5
src/dashboard/__init__.py
Normal file
5
src/dashboard/__init__.py
Normal file
@@ -0,0 +1,5 @@
|
|||||||
|
"""FastAPI dashboard package for observability APIs."""
|
||||||
|
|
||||||
|
from src.dashboard.app import create_dashboard_app
|
||||||
|
|
||||||
|
__all__ = ["create_dashboard_app"]
|
||||||
349
src/dashboard/app.py
Normal file
349
src/dashboard/app.py
Normal file
@@ -0,0 +1,349 @@
|
|||||||
|
"""FastAPI application for observability dashboard endpoints."""
|
||||||
|
|
||||||
|
from __future__ import annotations
|
||||||
|
|
||||||
|
import json
|
||||||
|
import sqlite3
|
||||||
|
from datetime import UTC, datetime
|
||||||
|
from pathlib import Path
|
||||||
|
from typing import Any
|
||||||
|
|
||||||
|
from fastapi import FastAPI, HTTPException, Query
|
||||||
|
from fastapi.responses import FileResponse
|
||||||
|
|
||||||
|
|
||||||
|
def create_dashboard_app(db_path: str) -> FastAPI:
|
||||||
|
"""Create dashboard FastAPI app bound to a SQLite database path."""
|
||||||
|
app = FastAPI(title="The Ouroboros Dashboard", version="1.0.0")
|
||||||
|
app.state.db_path = db_path
|
||||||
|
|
||||||
|
@app.get("/")
|
||||||
|
def index() -> FileResponse:
|
||||||
|
index_path = Path(__file__).parent / "static" / "index.html"
|
||||||
|
return FileResponse(index_path)
|
||||||
|
|
||||||
|
@app.get("/api/status")
|
||||||
|
def get_status() -> dict[str, Any]:
|
||||||
|
today = datetime.now(UTC).date().isoformat()
|
||||||
|
with _connect(db_path) as conn:
|
||||||
|
markets = ["KR", "US"]
|
||||||
|
market_status: dict[str, Any] = {}
|
||||||
|
total_trades = 0
|
||||||
|
total_pnl = 0.0
|
||||||
|
total_decisions = 0
|
||||||
|
for market in markets:
|
||||||
|
trade_row = conn.execute(
|
||||||
|
"""
|
||||||
|
SELECT COUNT(*) AS c, COALESCE(SUM(pnl), 0.0) AS p
|
||||||
|
FROM trades
|
||||||
|
WHERE DATE(timestamp) = ? AND market = ?
|
||||||
|
""",
|
||||||
|
(today, market),
|
||||||
|
).fetchone()
|
||||||
|
decision_row = conn.execute(
|
||||||
|
"""
|
||||||
|
SELECT COUNT(*) AS c
|
||||||
|
FROM decision_logs
|
||||||
|
WHERE DATE(timestamp) = ? AND market = ?
|
||||||
|
""",
|
||||||
|
(today, market),
|
||||||
|
).fetchone()
|
||||||
|
playbook_row = conn.execute(
|
||||||
|
"""
|
||||||
|
SELECT status
|
||||||
|
FROM playbooks
|
||||||
|
WHERE date = ? AND market = ?
|
||||||
|
LIMIT 1
|
||||||
|
""",
|
||||||
|
(today, market),
|
||||||
|
).fetchone()
|
||||||
|
market_status[market] = {
|
||||||
|
"trade_count": int(trade_row["c"] if trade_row else 0),
|
||||||
|
"total_pnl": float(trade_row["p"] if trade_row else 0.0),
|
||||||
|
"decision_count": int(decision_row["c"] if decision_row else 0),
|
||||||
|
"playbook_status": playbook_row["status"] if playbook_row else None,
|
||||||
|
}
|
||||||
|
total_trades += market_status[market]["trade_count"]
|
||||||
|
total_pnl += market_status[market]["total_pnl"]
|
||||||
|
total_decisions += market_status[market]["decision_count"]
|
||||||
|
|
||||||
|
return {
|
||||||
|
"date": today,
|
||||||
|
"markets": market_status,
|
||||||
|
"totals": {
|
||||||
|
"trade_count": total_trades,
|
||||||
|
"total_pnl": round(total_pnl, 2),
|
||||||
|
"decision_count": total_decisions,
|
||||||
|
},
|
||||||
|
}
|
||||||
|
|
||||||
|
@app.get("/api/playbook/{date_str}")
|
||||||
|
def get_playbook(date_str: str, market: str = Query("KR")) -> dict[str, Any]:
|
||||||
|
with _connect(db_path) as conn:
|
||||||
|
row = conn.execute(
|
||||||
|
"""
|
||||||
|
SELECT date, market, status, playbook_json, generated_at,
|
||||||
|
token_count, scenario_count, match_count
|
||||||
|
FROM playbooks
|
||||||
|
WHERE date = ? AND market = ?
|
||||||
|
""",
|
||||||
|
(date_str, market),
|
||||||
|
).fetchone()
|
||||||
|
if row is None:
|
||||||
|
raise HTTPException(status_code=404, detail="playbook not found")
|
||||||
|
return {
|
||||||
|
"date": row["date"],
|
||||||
|
"market": row["market"],
|
||||||
|
"status": row["status"],
|
||||||
|
"playbook": json.loads(row["playbook_json"]),
|
||||||
|
"generated_at": row["generated_at"],
|
||||||
|
"token_count": row["token_count"],
|
||||||
|
"scenario_count": row["scenario_count"],
|
||||||
|
"match_count": row["match_count"],
|
||||||
|
}
|
||||||
|
|
||||||
|
@app.get("/api/scorecard/{date_str}")
|
||||||
|
def get_scorecard(date_str: str, market: str = Query("KR")) -> dict[str, Any]:
|
||||||
|
key = f"scorecard_{market}"
|
||||||
|
with _connect(db_path) as conn:
|
||||||
|
row = conn.execute(
|
||||||
|
"""
|
||||||
|
SELECT value
|
||||||
|
FROM contexts
|
||||||
|
WHERE layer = 'L6_DAILY' AND timeframe = ? AND key = ?
|
||||||
|
""",
|
||||||
|
(date_str, key),
|
||||||
|
).fetchone()
|
||||||
|
if row is None:
|
||||||
|
raise HTTPException(status_code=404, detail="scorecard not found")
|
||||||
|
return {"date": date_str, "market": market, "scorecard": json.loads(row["value"])}
|
||||||
|
|
||||||
|
@app.get("/api/performance")
|
||||||
|
def get_performance(market: str = Query("all")) -> dict[str, Any]:
|
||||||
|
with _connect(db_path) as conn:
|
||||||
|
if market == "all":
|
||||||
|
by_market_rows = conn.execute(
|
||||||
|
"""
|
||||||
|
SELECT market,
|
||||||
|
COUNT(*) AS total_trades,
|
||||||
|
SUM(CASE WHEN pnl > 0 THEN 1 ELSE 0 END) AS wins,
|
||||||
|
SUM(CASE WHEN pnl < 0 THEN 1 ELSE 0 END) AS losses,
|
||||||
|
COALESCE(SUM(pnl), 0.0) AS total_pnl,
|
||||||
|
COALESCE(AVG(confidence), 0.0) AS avg_confidence
|
||||||
|
FROM trades
|
||||||
|
GROUP BY market
|
||||||
|
ORDER BY market
|
||||||
|
"""
|
||||||
|
).fetchall()
|
||||||
|
combined = _performance_from_rows(by_market_rows)
|
||||||
|
return {
|
||||||
|
"market": "all",
|
||||||
|
"combined": combined,
|
||||||
|
"by_market": [
|
||||||
|
_row_to_performance(row)
|
||||||
|
for row in by_market_rows
|
||||||
|
],
|
||||||
|
}
|
||||||
|
|
||||||
|
row = conn.execute(
|
||||||
|
"""
|
||||||
|
SELECT market,
|
||||||
|
COUNT(*) AS total_trades,
|
||||||
|
SUM(CASE WHEN pnl > 0 THEN 1 ELSE 0 END) AS wins,
|
||||||
|
SUM(CASE WHEN pnl < 0 THEN 1 ELSE 0 END) AS losses,
|
||||||
|
COALESCE(SUM(pnl), 0.0) AS total_pnl,
|
||||||
|
COALESCE(AVG(confidence), 0.0) AS avg_confidence
|
||||||
|
FROM trades
|
||||||
|
WHERE market = ?
|
||||||
|
GROUP BY market
|
||||||
|
""",
|
||||||
|
(market,),
|
||||||
|
).fetchone()
|
||||||
|
if row is None:
|
||||||
|
return {"market": market, "metrics": _empty_performance(market)}
|
||||||
|
return {"market": market, "metrics": _row_to_performance(row)}
|
||||||
|
|
||||||
|
@app.get("/api/context/{layer}")
|
||||||
|
def get_context_layer(
|
||||||
|
layer: str,
|
||||||
|
timeframe: str | None = Query(default=None),
|
||||||
|
limit: int = Query(default=100, ge=1, le=1000),
|
||||||
|
) -> dict[str, Any]:
|
||||||
|
with _connect(db_path) as conn:
|
||||||
|
if timeframe is None:
|
||||||
|
rows = conn.execute(
|
||||||
|
"""
|
||||||
|
SELECT timeframe, key, value, updated_at
|
||||||
|
FROM contexts
|
||||||
|
WHERE layer = ?
|
||||||
|
ORDER BY updated_at DESC
|
||||||
|
LIMIT ?
|
||||||
|
""",
|
||||||
|
(layer, limit),
|
||||||
|
).fetchall()
|
||||||
|
else:
|
||||||
|
rows = conn.execute(
|
||||||
|
"""
|
||||||
|
SELECT timeframe, key, value, updated_at
|
||||||
|
FROM contexts
|
||||||
|
WHERE layer = ? AND timeframe = ?
|
||||||
|
ORDER BY key
|
||||||
|
LIMIT ?
|
||||||
|
""",
|
||||||
|
(layer, timeframe, limit),
|
||||||
|
).fetchall()
|
||||||
|
|
||||||
|
entries = [
|
||||||
|
{
|
||||||
|
"timeframe": row["timeframe"],
|
||||||
|
"key": row["key"],
|
||||||
|
"value": json.loads(row["value"]),
|
||||||
|
"updated_at": row["updated_at"],
|
||||||
|
}
|
||||||
|
for row in rows
|
||||||
|
]
|
||||||
|
return {
|
||||||
|
"layer": layer,
|
||||||
|
"timeframe": timeframe,
|
||||||
|
"count": len(entries),
|
||||||
|
"entries": entries,
|
||||||
|
}
|
||||||
|
|
||||||
|
@app.get("/api/decisions")
|
||||||
|
def get_decisions(
|
||||||
|
market: str = Query("KR"),
|
||||||
|
limit: int = Query(default=50, ge=1, le=500),
|
||||||
|
) -> dict[str, Any]:
|
||||||
|
with _connect(db_path) as conn:
|
||||||
|
rows = conn.execute(
|
||||||
|
"""
|
||||||
|
SELECT decision_id, timestamp, stock_code, market, exchange_code,
|
||||||
|
action, confidence, rationale, context_snapshot, input_data,
|
||||||
|
outcome_pnl, outcome_accuracy
|
||||||
|
FROM decision_logs
|
||||||
|
WHERE market = ?
|
||||||
|
ORDER BY timestamp DESC
|
||||||
|
LIMIT ?
|
||||||
|
""",
|
||||||
|
(market, limit),
|
||||||
|
).fetchall()
|
||||||
|
decisions = []
|
||||||
|
for row in rows:
|
||||||
|
decisions.append(
|
||||||
|
{
|
||||||
|
"decision_id": row["decision_id"],
|
||||||
|
"timestamp": row["timestamp"],
|
||||||
|
"stock_code": row["stock_code"],
|
||||||
|
"market": row["market"],
|
||||||
|
"exchange_code": row["exchange_code"],
|
||||||
|
"action": row["action"],
|
||||||
|
"confidence": row["confidence"],
|
||||||
|
"rationale": row["rationale"],
|
||||||
|
"context_snapshot": json.loads(row["context_snapshot"]),
|
||||||
|
"input_data": json.loads(row["input_data"]),
|
||||||
|
"outcome_pnl": row["outcome_pnl"],
|
||||||
|
"outcome_accuracy": row["outcome_accuracy"],
|
||||||
|
}
|
||||||
|
)
|
||||||
|
return {"market": market, "count": len(decisions), "decisions": decisions}
|
||||||
|
|
||||||
|
@app.get("/api/scenarios/active")
|
||||||
|
def get_active_scenarios(
|
||||||
|
market: str = Query("US"),
|
||||||
|
date_str: str | None = Query(default=None),
|
||||||
|
limit: int = Query(default=50, ge=1, le=500),
|
||||||
|
) -> dict[str, Any]:
|
||||||
|
if date_str is None:
|
||||||
|
date_str = datetime.now(UTC).date().isoformat()
|
||||||
|
|
||||||
|
with _connect(db_path) as conn:
|
||||||
|
rows = conn.execute(
|
||||||
|
"""
|
||||||
|
SELECT timestamp, stock_code, action, confidence, rationale, context_snapshot
|
||||||
|
FROM decision_logs
|
||||||
|
WHERE market = ? AND DATE(timestamp) = ?
|
||||||
|
ORDER BY timestamp DESC
|
||||||
|
LIMIT ?
|
||||||
|
""",
|
||||||
|
(market, date_str, limit),
|
||||||
|
).fetchall()
|
||||||
|
matches: list[dict[str, Any]] = []
|
||||||
|
for row in rows:
|
||||||
|
snapshot = json.loads(row["context_snapshot"])
|
||||||
|
scenario_match = snapshot.get("scenario_match", {})
|
||||||
|
if not isinstance(scenario_match, dict) or not scenario_match:
|
||||||
|
continue
|
||||||
|
matches.append(
|
||||||
|
{
|
||||||
|
"timestamp": row["timestamp"],
|
||||||
|
"stock_code": row["stock_code"],
|
||||||
|
"action": row["action"],
|
||||||
|
"confidence": row["confidence"],
|
||||||
|
"rationale": row["rationale"],
|
||||||
|
"scenario_match": scenario_match,
|
||||||
|
}
|
||||||
|
)
|
||||||
|
return {"market": market, "date": date_str, "count": len(matches), "matches": matches}
|
||||||
|
|
||||||
|
return app
|
||||||
|
|
||||||
|
|
||||||
|
def _connect(db_path: str) -> sqlite3.Connection:
|
||||||
|
conn = sqlite3.connect(db_path)
|
||||||
|
conn.row_factory = sqlite3.Row
|
||||||
|
return conn
|
||||||
|
|
||||||
|
|
||||||
|
def _row_to_performance(row: sqlite3.Row) -> dict[str, Any]:
|
||||||
|
wins = int(row["wins"] or 0)
|
||||||
|
losses = int(row["losses"] or 0)
|
||||||
|
total = int(row["total_trades"] or 0)
|
||||||
|
win_rate = round((wins / (wins + losses) * 100), 2) if (wins + losses) > 0 else 0.0
|
||||||
|
return {
|
||||||
|
"market": row["market"],
|
||||||
|
"total_trades": total,
|
||||||
|
"wins": wins,
|
||||||
|
"losses": losses,
|
||||||
|
"win_rate": win_rate,
|
||||||
|
"total_pnl": round(float(row["total_pnl"] or 0.0), 2),
|
||||||
|
"avg_confidence": round(float(row["avg_confidence"] or 0.0), 2),
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
def _performance_from_rows(rows: list[sqlite3.Row]) -> dict[str, Any]:
|
||||||
|
total_trades = 0
|
||||||
|
wins = 0
|
||||||
|
losses = 0
|
||||||
|
total_pnl = 0.0
|
||||||
|
confidence_weighted = 0.0
|
||||||
|
for row in rows:
|
||||||
|
market_total = int(row["total_trades"] or 0)
|
||||||
|
market_conf = float(row["avg_confidence"] or 0.0)
|
||||||
|
total_trades += market_total
|
||||||
|
wins += int(row["wins"] or 0)
|
||||||
|
losses += int(row["losses"] or 0)
|
||||||
|
total_pnl += float(row["total_pnl"] or 0.0)
|
||||||
|
confidence_weighted += market_total * market_conf
|
||||||
|
win_rate = round((wins / (wins + losses) * 100), 2) if (wins + losses) > 0 else 0.0
|
||||||
|
avg_confidence = round(confidence_weighted / total_trades, 2) if total_trades > 0 else 0.0
|
||||||
|
return {
|
||||||
|
"market": "all",
|
||||||
|
"total_trades": total_trades,
|
||||||
|
"wins": wins,
|
||||||
|
"losses": losses,
|
||||||
|
"win_rate": win_rate,
|
||||||
|
"total_pnl": round(total_pnl, 2),
|
||||||
|
"avg_confidence": avg_confidence,
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
def _empty_performance(market: str) -> dict[str, Any]:
|
||||||
|
return {
|
||||||
|
"market": market,
|
||||||
|
"total_trades": 0,
|
||||||
|
"wins": 0,
|
||||||
|
"losses": 0,
|
||||||
|
"win_rate": 0.0,
|
||||||
|
"total_pnl": 0.0,
|
||||||
|
"avg_confidence": 0.0,
|
||||||
|
}
|
||||||
61
src/dashboard/static/index.html
Normal file
61
src/dashboard/static/index.html
Normal file
@@ -0,0 +1,61 @@
|
|||||||
|
<!doctype html>
|
||||||
|
<html lang="en">
|
||||||
|
<head>
|
||||||
|
<meta charset="UTF-8" />
|
||||||
|
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
|
||||||
|
<title>The Ouroboros Dashboard</title>
|
||||||
|
<style>
|
||||||
|
:root {
|
||||||
|
--bg: #0b1724;
|
||||||
|
--panel: #12263a;
|
||||||
|
--fg: #e6eef7;
|
||||||
|
--muted: #9fb3c8;
|
||||||
|
--accent: #3cb371;
|
||||||
|
}
|
||||||
|
body {
|
||||||
|
margin: 0;
|
||||||
|
font-family: ui-monospace, SFMono-Regular, Menlo, monospace;
|
||||||
|
background: radial-gradient(circle at top left, #173b58, var(--bg));
|
||||||
|
color: var(--fg);
|
||||||
|
}
|
||||||
|
.wrap {
|
||||||
|
max-width: 900px;
|
||||||
|
margin: 48px auto;
|
||||||
|
padding: 0 16px;
|
||||||
|
}
|
||||||
|
.card {
|
||||||
|
background: color-mix(in oklab, var(--panel), black 12%);
|
||||||
|
border: 1px solid #28455f;
|
||||||
|
border-radius: 12px;
|
||||||
|
padding: 20px;
|
||||||
|
}
|
||||||
|
h1 {
|
||||||
|
margin-top: 0;
|
||||||
|
}
|
||||||
|
code {
|
||||||
|
color: var(--accent);
|
||||||
|
}
|
||||||
|
li {
|
||||||
|
margin: 6px 0;
|
||||||
|
color: var(--muted);
|
||||||
|
}
|
||||||
|
</style>
|
||||||
|
</head>
|
||||||
|
<body>
|
||||||
|
<div class="wrap">
|
||||||
|
<div class="card">
|
||||||
|
<h1>The Ouroboros Dashboard API</h1>
|
||||||
|
<p>Use the following endpoints:</p>
|
||||||
|
<ul>
|
||||||
|
<li><code>/api/status</code></li>
|
||||||
|
<li><code>/api/playbook/{date}?market=KR</code></li>
|
||||||
|
<li><code>/api/scorecard/{date}?market=KR</code></li>
|
||||||
|
<li><code>/api/performance?market=all</code></li>
|
||||||
|
<li><code>/api/context/{layer}</code></li>
|
||||||
|
<li><code>/api/decisions?market=KR</code></li>
|
||||||
|
<li><code>/api/scenarios/active?market=US</code></li>
|
||||||
|
</ul>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
</body>
|
||||||
|
</html>
|
||||||
35
src/db.py
35
src/db.py
@@ -6,6 +6,7 @@ import json
|
|||||||
import sqlite3
|
import sqlite3
|
||||||
from datetime import UTC, datetime
|
from datetime import UTC, datetime
|
||||||
from pathlib import Path
|
from pathlib import Path
|
||||||
|
from typing import Any
|
||||||
|
|
||||||
|
|
||||||
def init_db(db_path: str) -> sqlite3.Connection:
|
def init_db(db_path: str) -> sqlite3.Connection:
|
||||||
@@ -26,7 +27,8 @@ def init_db(db_path: str) -> sqlite3.Connection:
|
|||||||
price REAL,
|
price REAL,
|
||||||
pnl REAL DEFAULT 0.0,
|
pnl REAL DEFAULT 0.0,
|
||||||
market TEXT DEFAULT 'KR',
|
market TEXT DEFAULT 'KR',
|
||||||
exchange_code TEXT DEFAULT 'KRX'
|
exchange_code TEXT DEFAULT 'KRX',
|
||||||
|
decision_id TEXT
|
||||||
)
|
)
|
||||||
"""
|
"""
|
||||||
)
|
)
|
||||||
@@ -41,6 +43,8 @@ def init_db(db_path: str) -> sqlite3.Connection:
|
|||||||
conn.execute("ALTER TABLE trades ADD COLUMN exchange_code TEXT DEFAULT 'KRX'")
|
conn.execute("ALTER TABLE trades ADD COLUMN exchange_code TEXT DEFAULT 'KRX'")
|
||||||
if "selection_context" not in columns:
|
if "selection_context" not in columns:
|
||||||
conn.execute("ALTER TABLE trades ADD COLUMN selection_context TEXT")
|
conn.execute("ALTER TABLE trades ADD COLUMN selection_context TEXT")
|
||||||
|
if "decision_id" not in columns:
|
||||||
|
conn.execute("ALTER TABLE trades ADD COLUMN decision_id TEXT")
|
||||||
|
|
||||||
# Context tree tables for multi-layered memory management
|
# Context tree tables for multi-layered memory management
|
||||||
conn.execute(
|
conn.execute(
|
||||||
@@ -143,6 +147,7 @@ def log_trade(
|
|||||||
market: str = "KR",
|
market: str = "KR",
|
||||||
exchange_code: str = "KRX",
|
exchange_code: str = "KRX",
|
||||||
selection_context: dict[str, any] | None = None,
|
selection_context: dict[str, any] | None = None,
|
||||||
|
decision_id: str | None = None,
|
||||||
) -> None:
|
) -> None:
|
||||||
"""Insert a trade record into the database.
|
"""Insert a trade record into the database.
|
||||||
|
|
||||||
@@ -166,9 +171,9 @@ def log_trade(
|
|||||||
"""
|
"""
|
||||||
INSERT INTO trades (
|
INSERT INTO trades (
|
||||||
timestamp, stock_code, action, confidence, rationale,
|
timestamp, stock_code, action, confidence, rationale,
|
||||||
quantity, price, pnl, market, exchange_code, selection_context
|
quantity, price, pnl, market, exchange_code, selection_context, decision_id
|
||||||
)
|
)
|
||||||
VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)
|
VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)
|
||||||
""",
|
""",
|
||||||
(
|
(
|
||||||
datetime.now(UTC).isoformat(),
|
datetime.now(UTC).isoformat(),
|
||||||
@@ -182,6 +187,30 @@ def log_trade(
|
|||||||
market,
|
market,
|
||||||
exchange_code,
|
exchange_code,
|
||||||
context_json,
|
context_json,
|
||||||
|
decision_id,
|
||||||
),
|
),
|
||||||
)
|
)
|
||||||
conn.commit()
|
conn.commit()
|
||||||
|
|
||||||
|
|
||||||
|
def get_latest_buy_trade(
|
||||||
|
conn: sqlite3.Connection, stock_code: str, market: str
|
||||||
|
) -> dict[str, Any] | None:
|
||||||
|
"""Fetch the most recent BUY trade for a stock and market."""
|
||||||
|
cursor = conn.execute(
|
||||||
|
"""
|
||||||
|
SELECT decision_id, price, quantity
|
||||||
|
FROM trades
|
||||||
|
WHERE stock_code = ?
|
||||||
|
AND market = ?
|
||||||
|
AND action = 'BUY'
|
||||||
|
AND decision_id IS NOT NULL
|
||||||
|
ORDER BY timestamp DESC
|
||||||
|
LIMIT 1
|
||||||
|
""",
|
||||||
|
(stock_code, market),
|
||||||
|
)
|
||||||
|
row = cursor.fetchone()
|
||||||
|
if not row:
|
||||||
|
return None
|
||||||
|
return {"decision_id": row[0], "price": row[1], "quantity": row[2]}
|
||||||
|
|||||||
@@ -1,6 +1,7 @@
|
|||||||
"""Evolution engine for self-improving trading strategies."""
|
"""Evolution engine for self-improving trading strategies."""
|
||||||
|
|
||||||
from src.evolution.ab_test import ABTester, ABTestResult, StrategyPerformance
|
from src.evolution.ab_test import ABTester, ABTestResult, StrategyPerformance
|
||||||
|
from src.evolution.daily_review import DailyReviewer
|
||||||
from src.evolution.optimizer import EvolutionOptimizer
|
from src.evolution.optimizer import EvolutionOptimizer
|
||||||
from src.evolution.performance_tracker import (
|
from src.evolution.performance_tracker import (
|
||||||
PerformanceDashboard,
|
PerformanceDashboard,
|
||||||
@@ -18,4 +19,5 @@ __all__ = [
|
|||||||
"PerformanceDashboard",
|
"PerformanceDashboard",
|
||||||
"StrategyMetrics",
|
"StrategyMetrics",
|
||||||
"DailyScorecard",
|
"DailyScorecard",
|
||||||
|
"DailyReviewer",
|
||||||
]
|
]
|
||||||
|
|||||||
196
src/evolution/daily_review.py
Normal file
196
src/evolution/daily_review.py
Normal file
@@ -0,0 +1,196 @@
|
|||||||
|
"""Daily review generator for market-scoped end-of-day scorecards."""
|
||||||
|
|
||||||
|
from __future__ import annotations
|
||||||
|
|
||||||
|
import json
|
||||||
|
import logging
|
||||||
|
import re
|
||||||
|
import sqlite3
|
||||||
|
from dataclasses import asdict
|
||||||
|
|
||||||
|
from src.brain.gemini_client import GeminiClient
|
||||||
|
from src.context.layer import ContextLayer
|
||||||
|
from src.context.store import ContextStore
|
||||||
|
from src.evolution.scorecard import DailyScorecard
|
||||||
|
|
||||||
|
logger = logging.getLogger(__name__)
|
||||||
|
|
||||||
|
|
||||||
|
class DailyReviewer:
|
||||||
|
"""Builds daily scorecards and optional AI-generated lessons."""
|
||||||
|
|
||||||
|
def __init__(
|
||||||
|
self,
|
||||||
|
conn: sqlite3.Connection,
|
||||||
|
context_store: ContextStore,
|
||||||
|
gemini_client: GeminiClient | None = None,
|
||||||
|
) -> None:
|
||||||
|
self._conn = conn
|
||||||
|
self._context_store = context_store
|
||||||
|
self._gemini = gemini_client
|
||||||
|
|
||||||
|
def generate_scorecard(self, date: str, market: str) -> DailyScorecard:
|
||||||
|
"""Generate a market-scoped scorecard from decision logs and trades."""
|
||||||
|
decision_rows = self._conn.execute(
|
||||||
|
"""
|
||||||
|
SELECT action, confidence, context_snapshot
|
||||||
|
FROM decision_logs
|
||||||
|
WHERE DATE(timestamp) = ? AND market = ?
|
||||||
|
""",
|
||||||
|
(date, market),
|
||||||
|
).fetchall()
|
||||||
|
|
||||||
|
total_decisions = len(decision_rows)
|
||||||
|
buys = sum(1 for row in decision_rows if row[0] == "BUY")
|
||||||
|
sells = sum(1 for row in decision_rows if row[0] == "SELL")
|
||||||
|
holds = sum(1 for row in decision_rows if row[0] == "HOLD")
|
||||||
|
avg_confidence = (
|
||||||
|
round(sum(int(row[1]) for row in decision_rows) / total_decisions, 2)
|
||||||
|
if total_decisions > 0
|
||||||
|
else 0.0
|
||||||
|
)
|
||||||
|
|
||||||
|
matched = 0
|
||||||
|
for row in decision_rows:
|
||||||
|
try:
|
||||||
|
snapshot = json.loads(row[2]) if row[2] else {}
|
||||||
|
except json.JSONDecodeError:
|
||||||
|
snapshot = {}
|
||||||
|
scenario_match = snapshot.get("scenario_match", {})
|
||||||
|
if isinstance(scenario_match, dict) and scenario_match:
|
||||||
|
matched += 1
|
||||||
|
scenario_match_rate = (
|
||||||
|
round((matched / total_decisions) * 100, 2)
|
||||||
|
if total_decisions
|
||||||
|
else 0.0
|
||||||
|
)
|
||||||
|
|
||||||
|
trade_stats = self._conn.execute(
|
||||||
|
"""
|
||||||
|
SELECT
|
||||||
|
COALESCE(SUM(pnl), 0.0),
|
||||||
|
SUM(CASE WHEN pnl > 0 THEN 1 ELSE 0 END),
|
||||||
|
SUM(CASE WHEN pnl < 0 THEN 1 ELSE 0 END)
|
||||||
|
FROM trades
|
||||||
|
WHERE DATE(timestamp) = ? AND market = ?
|
||||||
|
""",
|
||||||
|
(date, market),
|
||||||
|
).fetchone()
|
||||||
|
total_pnl = round(float(trade_stats[0] or 0.0), 2) if trade_stats else 0.0
|
||||||
|
wins = int(trade_stats[1] or 0) if trade_stats else 0
|
||||||
|
losses = int(trade_stats[2] or 0) if trade_stats else 0
|
||||||
|
win_rate = round((wins / (wins + losses)) * 100, 2) if (wins + losses) > 0 else 0.0
|
||||||
|
|
||||||
|
top_winners = [
|
||||||
|
row[0]
|
||||||
|
for row in self._conn.execute(
|
||||||
|
"""
|
||||||
|
SELECT stock_code, SUM(pnl) AS stock_pnl
|
||||||
|
FROM trades
|
||||||
|
WHERE DATE(timestamp) = ? AND market = ?
|
||||||
|
GROUP BY stock_code
|
||||||
|
HAVING stock_pnl > 0
|
||||||
|
ORDER BY stock_pnl DESC
|
||||||
|
LIMIT 3
|
||||||
|
""",
|
||||||
|
(date, market),
|
||||||
|
).fetchall()
|
||||||
|
]
|
||||||
|
|
||||||
|
top_losers = [
|
||||||
|
row[0]
|
||||||
|
for row in self._conn.execute(
|
||||||
|
"""
|
||||||
|
SELECT stock_code, SUM(pnl) AS stock_pnl
|
||||||
|
FROM trades
|
||||||
|
WHERE DATE(timestamp) = ? AND market = ?
|
||||||
|
GROUP BY stock_code
|
||||||
|
HAVING stock_pnl < 0
|
||||||
|
ORDER BY stock_pnl ASC
|
||||||
|
LIMIT 3
|
||||||
|
""",
|
||||||
|
(date, market),
|
||||||
|
).fetchall()
|
||||||
|
]
|
||||||
|
|
||||||
|
return DailyScorecard(
|
||||||
|
date=date,
|
||||||
|
market=market,
|
||||||
|
total_decisions=total_decisions,
|
||||||
|
buys=buys,
|
||||||
|
sells=sells,
|
||||||
|
holds=holds,
|
||||||
|
total_pnl=total_pnl,
|
||||||
|
win_rate=win_rate,
|
||||||
|
avg_confidence=avg_confidence,
|
||||||
|
scenario_match_rate=scenario_match_rate,
|
||||||
|
top_winners=top_winners,
|
||||||
|
top_losers=top_losers,
|
||||||
|
lessons=[],
|
||||||
|
cross_market_note="",
|
||||||
|
)
|
||||||
|
|
||||||
|
async def generate_lessons(self, scorecard: DailyScorecard) -> list[str]:
|
||||||
|
"""Generate concise lessons from scorecard metrics using Gemini."""
|
||||||
|
if self._gemini is None:
|
||||||
|
return []
|
||||||
|
|
||||||
|
prompt = (
|
||||||
|
"You are a trading performance reviewer.\n"
|
||||||
|
"Return ONLY a JSON array of 1-3 short lessons in English.\n"
|
||||||
|
f"Market: {scorecard.market}\n"
|
||||||
|
f"Date: {scorecard.date}\n"
|
||||||
|
f"Total decisions: {scorecard.total_decisions}\n"
|
||||||
|
f"Buys/Sells/Holds: {scorecard.buys}/{scorecard.sells}/{scorecard.holds}\n"
|
||||||
|
f"Total PnL: {scorecard.total_pnl}\n"
|
||||||
|
f"Win rate: {scorecard.win_rate}%\n"
|
||||||
|
f"Average confidence: {scorecard.avg_confidence}\n"
|
||||||
|
f"Scenario match rate: {scorecard.scenario_match_rate}%\n"
|
||||||
|
f"Top winners: {', '.join(scorecard.top_winners) or 'N/A'}\n"
|
||||||
|
f"Top losers: {', '.join(scorecard.top_losers) or 'N/A'}\n"
|
||||||
|
)
|
||||||
|
|
||||||
|
try:
|
||||||
|
decision = await self._gemini.decide(
|
||||||
|
{
|
||||||
|
"stock_code": "REVIEW",
|
||||||
|
"market_name": scorecard.market,
|
||||||
|
"current_price": 0,
|
||||||
|
"prompt_override": prompt,
|
||||||
|
}
|
||||||
|
)
|
||||||
|
return self._parse_lessons(decision.rationale)
|
||||||
|
except Exception as exc:
|
||||||
|
logger.warning("Failed to generate daily lessons: %s", exc)
|
||||||
|
return []
|
||||||
|
|
||||||
|
def store_scorecard_in_context(self, scorecard: DailyScorecard) -> None:
|
||||||
|
"""Store scorecard in L6 using market-scoped key."""
|
||||||
|
self._context_store.set_context(
|
||||||
|
ContextLayer.L6_DAILY,
|
||||||
|
scorecard.date,
|
||||||
|
f"scorecard_{scorecard.market}",
|
||||||
|
asdict(scorecard),
|
||||||
|
)
|
||||||
|
|
||||||
|
def _parse_lessons(self, raw_text: str) -> list[str]:
|
||||||
|
"""Parse lessons from JSON array response or fallback text."""
|
||||||
|
raw_text = raw_text.strip()
|
||||||
|
try:
|
||||||
|
parsed = json.loads(raw_text)
|
||||||
|
if isinstance(parsed, list):
|
||||||
|
return [str(item).strip() for item in parsed if str(item).strip()][:3]
|
||||||
|
except json.JSONDecodeError:
|
||||||
|
pass
|
||||||
|
|
||||||
|
match = re.search(r"\[.*\]", raw_text, re.DOTALL)
|
||||||
|
if match:
|
||||||
|
try:
|
||||||
|
parsed = json.loads(match.group(0))
|
||||||
|
if isinstance(parsed, list):
|
||||||
|
return [str(item).strip() for item in parsed if str(item).strip()][:3]
|
||||||
|
except json.JSONDecodeError:
|
||||||
|
pass
|
||||||
|
|
||||||
|
lines = [line.strip("-* \t") for line in raw_text.splitlines() if line.strip()]
|
||||||
|
return lines[:3]
|
||||||
225
src/main.py
225
src/main.py
@@ -10,6 +10,7 @@ import argparse
|
|||||||
import asyncio
|
import asyncio
|
||||||
import logging
|
import logging
|
||||||
import signal
|
import signal
|
||||||
|
import threading
|
||||||
from datetime import UTC, datetime
|
from datetime import UTC, datetime
|
||||||
from typing import Any
|
from typing import Any
|
||||||
|
|
||||||
@@ -22,11 +23,14 @@ from src.broker.overseas import OverseasBroker
|
|||||||
from src.config import Settings
|
from src.config import Settings
|
||||||
from src.context.aggregator import ContextAggregator
|
from src.context.aggregator import ContextAggregator
|
||||||
from src.context.layer import ContextLayer
|
from src.context.layer import ContextLayer
|
||||||
|
from src.context.scheduler import ContextScheduler
|
||||||
from src.context.store import ContextStore
|
from src.context.store import ContextStore
|
||||||
from src.core.criticality import CriticalityAssessor
|
from src.core.criticality import CriticalityAssessor
|
||||||
from src.core.priority_queue import PriorityTaskQueue
|
from src.core.priority_queue import PriorityTaskQueue
|
||||||
from src.core.risk_manager import CircuitBreakerTripped, FatFingerRejected, RiskManager
|
from src.core.risk_manager import CircuitBreakerTripped, FatFingerRejected, RiskManager
|
||||||
from src.db import init_db, log_trade
|
from src.db import get_latest_buy_trade, init_db, log_trade
|
||||||
|
from src.evolution.daily_review import DailyReviewer
|
||||||
|
from src.evolution.optimizer import EvolutionOptimizer
|
||||||
from src.logging.decision_logger import DecisionLogger
|
from src.logging.decision_logger import DecisionLogger
|
||||||
from src.logging_config import setup_logging
|
from src.logging_config import setup_logging
|
||||||
from src.markets.schedule import MarketInfo, get_next_market_open, get_open_markets
|
from src.markets.schedule import MarketInfo, get_next_market_open, get_open_markets
|
||||||
@@ -279,7 +283,7 @@ async def trading_cycle(
|
|||||||
"pnl_pct": pnl_pct,
|
"pnl_pct": pnl_pct,
|
||||||
}
|
}
|
||||||
|
|
||||||
decision_logger.log_decision(
|
decision_id = decision_logger.log_decision(
|
||||||
stock_code=stock_code,
|
stock_code=stock_code,
|
||||||
market=market.code,
|
market=market.code,
|
||||||
exchange_code=market.exchange_code,
|
exchange_code=market.exchange_code,
|
||||||
@@ -291,6 +295,9 @@ async def trading_cycle(
|
|||||||
)
|
)
|
||||||
|
|
||||||
# 3. Execute if actionable
|
# 3. Execute if actionable
|
||||||
|
quantity = 0
|
||||||
|
trade_price = current_price
|
||||||
|
trade_pnl = 0.0
|
||||||
if decision.action in ("BUY", "SELL"):
|
if decision.action in ("BUY", "SELL"):
|
||||||
# Determine order size (simplified: 1 lot)
|
# Determine order size (simplified: 1 lot)
|
||||||
quantity = 1
|
quantity = 1
|
||||||
@@ -346,6 +353,18 @@ async def trading_cycle(
|
|||||||
except Exception as exc:
|
except Exception as exc:
|
||||||
logger.warning("Telegram notification failed: %s", exc)
|
logger.warning("Telegram notification failed: %s", exc)
|
||||||
|
|
||||||
|
if decision.action == "SELL":
|
||||||
|
buy_trade = get_latest_buy_trade(db_conn, stock_code, market.code)
|
||||||
|
if buy_trade and buy_trade.get("price") is not None:
|
||||||
|
buy_price = float(buy_trade["price"])
|
||||||
|
buy_qty = int(buy_trade.get("quantity") or 1)
|
||||||
|
trade_pnl = (trade_price - buy_price) * buy_qty
|
||||||
|
decision_logger.update_outcome(
|
||||||
|
decision_id=buy_trade["decision_id"],
|
||||||
|
pnl=trade_pnl,
|
||||||
|
accuracy=1 if trade_pnl > 0 else 0,
|
||||||
|
)
|
||||||
|
|
||||||
# 6. Log trade with selection context
|
# 6. Log trade with selection context
|
||||||
selection_context = None
|
selection_context = None
|
||||||
if stock_code in market_candidates:
|
if stock_code in market_candidates:
|
||||||
@@ -363,9 +382,13 @@ async def trading_cycle(
|
|||||||
action=decision.action,
|
action=decision.action,
|
||||||
confidence=decision.confidence,
|
confidence=decision.confidence,
|
||||||
rationale=decision.rationale,
|
rationale=decision.rationale,
|
||||||
|
quantity=quantity,
|
||||||
|
price=trade_price,
|
||||||
|
pnl=trade_pnl,
|
||||||
market=market.code,
|
market=market.code,
|
||||||
exchange_code=market.exchange_code,
|
exchange_code=market.exchange_code,
|
||||||
selection_context=selection_context,
|
selection_context=selection_context,
|
||||||
|
decision_id=decision_id,
|
||||||
)
|
)
|
||||||
|
|
||||||
# 7. Latency monitoring
|
# 7. Latency monitoring
|
||||||
@@ -600,7 +623,7 @@ async def run_daily_session(
|
|||||||
"pnl_pct": pnl_pct,
|
"pnl_pct": pnl_pct,
|
||||||
}
|
}
|
||||||
|
|
||||||
decision_logger.log_decision(
|
decision_id = decision_logger.log_decision(
|
||||||
stock_code=stock_code,
|
stock_code=stock_code,
|
||||||
market=market.code,
|
market=market.code,
|
||||||
exchange_code=market.exchange_code,
|
exchange_code=market.exchange_code,
|
||||||
@@ -612,6 +635,9 @@ async def run_daily_session(
|
|||||||
)
|
)
|
||||||
|
|
||||||
# Execute if actionable
|
# Execute if actionable
|
||||||
|
quantity = 0
|
||||||
|
trade_price = stock_data["current_price"]
|
||||||
|
trade_pnl = 0.0
|
||||||
if decision.action in ("BUY", "SELL"):
|
if decision.action in ("BUY", "SELL"):
|
||||||
quantity = 1
|
quantity = 1
|
||||||
order_amount = stock_data["current_price"] * quantity
|
order_amount = stock_data["current_price"] * quantity
|
||||||
@@ -684,6 +710,18 @@ async def run_daily_session(
|
|||||||
)
|
)
|
||||||
continue
|
continue
|
||||||
|
|
||||||
|
if decision.action == "SELL":
|
||||||
|
buy_trade = get_latest_buy_trade(db_conn, stock_code, market.code)
|
||||||
|
if buy_trade and buy_trade.get("price") is not None:
|
||||||
|
buy_price = float(buy_trade["price"])
|
||||||
|
buy_qty = int(buy_trade.get("quantity") or 1)
|
||||||
|
trade_pnl = (trade_price - buy_price) * buy_qty
|
||||||
|
decision_logger.update_outcome(
|
||||||
|
decision_id=buy_trade["decision_id"],
|
||||||
|
pnl=trade_pnl,
|
||||||
|
accuracy=1 if trade_pnl > 0 else 0,
|
||||||
|
)
|
||||||
|
|
||||||
# Log trade
|
# Log trade
|
||||||
log_trade(
|
log_trade(
|
||||||
conn=db_conn,
|
conn=db_conn,
|
||||||
@@ -691,13 +729,164 @@ async def run_daily_session(
|
|||||||
action=decision.action,
|
action=decision.action,
|
||||||
confidence=decision.confidence,
|
confidence=decision.confidence,
|
||||||
rationale=decision.rationale,
|
rationale=decision.rationale,
|
||||||
|
quantity=quantity,
|
||||||
|
price=trade_price,
|
||||||
|
pnl=trade_pnl,
|
||||||
market=market.code,
|
market=market.code,
|
||||||
exchange_code=market.exchange_code,
|
exchange_code=market.exchange_code,
|
||||||
|
decision_id=decision_id,
|
||||||
)
|
)
|
||||||
|
|
||||||
logger.info("Daily trading session completed")
|
logger.info("Daily trading session completed")
|
||||||
|
|
||||||
|
|
||||||
|
async def _handle_market_close(
|
||||||
|
market_code: str,
|
||||||
|
market_name: str,
|
||||||
|
market_timezone: Any,
|
||||||
|
telegram: TelegramClient,
|
||||||
|
context_aggregator: ContextAggregator,
|
||||||
|
daily_reviewer: DailyReviewer,
|
||||||
|
evolution_optimizer: EvolutionOptimizer | None = None,
|
||||||
|
) -> None:
|
||||||
|
"""Handle market-close tasks: notify, aggregate, review, and store context."""
|
||||||
|
await telegram.notify_market_close(market_name, 0.0)
|
||||||
|
|
||||||
|
market_date = datetime.now(market_timezone).date().isoformat()
|
||||||
|
context_aggregator.aggregate_daily_from_trades(
|
||||||
|
date=market_date,
|
||||||
|
market=market_code,
|
||||||
|
)
|
||||||
|
|
||||||
|
scorecard = daily_reviewer.generate_scorecard(market_date, market_code)
|
||||||
|
daily_reviewer.store_scorecard_in_context(scorecard)
|
||||||
|
|
||||||
|
lessons = await daily_reviewer.generate_lessons(scorecard)
|
||||||
|
if lessons:
|
||||||
|
scorecard.lessons = lessons
|
||||||
|
daily_reviewer.store_scorecard_in_context(scorecard)
|
||||||
|
|
||||||
|
await telegram.send_message(
|
||||||
|
f"<b>Daily Review ({market_code})</b>\n"
|
||||||
|
f"Date: {scorecard.date}\n"
|
||||||
|
f"Decisions: {scorecard.total_decisions}\n"
|
||||||
|
f"P&L: {scorecard.total_pnl:+.2f}\n"
|
||||||
|
f"Win Rate: {scorecard.win_rate:.2f}%\n"
|
||||||
|
f"Lessons: {', '.join(scorecard.lessons) if scorecard.lessons else 'N/A'}"
|
||||||
|
)
|
||||||
|
|
||||||
|
if evolution_optimizer is not None:
|
||||||
|
await _run_evolution_loop(
|
||||||
|
evolution_optimizer=evolution_optimizer,
|
||||||
|
telegram=telegram,
|
||||||
|
market_code=market_code,
|
||||||
|
market_date=market_date,
|
||||||
|
)
|
||||||
|
|
||||||
|
|
||||||
|
def _run_context_scheduler(
|
||||||
|
scheduler: ContextScheduler, now: datetime | None = None,
|
||||||
|
) -> None:
|
||||||
|
"""Run periodic context scheduler tasks and log when anything executes."""
|
||||||
|
result = scheduler.run_if_due(now=now)
|
||||||
|
if any(
|
||||||
|
[
|
||||||
|
result.weekly,
|
||||||
|
result.monthly,
|
||||||
|
result.quarterly,
|
||||||
|
result.annual,
|
||||||
|
result.legacy,
|
||||||
|
result.cleanup,
|
||||||
|
]
|
||||||
|
):
|
||||||
|
logger.info(
|
||||||
|
(
|
||||||
|
"Context scheduler ran (weekly=%s, monthly=%s, quarterly=%s, "
|
||||||
|
"annual=%s, legacy=%s, cleanup=%s)"
|
||||||
|
),
|
||||||
|
result.weekly,
|
||||||
|
result.monthly,
|
||||||
|
result.quarterly,
|
||||||
|
result.annual,
|
||||||
|
result.legacy,
|
||||||
|
result.cleanup,
|
||||||
|
)
|
||||||
|
|
||||||
|
|
||||||
|
async def _run_evolution_loop(
|
||||||
|
evolution_optimizer: EvolutionOptimizer,
|
||||||
|
telegram: TelegramClient,
|
||||||
|
market_code: str,
|
||||||
|
market_date: str,
|
||||||
|
) -> None:
|
||||||
|
"""Run evolution loop once at US close (end of trading day)."""
|
||||||
|
if market_code != "US":
|
||||||
|
return
|
||||||
|
|
||||||
|
try:
|
||||||
|
pr_info = await evolution_optimizer.evolve()
|
||||||
|
except Exception as exc:
|
||||||
|
logger.warning("Evolution loop failed on %s: %s", market_date, exc)
|
||||||
|
return
|
||||||
|
|
||||||
|
if pr_info is None:
|
||||||
|
logger.info("Evolution loop skipped on %s (no actionable failures)", market_date)
|
||||||
|
return
|
||||||
|
|
||||||
|
try:
|
||||||
|
await telegram.send_message(
|
||||||
|
"<b>Evolution Update</b>\n"
|
||||||
|
f"Date: {market_date}\n"
|
||||||
|
f"PR: {pr_info.get('title', 'N/A')}\n"
|
||||||
|
f"Branch: {pr_info.get('branch', 'N/A')}\n"
|
||||||
|
f"Status: {pr_info.get('status', 'N/A')}"
|
||||||
|
)
|
||||||
|
except Exception as exc:
|
||||||
|
logger.warning("Evolution notification failed on %s: %s", market_date, exc)
|
||||||
|
|
||||||
|
|
||||||
|
def _start_dashboard_server(settings: Settings) -> threading.Thread | None:
|
||||||
|
"""Start FastAPI dashboard in a daemon thread when enabled."""
|
||||||
|
if not settings.DASHBOARD_ENABLED:
|
||||||
|
return None
|
||||||
|
|
||||||
|
def _serve() -> None:
|
||||||
|
try:
|
||||||
|
import uvicorn
|
||||||
|
|
||||||
|
from src.dashboard import create_dashboard_app
|
||||||
|
|
||||||
|
app = create_dashboard_app(settings.DB_PATH)
|
||||||
|
uvicorn.run(
|
||||||
|
app,
|
||||||
|
host=settings.DASHBOARD_HOST,
|
||||||
|
port=settings.DASHBOARD_PORT,
|
||||||
|
log_level="info",
|
||||||
|
)
|
||||||
|
except Exception as exc:
|
||||||
|
logger.warning("Dashboard server failed to start: %s", exc)
|
||||||
|
|
||||||
|
thread = threading.Thread(
|
||||||
|
target=_serve,
|
||||||
|
name="dashboard-server",
|
||||||
|
daemon=True,
|
||||||
|
)
|
||||||
|
thread.start()
|
||||||
|
logger.info(
|
||||||
|
"Dashboard server started at http://%s:%d",
|
||||||
|
settings.DASHBOARD_HOST,
|
||||||
|
settings.DASHBOARD_PORT,
|
||||||
|
)
|
||||||
|
return thread
|
||||||
|
|
||||||
|
|
||||||
|
def _apply_dashboard_flag(settings: Settings, dashboard_flag: bool) -> Settings:
|
||||||
|
"""Apply CLI dashboard flag over environment settings."""
|
||||||
|
if dashboard_flag and not settings.DASHBOARD_ENABLED:
|
||||||
|
return settings.model_copy(update={"DASHBOARD_ENABLED": True})
|
||||||
|
return settings
|
||||||
|
|
||||||
|
|
||||||
async def run(settings: Settings) -> None:
|
async def run(settings: Settings) -> None:
|
||||||
"""Main async loop — iterate over open markets on a timer."""
|
"""Main async loop — iterate over open markets on a timer."""
|
||||||
broker = KISBroker(settings)
|
broker = KISBroker(settings)
|
||||||
@@ -708,11 +897,17 @@ async def run(settings: Settings) -> None:
|
|||||||
decision_logger = DecisionLogger(db_conn)
|
decision_logger = DecisionLogger(db_conn)
|
||||||
context_store = ContextStore(db_conn)
|
context_store = ContextStore(db_conn)
|
||||||
context_aggregator = ContextAggregator(db_conn)
|
context_aggregator = ContextAggregator(db_conn)
|
||||||
|
context_scheduler = ContextScheduler(
|
||||||
|
aggregator=context_aggregator,
|
||||||
|
store=context_store,
|
||||||
|
)
|
||||||
|
evolution_optimizer = EvolutionOptimizer(settings)
|
||||||
|
|
||||||
# V2 proactive strategy components
|
# V2 proactive strategy components
|
||||||
context_selector = ContextSelector(context_store)
|
context_selector = ContextSelector(context_store)
|
||||||
scenario_engine = ScenarioEngine()
|
scenario_engine = ScenarioEngine()
|
||||||
playbook_store = PlaybookStore(db_conn)
|
playbook_store = PlaybookStore(db_conn)
|
||||||
|
daily_reviewer = DailyReviewer(db_conn, context_store, gemini_client=brain)
|
||||||
pre_market_planner = PreMarketPlanner(
|
pre_market_planner = PreMarketPlanner(
|
||||||
gemini_client=brain,
|
gemini_client=brain,
|
||||||
context_store=context_store,
|
context_store=context_store,
|
||||||
@@ -890,6 +1085,7 @@ async def run(settings: Settings) -> None:
|
|||||||
low_volatility_threshold=30.0,
|
low_volatility_threshold=30.0,
|
||||||
)
|
)
|
||||||
priority_queue = PriorityTaskQueue(max_size=1000)
|
priority_queue = PriorityTaskQueue(max_size=1000)
|
||||||
|
_start_dashboard_server(settings)
|
||||||
|
|
||||||
# Track last scan time for each market
|
# Track last scan time for each market
|
||||||
last_scan_time: dict[str, float] = {}
|
last_scan_time: dict[str, float] = {}
|
||||||
@@ -940,6 +1136,7 @@ async def run(settings: Settings) -> None:
|
|||||||
while not shutdown.is_set():
|
while not shutdown.is_set():
|
||||||
# Wait for trading to be unpaused
|
# Wait for trading to be unpaused
|
||||||
await pause_trading.wait()
|
await pause_trading.wait()
|
||||||
|
_run_context_scheduler(context_scheduler, now=datetime.now(UTC))
|
||||||
|
|
||||||
try:
|
try:
|
||||||
await run_daily_session(
|
await run_daily_session(
|
||||||
@@ -978,6 +1175,7 @@ async def run(settings: Settings) -> None:
|
|||||||
while not shutdown.is_set():
|
while not shutdown.is_set():
|
||||||
# Wait for trading to be unpaused
|
# Wait for trading to be unpaused
|
||||||
await pause_trading.wait()
|
await pause_trading.wait()
|
||||||
|
_run_context_scheduler(context_scheduler, now=datetime.now(UTC))
|
||||||
|
|
||||||
# Get currently open markets
|
# Get currently open markets
|
||||||
open_markets = get_open_markets(settings.enabled_market_list)
|
open_markets = get_open_markets(settings.enabled_market_list)
|
||||||
@@ -991,13 +1189,14 @@ async def run(settings: Settings) -> None:
|
|||||||
|
|
||||||
market_info = MARKETS.get(market_code)
|
market_info = MARKETS.get(market_code)
|
||||||
if market_info:
|
if market_info:
|
||||||
await telegram.notify_market_close(market_info.name, 0.0)
|
await _handle_market_close(
|
||||||
market_date = datetime.now(
|
market_code=market_code,
|
||||||
market_info.timezone
|
market_name=market_info.name,
|
||||||
).date().isoformat()
|
market_timezone=market_info.timezone,
|
||||||
context_aggregator.aggregate_daily_from_trades(
|
telegram=telegram,
|
||||||
date=market_date,
|
context_aggregator=context_aggregator,
|
||||||
market=market_code,
|
daily_reviewer=daily_reviewer,
|
||||||
|
evolution_optimizer=evolution_optimizer,
|
||||||
)
|
)
|
||||||
except Exception as exc:
|
except Exception as exc:
|
||||||
logger.warning("Market close notification failed: %s", exc)
|
logger.warning("Market close notification failed: %s", exc)
|
||||||
@@ -1240,10 +1439,16 @@ def main() -> None:
|
|||||||
default="paper",
|
default="paper",
|
||||||
help="Trading mode (default: paper)",
|
help="Trading mode (default: paper)",
|
||||||
)
|
)
|
||||||
|
parser.add_argument(
|
||||||
|
"--dashboard",
|
||||||
|
action="store_true",
|
||||||
|
help="Enable FastAPI dashboard server in background thread",
|
||||||
|
)
|
||||||
args = parser.parse_args()
|
args = parser.parse_args()
|
||||||
|
|
||||||
setup_logging()
|
setup_logging()
|
||||||
settings = Settings(MODE=args.mode) # type: ignore[call-arg]
|
settings = Settings(MODE=args.mode) # type: ignore[call-arg]
|
||||||
|
settings = _apply_dashboard_flag(settings, args.dashboard)
|
||||||
asyncio.run(run(settings))
|
asyncio.run(run(settings))
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
@@ -8,7 +8,7 @@ from __future__ import annotations
|
|||||||
|
|
||||||
import json
|
import json
|
||||||
import logging
|
import logging
|
||||||
from datetime import date
|
from datetime import date, timedelta
|
||||||
from typing import Any
|
from typing import Any
|
||||||
|
|
||||||
from src.analysis.smart_scanner import ScanCandidate
|
from src.analysis.smart_scanner import ScanCandidate
|
||||||
@@ -95,10 +95,17 @@ class PreMarketPlanner:
|
|||||||
try:
|
try:
|
||||||
# 1. Gather context
|
# 1. Gather context
|
||||||
context_data = self._gather_context()
|
context_data = self._gather_context()
|
||||||
|
self_market_scorecard = self.build_self_market_scorecard(market, today)
|
||||||
cross_market = self.build_cross_market_context(market, today)
|
cross_market = self.build_cross_market_context(market, today)
|
||||||
|
|
||||||
# 2. Build prompt
|
# 2. Build prompt
|
||||||
prompt = self._build_prompt(market, candidates, context_data, cross_market)
|
prompt = self._build_prompt(
|
||||||
|
market,
|
||||||
|
candidates,
|
||||||
|
context_data,
|
||||||
|
self_market_scorecard,
|
||||||
|
cross_market,
|
||||||
|
)
|
||||||
|
|
||||||
# 3. Call Gemini
|
# 3. Call Gemini
|
||||||
market_data = {
|
market_data = {
|
||||||
@@ -145,7 +152,8 @@ class PreMarketPlanner:
|
|||||||
other_market = "US" if target_market == "KR" else "KR"
|
other_market = "US" if target_market == "KR" else "KR"
|
||||||
if today is None:
|
if today is None:
|
||||||
today = date.today()
|
today = date.today()
|
||||||
timeframe = today.isoformat()
|
timeframe_date = today - timedelta(days=1) if target_market == "KR" else today
|
||||||
|
timeframe = timeframe_date.isoformat()
|
||||||
|
|
||||||
scorecard_key = f"scorecard_{other_market}"
|
scorecard_key = f"scorecard_{other_market}"
|
||||||
scorecard_data = self._context_store.get_context(
|
scorecard_data = self._context_store.get_context(
|
||||||
@@ -175,6 +183,37 @@ class PreMarketPlanner:
|
|||||||
lessons=scorecard_data.get("lessons", []),
|
lessons=scorecard_data.get("lessons", []),
|
||||||
)
|
)
|
||||||
|
|
||||||
|
def build_self_market_scorecard(
|
||||||
|
self, market: str, today: date | None = None,
|
||||||
|
) -> dict[str, Any] | None:
|
||||||
|
"""Build previous-day scorecard for the same market."""
|
||||||
|
if today is None:
|
||||||
|
today = date.today()
|
||||||
|
timeframe = (today - timedelta(days=1)).isoformat()
|
||||||
|
scorecard_key = f"scorecard_{market}"
|
||||||
|
scorecard_data = self._context_store.get_context(
|
||||||
|
ContextLayer.L6_DAILY, timeframe, scorecard_key
|
||||||
|
)
|
||||||
|
|
||||||
|
if scorecard_data is None:
|
||||||
|
return None
|
||||||
|
|
||||||
|
if isinstance(scorecard_data, str):
|
||||||
|
try:
|
||||||
|
scorecard_data = json.loads(scorecard_data)
|
||||||
|
except (json.JSONDecodeError, TypeError):
|
||||||
|
return None
|
||||||
|
|
||||||
|
if not isinstance(scorecard_data, dict):
|
||||||
|
return None
|
||||||
|
|
||||||
|
return {
|
||||||
|
"date": timeframe,
|
||||||
|
"total_pnl": float(scorecard_data.get("total_pnl", 0.0)),
|
||||||
|
"win_rate": float(scorecard_data.get("win_rate", 0.0)),
|
||||||
|
"lessons": scorecard_data.get("lessons", []),
|
||||||
|
}
|
||||||
|
|
||||||
def _gather_context(self) -> dict[str, Any]:
|
def _gather_context(self) -> dict[str, Any]:
|
||||||
"""Gather strategic context using ContextSelector."""
|
"""Gather strategic context using ContextSelector."""
|
||||||
layers = self._context_selector.select_layers(
|
layers = self._context_selector.select_layers(
|
||||||
@@ -188,6 +227,7 @@ class PreMarketPlanner:
|
|||||||
market: str,
|
market: str,
|
||||||
candidates: list[ScanCandidate],
|
candidates: list[ScanCandidate],
|
||||||
context_data: dict[str, Any],
|
context_data: dict[str, Any],
|
||||||
|
self_market_scorecard: dict[str, Any] | None,
|
||||||
cross_market: CrossMarketContext | None,
|
cross_market: CrossMarketContext | None,
|
||||||
) -> str:
|
) -> str:
|
||||||
"""Build a structured prompt for Gemini to generate scenario JSON."""
|
"""Build a structured prompt for Gemini to generate scenario JSON."""
|
||||||
@@ -211,6 +251,18 @@ class PreMarketPlanner:
|
|||||||
if cross_market.lessons:
|
if cross_market.lessons:
|
||||||
cross_market_text += f"- Lessons: {'; '.join(cross_market.lessons[:3])}\n"
|
cross_market_text += f"- Lessons: {'; '.join(cross_market.lessons[:3])}\n"
|
||||||
|
|
||||||
|
self_market_text = ""
|
||||||
|
if self_market_scorecard:
|
||||||
|
self_market_text = (
|
||||||
|
f"\n## My Market Previous Day ({market})\n"
|
||||||
|
f"- Date: {self_market_scorecard['date']}\n"
|
||||||
|
f"- P&L: {self_market_scorecard['total_pnl']:+.2f}%\n"
|
||||||
|
f"- Win Rate: {self_market_scorecard['win_rate']:.0f}%\n"
|
||||||
|
)
|
||||||
|
lessons = self_market_scorecard.get("lessons", [])
|
||||||
|
if lessons:
|
||||||
|
self_market_text += f"- Lessons: {'; '.join(lessons[:3])}\n"
|
||||||
|
|
||||||
context_text = ""
|
context_text = ""
|
||||||
if context_data:
|
if context_data:
|
||||||
context_text = "\n## Strategic Context\n"
|
context_text = "\n## Strategic Context\n"
|
||||||
@@ -224,6 +276,7 @@ class PreMarketPlanner:
|
|||||||
f"You are a pre-market trading strategist for the {market} market.\n"
|
f"You are a pre-market trading strategist for the {market} market.\n"
|
||||||
f"Generate structured trading scenarios for today.\n\n"
|
f"Generate structured trading scenarios for today.\n\n"
|
||||||
f"## Candidates (from volatility scanner)\n{candidates_text}\n"
|
f"## Candidates (from volatility scanner)\n{candidates_text}\n"
|
||||||
|
f"{self_market_text}"
|
||||||
f"{cross_market_text}"
|
f"{cross_market_text}"
|
||||||
f"{context_text}\n"
|
f"{context_text}\n"
|
||||||
f"## Instructions\n"
|
f"## Instructions\n"
|
||||||
|
|||||||
387
tests/test_daily_review.py
Normal file
387
tests/test_daily_review.py
Normal file
@@ -0,0 +1,387 @@
|
|||||||
|
"""Tests for DailyReviewer."""
|
||||||
|
|
||||||
|
from __future__ import annotations
|
||||||
|
|
||||||
|
import json
|
||||||
|
import sqlite3
|
||||||
|
from types import SimpleNamespace
|
||||||
|
from unittest.mock import AsyncMock, MagicMock
|
||||||
|
|
||||||
|
import pytest
|
||||||
|
|
||||||
|
from src.context.layer import ContextLayer
|
||||||
|
from src.context.store import ContextStore
|
||||||
|
from src.db import init_db, log_trade
|
||||||
|
from src.evolution.daily_review import DailyReviewer
|
||||||
|
from src.evolution.scorecard import DailyScorecard
|
||||||
|
from src.logging.decision_logger import DecisionLogger
|
||||||
|
|
||||||
|
from datetime import UTC, datetime
|
||||||
|
|
||||||
|
TODAY = datetime.now(UTC).strftime("%Y-%m-%d")
|
||||||
|
|
||||||
|
|
||||||
|
@pytest.fixture
|
||||||
|
def db_conn() -> sqlite3.Connection:
|
||||||
|
return init_db(":memory:")
|
||||||
|
|
||||||
|
|
||||||
|
@pytest.fixture
|
||||||
|
def context_store(db_conn: sqlite3.Connection) -> ContextStore:
|
||||||
|
return ContextStore(db_conn)
|
||||||
|
|
||||||
|
|
||||||
|
def _log_decision(
|
||||||
|
logger: DecisionLogger,
|
||||||
|
*,
|
||||||
|
stock_code: str,
|
||||||
|
market: str,
|
||||||
|
action: str,
|
||||||
|
confidence: int,
|
||||||
|
scenario_match: dict[str, float] | None = None,
|
||||||
|
) -> str:
|
||||||
|
return logger.log_decision(
|
||||||
|
stock_code=stock_code,
|
||||||
|
market=market,
|
||||||
|
exchange_code="KRX" if market == "KR" else "NASDAQ",
|
||||||
|
action=action,
|
||||||
|
confidence=confidence,
|
||||||
|
rationale="test",
|
||||||
|
context_snapshot={"scenario_match": scenario_match or {}},
|
||||||
|
input_data={"stock_code": stock_code},
|
||||||
|
)
|
||||||
|
|
||||||
|
|
||||||
|
def test_generate_scorecard_market_scoped(
|
||||||
|
db_conn: sqlite3.Connection, context_store: ContextStore,
|
||||||
|
) -> None:
|
||||||
|
reviewer = DailyReviewer(db_conn, context_store)
|
||||||
|
logger = DecisionLogger(db_conn)
|
||||||
|
|
||||||
|
buy_id = _log_decision(
|
||||||
|
logger,
|
||||||
|
stock_code="005930",
|
||||||
|
market="KR",
|
||||||
|
action="BUY",
|
||||||
|
confidence=90,
|
||||||
|
scenario_match={"rsi": 29.0},
|
||||||
|
)
|
||||||
|
_log_decision(
|
||||||
|
logger,
|
||||||
|
stock_code="000660",
|
||||||
|
market="KR",
|
||||||
|
action="HOLD",
|
||||||
|
confidence=60,
|
||||||
|
)
|
||||||
|
_log_decision(
|
||||||
|
logger,
|
||||||
|
stock_code="AAPL",
|
||||||
|
market="US",
|
||||||
|
action="SELL",
|
||||||
|
confidence=80,
|
||||||
|
scenario_match={"volume_ratio": 2.1},
|
||||||
|
)
|
||||||
|
|
||||||
|
log_trade(
|
||||||
|
db_conn,
|
||||||
|
"005930",
|
||||||
|
"BUY",
|
||||||
|
90,
|
||||||
|
"buy",
|
||||||
|
quantity=1,
|
||||||
|
price=100.0,
|
||||||
|
pnl=10.0,
|
||||||
|
market="KR",
|
||||||
|
exchange_code="KRX",
|
||||||
|
decision_id=buy_id,
|
||||||
|
)
|
||||||
|
log_trade(
|
||||||
|
db_conn,
|
||||||
|
"000660",
|
||||||
|
"HOLD",
|
||||||
|
60,
|
||||||
|
"hold",
|
||||||
|
quantity=0,
|
||||||
|
price=0.0,
|
||||||
|
pnl=0.0,
|
||||||
|
market="KR",
|
||||||
|
exchange_code="KRX",
|
||||||
|
)
|
||||||
|
log_trade(
|
||||||
|
db_conn,
|
||||||
|
"AAPL",
|
||||||
|
"SELL",
|
||||||
|
80,
|
||||||
|
"sell",
|
||||||
|
quantity=1,
|
||||||
|
price=200.0,
|
||||||
|
pnl=-5.0,
|
||||||
|
market="US",
|
||||||
|
exchange_code="NASDAQ",
|
||||||
|
)
|
||||||
|
|
||||||
|
scorecard = reviewer.generate_scorecard(TODAY, "KR")
|
||||||
|
|
||||||
|
assert scorecard.market == "KR"
|
||||||
|
assert scorecard.total_decisions == 2
|
||||||
|
assert scorecard.buys == 1
|
||||||
|
assert scorecard.sells == 0
|
||||||
|
assert scorecard.holds == 1
|
||||||
|
assert scorecard.total_pnl == 10.0
|
||||||
|
assert scorecard.win_rate == 100.0
|
||||||
|
assert scorecard.avg_confidence == 75.0
|
||||||
|
assert scorecard.scenario_match_rate == 50.0
|
||||||
|
|
||||||
|
|
||||||
|
def test_generate_scorecard_top_winners_and_losers(
|
||||||
|
db_conn: sqlite3.Connection, context_store: ContextStore,
|
||||||
|
) -> None:
|
||||||
|
reviewer = DailyReviewer(db_conn, context_store)
|
||||||
|
logger = DecisionLogger(db_conn)
|
||||||
|
|
||||||
|
for code, pnl in [("005930", 30.0), ("000660", 10.0), ("035420", -15.0), ("051910", -5.0)]:
|
||||||
|
decision_id = _log_decision(
|
||||||
|
logger,
|
||||||
|
stock_code=code,
|
||||||
|
market="KR",
|
||||||
|
action="BUY" if pnl >= 0 else "SELL",
|
||||||
|
confidence=80,
|
||||||
|
scenario_match={"rsi": 30.0},
|
||||||
|
)
|
||||||
|
log_trade(
|
||||||
|
db_conn,
|
||||||
|
code,
|
||||||
|
"BUY" if pnl >= 0 else "SELL",
|
||||||
|
80,
|
||||||
|
"test",
|
||||||
|
quantity=1,
|
||||||
|
price=100.0,
|
||||||
|
pnl=pnl,
|
||||||
|
market="KR",
|
||||||
|
exchange_code="KRX",
|
||||||
|
decision_id=decision_id,
|
||||||
|
)
|
||||||
|
|
||||||
|
scorecard = reviewer.generate_scorecard(TODAY, "KR")
|
||||||
|
assert scorecard.top_winners == ["005930", "000660"]
|
||||||
|
assert scorecard.top_losers == ["035420", "051910"]
|
||||||
|
|
||||||
|
|
||||||
|
def test_generate_scorecard_empty_day(
|
||||||
|
db_conn: sqlite3.Connection, context_store: ContextStore,
|
||||||
|
) -> None:
|
||||||
|
reviewer = DailyReviewer(db_conn, context_store)
|
||||||
|
scorecard = reviewer.generate_scorecard(TODAY, "KR")
|
||||||
|
|
||||||
|
assert scorecard.total_decisions == 0
|
||||||
|
assert scorecard.total_pnl == 0.0
|
||||||
|
assert scorecard.win_rate == 0.0
|
||||||
|
assert scorecard.avg_confidence == 0.0
|
||||||
|
assert scorecard.scenario_match_rate == 0.0
|
||||||
|
assert scorecard.top_winners == []
|
||||||
|
assert scorecard.top_losers == []
|
||||||
|
|
||||||
|
|
||||||
|
@pytest.mark.asyncio
|
||||||
|
async def test_generate_lessons_without_gemini_returns_empty(
|
||||||
|
db_conn: sqlite3.Connection, context_store: ContextStore,
|
||||||
|
) -> None:
|
||||||
|
reviewer = DailyReviewer(db_conn, context_store, gemini_client=None)
|
||||||
|
lessons = await reviewer.generate_lessons(
|
||||||
|
DailyScorecard(
|
||||||
|
date="2026-02-14",
|
||||||
|
market="KR",
|
||||||
|
total_decisions=1,
|
||||||
|
buys=1,
|
||||||
|
sells=0,
|
||||||
|
holds=0,
|
||||||
|
total_pnl=5.0,
|
||||||
|
win_rate=100.0,
|
||||||
|
avg_confidence=90.0,
|
||||||
|
scenario_match_rate=100.0,
|
||||||
|
)
|
||||||
|
)
|
||||||
|
assert lessons == []
|
||||||
|
|
||||||
|
|
||||||
|
@pytest.mark.asyncio
|
||||||
|
async def test_generate_lessons_parses_json_array(
|
||||||
|
db_conn: sqlite3.Connection, context_store: ContextStore,
|
||||||
|
) -> None:
|
||||||
|
mock_gemini = MagicMock()
|
||||||
|
mock_gemini.decide = AsyncMock(
|
||||||
|
return_value=SimpleNamespace(rationale='["Cut losers earlier", "Reduce midday churn"]')
|
||||||
|
)
|
||||||
|
reviewer = DailyReviewer(db_conn, context_store, gemini_client=mock_gemini)
|
||||||
|
|
||||||
|
lessons = await reviewer.generate_lessons(
|
||||||
|
DailyScorecard(
|
||||||
|
date="2026-02-14",
|
||||||
|
market="KR",
|
||||||
|
total_decisions=3,
|
||||||
|
buys=1,
|
||||||
|
sells=1,
|
||||||
|
holds=1,
|
||||||
|
total_pnl=-2.5,
|
||||||
|
win_rate=50.0,
|
||||||
|
avg_confidence=70.0,
|
||||||
|
scenario_match_rate=66.7,
|
||||||
|
)
|
||||||
|
)
|
||||||
|
assert lessons == ["Cut losers earlier", "Reduce midday churn"]
|
||||||
|
|
||||||
|
|
||||||
|
@pytest.mark.asyncio
|
||||||
|
async def test_generate_lessons_fallback_to_lines(
|
||||||
|
db_conn: sqlite3.Connection, context_store: ContextStore,
|
||||||
|
) -> None:
|
||||||
|
mock_gemini = MagicMock()
|
||||||
|
mock_gemini.decide = AsyncMock(
|
||||||
|
return_value=SimpleNamespace(rationale="- Keep risk tighter\n- Increase selectivity")
|
||||||
|
)
|
||||||
|
reviewer = DailyReviewer(db_conn, context_store, gemini_client=mock_gemini)
|
||||||
|
|
||||||
|
lessons = await reviewer.generate_lessons(
|
||||||
|
DailyScorecard(
|
||||||
|
date="2026-02-14",
|
||||||
|
market="US",
|
||||||
|
total_decisions=2,
|
||||||
|
buys=1,
|
||||||
|
sells=1,
|
||||||
|
holds=0,
|
||||||
|
total_pnl=1.0,
|
||||||
|
win_rate=50.0,
|
||||||
|
avg_confidence=75.0,
|
||||||
|
scenario_match_rate=100.0,
|
||||||
|
)
|
||||||
|
)
|
||||||
|
assert lessons == ["Keep risk tighter", "Increase selectivity"]
|
||||||
|
|
||||||
|
|
||||||
|
@pytest.mark.asyncio
|
||||||
|
async def test_generate_lessons_handles_gemini_error(
|
||||||
|
db_conn: sqlite3.Connection, context_store: ContextStore,
|
||||||
|
) -> None:
|
||||||
|
mock_gemini = MagicMock()
|
||||||
|
mock_gemini.decide = AsyncMock(side_effect=RuntimeError("boom"))
|
||||||
|
reviewer = DailyReviewer(db_conn, context_store, gemini_client=mock_gemini)
|
||||||
|
|
||||||
|
lessons = await reviewer.generate_lessons(
|
||||||
|
DailyScorecard(
|
||||||
|
date="2026-02-14",
|
||||||
|
market="US",
|
||||||
|
total_decisions=0,
|
||||||
|
buys=0,
|
||||||
|
sells=0,
|
||||||
|
holds=0,
|
||||||
|
total_pnl=0.0,
|
||||||
|
win_rate=0.0,
|
||||||
|
avg_confidence=0.0,
|
||||||
|
scenario_match_rate=0.0,
|
||||||
|
)
|
||||||
|
)
|
||||||
|
assert lessons == []
|
||||||
|
|
||||||
|
|
||||||
|
def test_store_scorecard_in_context(
|
||||||
|
db_conn: sqlite3.Connection, context_store: ContextStore,
|
||||||
|
) -> None:
|
||||||
|
reviewer = DailyReviewer(db_conn, context_store)
|
||||||
|
scorecard = DailyScorecard(
|
||||||
|
date="2026-02-14",
|
||||||
|
market="KR",
|
||||||
|
total_decisions=5,
|
||||||
|
buys=2,
|
||||||
|
sells=1,
|
||||||
|
holds=2,
|
||||||
|
total_pnl=15.0,
|
||||||
|
win_rate=66.67,
|
||||||
|
avg_confidence=82.0,
|
||||||
|
scenario_match_rate=80.0,
|
||||||
|
lessons=["Keep position sizing stable"],
|
||||||
|
cross_market_note="US risk-off",
|
||||||
|
)
|
||||||
|
|
||||||
|
reviewer.store_scorecard_in_context(scorecard)
|
||||||
|
|
||||||
|
stored = context_store.get_context(
|
||||||
|
ContextLayer.L6_DAILY,
|
||||||
|
"2026-02-14",
|
||||||
|
"scorecard_KR",
|
||||||
|
)
|
||||||
|
assert stored is not None
|
||||||
|
assert stored["market"] == "KR"
|
||||||
|
assert stored["total_pnl"] == 15.0
|
||||||
|
assert stored["lessons"] == ["Keep position sizing stable"]
|
||||||
|
|
||||||
|
|
||||||
|
def test_store_scorecard_key_is_market_scoped(
|
||||||
|
db_conn: sqlite3.Connection, context_store: ContextStore,
|
||||||
|
) -> None:
|
||||||
|
reviewer = DailyReviewer(db_conn, context_store)
|
||||||
|
kr = DailyScorecard(
|
||||||
|
date="2026-02-14",
|
||||||
|
market="KR",
|
||||||
|
total_decisions=1,
|
||||||
|
buys=1,
|
||||||
|
sells=0,
|
||||||
|
holds=0,
|
||||||
|
total_pnl=1.0,
|
||||||
|
win_rate=100.0,
|
||||||
|
avg_confidence=90.0,
|
||||||
|
scenario_match_rate=100.0,
|
||||||
|
)
|
||||||
|
us = DailyScorecard(
|
||||||
|
date="2026-02-14",
|
||||||
|
market="US",
|
||||||
|
total_decisions=1,
|
||||||
|
buys=0,
|
||||||
|
sells=1,
|
||||||
|
holds=0,
|
||||||
|
total_pnl=-1.0,
|
||||||
|
win_rate=0.0,
|
||||||
|
avg_confidence=70.0,
|
||||||
|
scenario_match_rate=100.0,
|
||||||
|
)
|
||||||
|
|
||||||
|
reviewer.store_scorecard_in_context(kr)
|
||||||
|
reviewer.store_scorecard_in_context(us)
|
||||||
|
|
||||||
|
kr_ctx = context_store.get_context(ContextLayer.L6_DAILY, "2026-02-14", "scorecard_KR")
|
||||||
|
us_ctx = context_store.get_context(ContextLayer.L6_DAILY, "2026-02-14", "scorecard_US")
|
||||||
|
|
||||||
|
assert kr_ctx["market"] == "KR"
|
||||||
|
assert us_ctx["market"] == "US"
|
||||||
|
assert kr_ctx["total_pnl"] == 1.0
|
||||||
|
assert us_ctx["total_pnl"] == -1.0
|
||||||
|
|
||||||
|
|
||||||
|
def test_generate_scorecard_handles_invalid_context_snapshot(
|
||||||
|
db_conn: sqlite3.Connection, context_store: ContextStore,
|
||||||
|
) -> None:
|
||||||
|
reviewer = DailyReviewer(db_conn, context_store)
|
||||||
|
db_conn.execute(
|
||||||
|
"""
|
||||||
|
INSERT INTO decision_logs (
|
||||||
|
decision_id, timestamp, stock_code, market, exchange_code,
|
||||||
|
action, confidence, rationale, context_snapshot, input_data
|
||||||
|
) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?)
|
||||||
|
""",
|
||||||
|
(
|
||||||
|
"d1",
|
||||||
|
"2026-02-14T09:00:00+00:00",
|
||||||
|
"005930",
|
||||||
|
"KR",
|
||||||
|
"KRX",
|
||||||
|
"HOLD",
|
||||||
|
50,
|
||||||
|
"test",
|
||||||
|
"{invalid_json",
|
||||||
|
json.dumps({}),
|
||||||
|
),
|
||||||
|
)
|
||||||
|
db_conn.commit()
|
||||||
|
|
||||||
|
scorecard = reviewer.generate_scorecard("2026-02-14", "KR")
|
||||||
|
assert scorecard.total_decisions == 1
|
||||||
|
assert scorecard.scenario_match_rate == 0.0
|
||||||
270
tests/test_dashboard.py
Normal file
270
tests/test_dashboard.py
Normal file
@@ -0,0 +1,270 @@
|
|||||||
|
"""Tests for FastAPI dashboard endpoints."""
|
||||||
|
|
||||||
|
from __future__ import annotations
|
||||||
|
|
||||||
|
import json
|
||||||
|
import sqlite3
|
||||||
|
from pathlib import Path
|
||||||
|
|
||||||
|
import pytest
|
||||||
|
|
||||||
|
pytest.importorskip("fastapi")
|
||||||
|
from fastapi.testclient import TestClient
|
||||||
|
|
||||||
|
from src.dashboard.app import create_dashboard_app
|
||||||
|
from src.db import init_db
|
||||||
|
|
||||||
|
|
||||||
|
def _seed_db(conn: sqlite3.Connection) -> None:
|
||||||
|
conn.execute(
|
||||||
|
"""
|
||||||
|
INSERT INTO playbooks (
|
||||||
|
date, market, status, playbook_json, generated_at,
|
||||||
|
token_count, scenario_count, match_count
|
||||||
|
) VALUES (?, ?, ?, ?, ?, ?, ?, ?)
|
||||||
|
""",
|
||||||
|
(
|
||||||
|
"2026-02-14",
|
||||||
|
"KR",
|
||||||
|
"ready",
|
||||||
|
json.dumps({"market": "KR", "stock_playbooks": []}),
|
||||||
|
"2026-02-14T08:30:00+00:00",
|
||||||
|
123,
|
||||||
|
2,
|
||||||
|
1,
|
||||||
|
),
|
||||||
|
)
|
||||||
|
conn.execute(
|
||||||
|
"""
|
||||||
|
INSERT INTO contexts (layer, timeframe, key, value, created_at, updated_at)
|
||||||
|
VALUES (?, ?, ?, ?, ?, ?)
|
||||||
|
""",
|
||||||
|
(
|
||||||
|
"L6_DAILY",
|
||||||
|
"2026-02-14",
|
||||||
|
"scorecard_KR",
|
||||||
|
json.dumps({"market": "KR", "total_pnl": 1.5, "win_rate": 60.0}),
|
||||||
|
"2026-02-14T15:30:00+00:00",
|
||||||
|
"2026-02-14T15:30:00+00:00",
|
||||||
|
),
|
||||||
|
)
|
||||||
|
conn.execute(
|
||||||
|
"""
|
||||||
|
INSERT INTO contexts (layer, timeframe, key, value, created_at, updated_at)
|
||||||
|
VALUES (?, ?, ?, ?, ?, ?)
|
||||||
|
""",
|
||||||
|
(
|
||||||
|
"L7_REALTIME",
|
||||||
|
"2026-02-14T10:00:00+00:00",
|
||||||
|
"volatility_KR_005930",
|
||||||
|
json.dumps({"momentum_score": 70.0}),
|
||||||
|
"2026-02-14T10:00:00+00:00",
|
||||||
|
"2026-02-14T10:00:00+00:00",
|
||||||
|
),
|
||||||
|
)
|
||||||
|
conn.execute(
|
||||||
|
"""
|
||||||
|
INSERT INTO decision_logs (
|
||||||
|
decision_id, timestamp, stock_code, market, exchange_code,
|
||||||
|
action, confidence, rationale, context_snapshot, input_data
|
||||||
|
) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?)
|
||||||
|
""",
|
||||||
|
(
|
||||||
|
"d-kr-1",
|
||||||
|
"2026-02-14T09:10:00+00:00",
|
||||||
|
"005930",
|
||||||
|
"KR",
|
||||||
|
"KRX",
|
||||||
|
"BUY",
|
||||||
|
85,
|
||||||
|
"signal matched",
|
||||||
|
json.dumps({"scenario_match": {"rsi": 28.0}}),
|
||||||
|
json.dumps({"current_price": 70000}),
|
||||||
|
),
|
||||||
|
)
|
||||||
|
conn.execute(
|
||||||
|
"""
|
||||||
|
INSERT INTO decision_logs (
|
||||||
|
decision_id, timestamp, stock_code, market, exchange_code,
|
||||||
|
action, confidence, rationale, context_snapshot, input_data
|
||||||
|
) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?)
|
||||||
|
""",
|
||||||
|
(
|
||||||
|
"d-us-1",
|
||||||
|
"2026-02-14T21:10:00+00:00",
|
||||||
|
"AAPL",
|
||||||
|
"US",
|
||||||
|
"NASDAQ",
|
||||||
|
"SELL",
|
||||||
|
80,
|
||||||
|
"no match",
|
||||||
|
json.dumps({"scenario_match": {}}),
|
||||||
|
json.dumps({"current_price": 200}),
|
||||||
|
),
|
||||||
|
)
|
||||||
|
conn.execute(
|
||||||
|
"""
|
||||||
|
INSERT INTO trades (
|
||||||
|
timestamp, stock_code, action, confidence, rationale,
|
||||||
|
quantity, price, pnl, market, exchange_code, selection_context, decision_id
|
||||||
|
) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)
|
||||||
|
""",
|
||||||
|
(
|
||||||
|
"2026-02-14T09:11:00+00:00",
|
||||||
|
"005930",
|
||||||
|
"BUY",
|
||||||
|
85,
|
||||||
|
"buy",
|
||||||
|
1,
|
||||||
|
70000,
|
||||||
|
2.0,
|
||||||
|
"KR",
|
||||||
|
"KRX",
|
||||||
|
None,
|
||||||
|
"d-kr-1",
|
||||||
|
),
|
||||||
|
)
|
||||||
|
conn.execute(
|
||||||
|
"""
|
||||||
|
INSERT INTO trades (
|
||||||
|
timestamp, stock_code, action, confidence, rationale,
|
||||||
|
quantity, price, pnl, market, exchange_code, selection_context, decision_id
|
||||||
|
) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)
|
||||||
|
""",
|
||||||
|
(
|
||||||
|
"2026-02-14T21:11:00+00:00",
|
||||||
|
"AAPL",
|
||||||
|
"SELL",
|
||||||
|
80,
|
||||||
|
"sell",
|
||||||
|
1,
|
||||||
|
200,
|
||||||
|
-1.0,
|
||||||
|
"US",
|
||||||
|
"NASDAQ",
|
||||||
|
None,
|
||||||
|
"d-us-1",
|
||||||
|
),
|
||||||
|
)
|
||||||
|
conn.commit()
|
||||||
|
|
||||||
|
|
||||||
|
def _client(tmp_path: Path) -> TestClient:
|
||||||
|
db_path = tmp_path / "dashboard_test.db"
|
||||||
|
conn = init_db(str(db_path))
|
||||||
|
_seed_db(conn)
|
||||||
|
conn.close()
|
||||||
|
app = create_dashboard_app(str(db_path))
|
||||||
|
return TestClient(app)
|
||||||
|
|
||||||
|
|
||||||
|
def test_index_serves_html(tmp_path: Path) -> None:
|
||||||
|
client = _client(tmp_path)
|
||||||
|
resp = client.get("/")
|
||||||
|
assert resp.status_code == 200
|
||||||
|
assert "The Ouroboros Dashboard API" in resp.text
|
||||||
|
|
||||||
|
|
||||||
|
def test_status_endpoint(tmp_path: Path) -> None:
|
||||||
|
client = _client(tmp_path)
|
||||||
|
resp = client.get("/api/status")
|
||||||
|
assert resp.status_code == 200
|
||||||
|
body = resp.json()
|
||||||
|
assert "KR" in body["markets"]
|
||||||
|
assert "US" in body["markets"]
|
||||||
|
assert "totals" in body
|
||||||
|
|
||||||
|
|
||||||
|
def test_playbook_found(tmp_path: Path) -> None:
|
||||||
|
client = _client(tmp_path)
|
||||||
|
resp = client.get("/api/playbook/2026-02-14?market=KR")
|
||||||
|
assert resp.status_code == 200
|
||||||
|
assert resp.json()["market"] == "KR"
|
||||||
|
|
||||||
|
|
||||||
|
def test_playbook_not_found(tmp_path: Path) -> None:
|
||||||
|
client = _client(tmp_path)
|
||||||
|
resp = client.get("/api/playbook/2026-02-15?market=KR")
|
||||||
|
assert resp.status_code == 404
|
||||||
|
|
||||||
|
|
||||||
|
def test_scorecard_found(tmp_path: Path) -> None:
|
||||||
|
client = _client(tmp_path)
|
||||||
|
resp = client.get("/api/scorecard/2026-02-14?market=KR")
|
||||||
|
assert resp.status_code == 200
|
||||||
|
assert resp.json()["scorecard"]["total_pnl"] == 1.5
|
||||||
|
|
||||||
|
|
||||||
|
def test_scorecard_not_found(tmp_path: Path) -> None:
|
||||||
|
client = _client(tmp_path)
|
||||||
|
resp = client.get("/api/scorecard/2026-02-15?market=KR")
|
||||||
|
assert resp.status_code == 404
|
||||||
|
|
||||||
|
|
||||||
|
def test_performance_all(tmp_path: Path) -> None:
|
||||||
|
client = _client(tmp_path)
|
||||||
|
resp = client.get("/api/performance?market=all")
|
||||||
|
assert resp.status_code == 200
|
||||||
|
body = resp.json()
|
||||||
|
assert body["market"] == "all"
|
||||||
|
assert body["combined"]["total_trades"] == 2
|
||||||
|
assert len(body["by_market"]) == 2
|
||||||
|
|
||||||
|
|
||||||
|
def test_performance_market_filter(tmp_path: Path) -> None:
|
||||||
|
client = _client(tmp_path)
|
||||||
|
resp = client.get("/api/performance?market=KR")
|
||||||
|
assert resp.status_code == 200
|
||||||
|
body = resp.json()
|
||||||
|
assert body["market"] == "KR"
|
||||||
|
assert body["metrics"]["total_trades"] == 1
|
||||||
|
|
||||||
|
|
||||||
|
def test_performance_empty_market(tmp_path: Path) -> None:
|
||||||
|
client = _client(tmp_path)
|
||||||
|
resp = client.get("/api/performance?market=JP")
|
||||||
|
assert resp.status_code == 200
|
||||||
|
assert resp.json()["metrics"]["total_trades"] == 0
|
||||||
|
|
||||||
|
|
||||||
|
def test_context_layer_all(tmp_path: Path) -> None:
|
||||||
|
client = _client(tmp_path)
|
||||||
|
resp = client.get("/api/context/L7_REALTIME")
|
||||||
|
assert resp.status_code == 200
|
||||||
|
body = resp.json()
|
||||||
|
assert body["layer"] == "L7_REALTIME"
|
||||||
|
assert body["count"] == 1
|
||||||
|
|
||||||
|
|
||||||
|
def test_context_layer_timeframe_filter(tmp_path: Path) -> None:
|
||||||
|
client = _client(tmp_path)
|
||||||
|
resp = client.get("/api/context/L6_DAILY?timeframe=2026-02-14")
|
||||||
|
assert resp.status_code == 200
|
||||||
|
body = resp.json()
|
||||||
|
assert body["count"] == 1
|
||||||
|
assert body["entries"][0]["key"] == "scorecard_KR"
|
||||||
|
|
||||||
|
|
||||||
|
def test_decisions_endpoint(tmp_path: Path) -> None:
|
||||||
|
client = _client(tmp_path)
|
||||||
|
resp = client.get("/api/decisions?market=KR")
|
||||||
|
assert resp.status_code == 200
|
||||||
|
body = resp.json()
|
||||||
|
assert body["count"] == 1
|
||||||
|
assert body["decisions"][0]["decision_id"] == "d-kr-1"
|
||||||
|
|
||||||
|
|
||||||
|
def test_scenarios_active_filters_non_matched(tmp_path: Path) -> None:
|
||||||
|
client = _client(tmp_path)
|
||||||
|
resp = client.get("/api/scenarios/active?market=KR&date_str=2026-02-14")
|
||||||
|
assert resp.status_code == 200
|
||||||
|
body = resp.json()
|
||||||
|
assert body["count"] == 1
|
||||||
|
assert body["matches"][0]["stock_code"] == "005930"
|
||||||
|
|
||||||
|
|
||||||
|
def test_scenarios_active_empty_when_no_matches(tmp_path: Path) -> None:
|
||||||
|
client = _client(tmp_path)
|
||||||
|
resp = client.get("/api/scenarios/active?market=US&date_str=2026-02-14")
|
||||||
|
assert resp.status_code == 200
|
||||||
|
assert resp.json()["count"] == 0
|
||||||
@@ -1,13 +1,26 @@
|
|||||||
"""Tests for main trading loop integration."""
|
"""Tests for main trading loop integration."""
|
||||||
|
|
||||||
from datetime import date
|
from datetime import UTC, date, datetime
|
||||||
from unittest.mock import ANY, AsyncMock, MagicMock, patch
|
from unittest.mock import ANY, AsyncMock, MagicMock, patch
|
||||||
|
|
||||||
import pytest
|
import pytest
|
||||||
|
|
||||||
from src.core.risk_manager import CircuitBreakerTripped, FatFingerRejected
|
from src.config import Settings
|
||||||
from src.context.layer import ContextLayer
|
from src.context.layer import ContextLayer
|
||||||
from src.main import safe_float, trading_cycle
|
from src.context.scheduler import ScheduleResult
|
||||||
|
from src.core.risk_manager import CircuitBreakerTripped, FatFingerRejected
|
||||||
|
from src.db import init_db, log_trade
|
||||||
|
from src.evolution.scorecard import DailyScorecard
|
||||||
|
from src.logging.decision_logger import DecisionLogger
|
||||||
|
from src.main import (
|
||||||
|
_apply_dashboard_flag,
|
||||||
|
_handle_market_close,
|
||||||
|
_run_context_scheduler,
|
||||||
|
_run_evolution_loop,
|
||||||
|
_start_dashboard_server,
|
||||||
|
safe_float,
|
||||||
|
trading_cycle,
|
||||||
|
)
|
||||||
from src.strategy.models import (
|
from src.strategy.models import (
|
||||||
DayPlaybook,
|
DayPlaybook,
|
||||||
ScenarioAction,
|
ScenarioAction,
|
||||||
@@ -44,6 +57,17 @@ def _make_hold_match(stock_code: str = "005930") -> ScenarioMatch:
|
|||||||
)
|
)
|
||||||
|
|
||||||
|
|
||||||
|
def _make_sell_match(stock_code: str = "005930") -> ScenarioMatch:
|
||||||
|
"""Create a ScenarioMatch that returns SELL."""
|
||||||
|
return ScenarioMatch(
|
||||||
|
stock_code=stock_code,
|
||||||
|
matched_scenario=None,
|
||||||
|
action=ScenarioAction.SELL,
|
||||||
|
confidence=90,
|
||||||
|
rationale="Test sell",
|
||||||
|
)
|
||||||
|
|
||||||
|
|
||||||
class TestSafeFloat:
|
class TestSafeFloat:
|
||||||
"""Test safe_float() helper function."""
|
"""Test safe_float() helper function."""
|
||||||
|
|
||||||
@@ -1113,3 +1137,364 @@ class TestScenarioEngineIntegration:
|
|||||||
# REDUCE_ALL is not BUY or SELL — no order sent
|
# REDUCE_ALL is not BUY or SELL — no order sent
|
||||||
mock_broker.send_order.assert_not_called()
|
mock_broker.send_order.assert_not_called()
|
||||||
mock_telegram.notify_trade_execution.assert_not_called()
|
mock_telegram.notify_trade_execution.assert_not_called()
|
||||||
|
|
||||||
|
|
||||||
|
@pytest.mark.asyncio
|
||||||
|
async def test_sell_updates_original_buy_decision_outcome() -> None:
|
||||||
|
"""SELL should update the original BUY decision outcome in decision_logs."""
|
||||||
|
db_conn = init_db(":memory:")
|
||||||
|
decision_logger = DecisionLogger(db_conn)
|
||||||
|
|
||||||
|
buy_decision_id = decision_logger.log_decision(
|
||||||
|
stock_code="005930",
|
||||||
|
market="KR",
|
||||||
|
exchange_code="KRX",
|
||||||
|
action="BUY",
|
||||||
|
confidence=85,
|
||||||
|
rationale="Initial buy",
|
||||||
|
context_snapshot={},
|
||||||
|
input_data={},
|
||||||
|
)
|
||||||
|
log_trade(
|
||||||
|
conn=db_conn,
|
||||||
|
stock_code="005930",
|
||||||
|
action="BUY",
|
||||||
|
confidence=85,
|
||||||
|
rationale="Initial buy",
|
||||||
|
quantity=1,
|
||||||
|
price=100.0,
|
||||||
|
pnl=0.0,
|
||||||
|
market="KR",
|
||||||
|
exchange_code="KRX",
|
||||||
|
decision_id=buy_decision_id,
|
||||||
|
)
|
||||||
|
|
||||||
|
broker = MagicMock()
|
||||||
|
broker.get_orderbook = AsyncMock(
|
||||||
|
return_value={"output1": {"stck_prpr": "120", "frgn_ntby_qty": "0"}}
|
||||||
|
)
|
||||||
|
broker.get_balance = AsyncMock(
|
||||||
|
return_value={
|
||||||
|
"output2": [
|
||||||
|
{
|
||||||
|
"tot_evlu_amt": "100000",
|
||||||
|
"dnca_tot_amt": "10000",
|
||||||
|
"pchs_amt_smtl_amt": "90000",
|
||||||
|
}
|
||||||
|
]
|
||||||
|
}
|
||||||
|
)
|
||||||
|
broker.send_order = AsyncMock(return_value={"msg1": "OK"})
|
||||||
|
|
||||||
|
overseas_broker = MagicMock()
|
||||||
|
engine = MagicMock(spec=ScenarioEngine)
|
||||||
|
engine.evaluate = MagicMock(return_value=_make_sell_match())
|
||||||
|
risk = MagicMock()
|
||||||
|
context_store = MagicMock(
|
||||||
|
get_latest_timeframe=MagicMock(return_value=None),
|
||||||
|
set_context=MagicMock(),
|
||||||
|
)
|
||||||
|
criticality_assessor = MagicMock(
|
||||||
|
assess_market_conditions=MagicMock(return_value=MagicMock(value="NORMAL")),
|
||||||
|
get_timeout=MagicMock(return_value=5.0),
|
||||||
|
)
|
||||||
|
telegram = MagicMock()
|
||||||
|
telegram.notify_trade_execution = AsyncMock()
|
||||||
|
telegram.notify_fat_finger = AsyncMock()
|
||||||
|
telegram.notify_circuit_breaker = AsyncMock()
|
||||||
|
telegram.notify_scenario_matched = AsyncMock()
|
||||||
|
|
||||||
|
market = MagicMock()
|
||||||
|
market.name = "Korea"
|
||||||
|
market.code = "KR"
|
||||||
|
market.exchange_code = "KRX"
|
||||||
|
market.is_domestic = True
|
||||||
|
|
||||||
|
await trading_cycle(
|
||||||
|
broker=broker,
|
||||||
|
overseas_broker=overseas_broker,
|
||||||
|
scenario_engine=engine,
|
||||||
|
playbook=_make_playbook(),
|
||||||
|
risk=risk,
|
||||||
|
db_conn=db_conn,
|
||||||
|
decision_logger=decision_logger,
|
||||||
|
context_store=context_store,
|
||||||
|
criticality_assessor=criticality_assessor,
|
||||||
|
telegram=telegram,
|
||||||
|
market=market,
|
||||||
|
stock_code="005930",
|
||||||
|
scan_candidates={},
|
||||||
|
)
|
||||||
|
|
||||||
|
updated_buy = decision_logger.get_decision_by_id(buy_decision_id)
|
||||||
|
assert updated_buy is not None
|
||||||
|
assert updated_buy.outcome_pnl == 20.0
|
||||||
|
assert updated_buy.outcome_accuracy == 1
|
||||||
|
|
||||||
|
|
||||||
|
@pytest.mark.asyncio
|
||||||
|
async def test_handle_market_close_runs_daily_review_flow() -> None:
|
||||||
|
"""Market close should aggregate, create scorecard, lessons, and notify."""
|
||||||
|
telegram = MagicMock()
|
||||||
|
telegram.notify_market_close = AsyncMock()
|
||||||
|
telegram.send_message = AsyncMock()
|
||||||
|
|
||||||
|
context_aggregator = MagicMock()
|
||||||
|
reviewer = MagicMock()
|
||||||
|
reviewer.generate_scorecard.return_value = DailyScorecard(
|
||||||
|
date="2026-02-14",
|
||||||
|
market="KR",
|
||||||
|
total_decisions=3,
|
||||||
|
buys=1,
|
||||||
|
sells=1,
|
||||||
|
holds=1,
|
||||||
|
total_pnl=12.5,
|
||||||
|
win_rate=50.0,
|
||||||
|
avg_confidence=75.0,
|
||||||
|
scenario_match_rate=66.7,
|
||||||
|
)
|
||||||
|
reviewer.generate_lessons = AsyncMock(return_value=["Cut losers faster"])
|
||||||
|
|
||||||
|
await _handle_market_close(
|
||||||
|
market_code="KR",
|
||||||
|
market_name="Korea",
|
||||||
|
market_timezone=UTC,
|
||||||
|
telegram=telegram,
|
||||||
|
context_aggregator=context_aggregator,
|
||||||
|
daily_reviewer=reviewer,
|
||||||
|
)
|
||||||
|
|
||||||
|
telegram.notify_market_close.assert_called_once_with("Korea", 0.0)
|
||||||
|
context_aggregator.aggregate_daily_from_trades.assert_called_once()
|
||||||
|
reviewer.generate_scorecard.assert_called_once()
|
||||||
|
assert reviewer.store_scorecard_in_context.call_count == 2
|
||||||
|
reviewer.generate_lessons.assert_called_once()
|
||||||
|
telegram.send_message.assert_called_once()
|
||||||
|
|
||||||
|
|
||||||
|
@pytest.mark.asyncio
|
||||||
|
async def test_handle_market_close_without_lessons_stores_once() -> None:
|
||||||
|
"""If no lessons are generated, scorecard should be stored once."""
|
||||||
|
telegram = MagicMock()
|
||||||
|
telegram.notify_market_close = AsyncMock()
|
||||||
|
telegram.send_message = AsyncMock()
|
||||||
|
|
||||||
|
context_aggregator = MagicMock()
|
||||||
|
reviewer = MagicMock()
|
||||||
|
reviewer.generate_scorecard.return_value = DailyScorecard(
|
||||||
|
date="2026-02-14",
|
||||||
|
market="US",
|
||||||
|
total_decisions=1,
|
||||||
|
buys=0,
|
||||||
|
sells=1,
|
||||||
|
holds=0,
|
||||||
|
total_pnl=-3.0,
|
||||||
|
win_rate=0.0,
|
||||||
|
avg_confidence=65.0,
|
||||||
|
scenario_match_rate=100.0,
|
||||||
|
)
|
||||||
|
reviewer.generate_lessons = AsyncMock(return_value=[])
|
||||||
|
|
||||||
|
await _handle_market_close(
|
||||||
|
market_code="US",
|
||||||
|
market_name="United States",
|
||||||
|
market_timezone=UTC,
|
||||||
|
telegram=telegram,
|
||||||
|
context_aggregator=context_aggregator,
|
||||||
|
daily_reviewer=reviewer,
|
||||||
|
)
|
||||||
|
|
||||||
|
assert reviewer.store_scorecard_in_context.call_count == 1
|
||||||
|
|
||||||
|
|
||||||
|
@pytest.mark.asyncio
|
||||||
|
async def test_handle_market_close_triggers_evolution_for_us() -> None:
|
||||||
|
telegram = MagicMock()
|
||||||
|
telegram.notify_market_close = AsyncMock()
|
||||||
|
telegram.send_message = AsyncMock()
|
||||||
|
|
||||||
|
context_aggregator = MagicMock()
|
||||||
|
reviewer = MagicMock()
|
||||||
|
reviewer.generate_scorecard.return_value = DailyScorecard(
|
||||||
|
date="2026-02-14",
|
||||||
|
market="US",
|
||||||
|
total_decisions=2,
|
||||||
|
buys=1,
|
||||||
|
sells=1,
|
||||||
|
holds=0,
|
||||||
|
total_pnl=3.0,
|
||||||
|
win_rate=50.0,
|
||||||
|
avg_confidence=80.0,
|
||||||
|
scenario_match_rate=100.0,
|
||||||
|
)
|
||||||
|
reviewer.generate_lessons = AsyncMock(return_value=[])
|
||||||
|
|
||||||
|
evolution_optimizer = MagicMock()
|
||||||
|
evolution_optimizer.evolve = AsyncMock(return_value=None)
|
||||||
|
|
||||||
|
await _handle_market_close(
|
||||||
|
market_code="US",
|
||||||
|
market_name="United States",
|
||||||
|
market_timezone=UTC,
|
||||||
|
telegram=telegram,
|
||||||
|
context_aggregator=context_aggregator,
|
||||||
|
daily_reviewer=reviewer,
|
||||||
|
evolution_optimizer=evolution_optimizer,
|
||||||
|
)
|
||||||
|
|
||||||
|
evolution_optimizer.evolve.assert_called_once()
|
||||||
|
|
||||||
|
|
||||||
|
@pytest.mark.asyncio
|
||||||
|
async def test_handle_market_close_skips_evolution_for_kr() -> None:
|
||||||
|
telegram = MagicMock()
|
||||||
|
telegram.notify_market_close = AsyncMock()
|
||||||
|
telegram.send_message = AsyncMock()
|
||||||
|
|
||||||
|
context_aggregator = MagicMock()
|
||||||
|
reviewer = MagicMock()
|
||||||
|
reviewer.generate_scorecard.return_value = DailyScorecard(
|
||||||
|
date="2026-02-14",
|
||||||
|
market="KR",
|
||||||
|
total_decisions=1,
|
||||||
|
buys=1,
|
||||||
|
sells=0,
|
||||||
|
holds=0,
|
||||||
|
total_pnl=1.0,
|
||||||
|
win_rate=100.0,
|
||||||
|
avg_confidence=90.0,
|
||||||
|
scenario_match_rate=100.0,
|
||||||
|
)
|
||||||
|
reviewer.generate_lessons = AsyncMock(return_value=[])
|
||||||
|
|
||||||
|
evolution_optimizer = MagicMock()
|
||||||
|
evolution_optimizer.evolve = AsyncMock(return_value=None)
|
||||||
|
|
||||||
|
await _handle_market_close(
|
||||||
|
market_code="KR",
|
||||||
|
market_name="Korea",
|
||||||
|
market_timezone=UTC,
|
||||||
|
telegram=telegram,
|
||||||
|
context_aggregator=context_aggregator,
|
||||||
|
daily_reviewer=reviewer,
|
||||||
|
evolution_optimizer=evolution_optimizer,
|
||||||
|
)
|
||||||
|
|
||||||
|
evolution_optimizer.evolve.assert_not_called()
|
||||||
|
|
||||||
|
|
||||||
|
def test_run_context_scheduler_invokes_scheduler() -> None:
|
||||||
|
"""Scheduler helper should call run_if_due with provided datetime."""
|
||||||
|
scheduler = MagicMock()
|
||||||
|
scheduler.run_if_due = MagicMock(return_value=ScheduleResult(cleanup=True))
|
||||||
|
|
||||||
|
_run_context_scheduler(scheduler, now=datetime(2026, 2, 14, tzinfo=UTC))
|
||||||
|
|
||||||
|
scheduler.run_if_due.assert_called_once()
|
||||||
|
|
||||||
|
|
||||||
|
@pytest.mark.asyncio
|
||||||
|
async def test_run_evolution_loop_skips_non_us_market() -> None:
|
||||||
|
optimizer = MagicMock()
|
||||||
|
optimizer.evolve = AsyncMock()
|
||||||
|
telegram = MagicMock()
|
||||||
|
telegram.send_message = AsyncMock()
|
||||||
|
|
||||||
|
await _run_evolution_loop(
|
||||||
|
evolution_optimizer=optimizer,
|
||||||
|
telegram=telegram,
|
||||||
|
market_code="KR",
|
||||||
|
market_date="2026-02-14",
|
||||||
|
)
|
||||||
|
|
||||||
|
optimizer.evolve.assert_not_called()
|
||||||
|
telegram.send_message.assert_not_called()
|
||||||
|
|
||||||
|
|
||||||
|
@pytest.mark.asyncio
|
||||||
|
async def test_run_evolution_loop_notifies_when_pr_generated() -> None:
|
||||||
|
optimizer = MagicMock()
|
||||||
|
optimizer.evolve = AsyncMock(
|
||||||
|
return_value={
|
||||||
|
"title": "[Evolution] New strategy: v20260214_050000",
|
||||||
|
"branch": "evolution/v20260214_050000",
|
||||||
|
"status": "ready_for_review",
|
||||||
|
}
|
||||||
|
)
|
||||||
|
telegram = MagicMock()
|
||||||
|
telegram.send_message = AsyncMock()
|
||||||
|
|
||||||
|
await _run_evolution_loop(
|
||||||
|
evolution_optimizer=optimizer,
|
||||||
|
telegram=telegram,
|
||||||
|
market_code="US",
|
||||||
|
market_date="2026-02-14",
|
||||||
|
)
|
||||||
|
|
||||||
|
optimizer.evolve.assert_called_once()
|
||||||
|
telegram.send_message.assert_called_once()
|
||||||
|
|
||||||
|
|
||||||
|
@pytest.mark.asyncio
|
||||||
|
async def test_run_evolution_loop_notification_error_is_ignored() -> None:
|
||||||
|
optimizer = MagicMock()
|
||||||
|
optimizer.evolve = AsyncMock(
|
||||||
|
return_value={
|
||||||
|
"title": "[Evolution] New strategy: v20260214_050000",
|
||||||
|
"branch": "evolution/v20260214_050000",
|
||||||
|
"status": "ready_for_review",
|
||||||
|
}
|
||||||
|
)
|
||||||
|
telegram = MagicMock()
|
||||||
|
telegram.send_message = AsyncMock(side_effect=RuntimeError("telegram down"))
|
||||||
|
|
||||||
|
await _run_evolution_loop(
|
||||||
|
evolution_optimizer=optimizer,
|
||||||
|
telegram=telegram,
|
||||||
|
market_code="US",
|
||||||
|
market_date="2026-02-14",
|
||||||
|
)
|
||||||
|
|
||||||
|
optimizer.evolve.assert_called_once()
|
||||||
|
telegram.send_message.assert_called_once()
|
||||||
|
|
||||||
|
|
||||||
|
def test_apply_dashboard_flag_enables_dashboard() -> None:
|
||||||
|
settings = Settings(
|
||||||
|
KIS_APP_KEY="test_key",
|
||||||
|
KIS_APP_SECRET="test_secret",
|
||||||
|
KIS_ACCOUNT_NO="12345678-01",
|
||||||
|
GEMINI_API_KEY="test_gemini_key",
|
||||||
|
DASHBOARD_ENABLED=False,
|
||||||
|
)
|
||||||
|
updated = _apply_dashboard_flag(settings, dashboard_flag=True)
|
||||||
|
assert updated.DASHBOARD_ENABLED is True
|
||||||
|
|
||||||
|
|
||||||
|
def test_start_dashboard_server_disabled_returns_none() -> None:
|
||||||
|
settings = Settings(
|
||||||
|
KIS_APP_KEY="test_key",
|
||||||
|
KIS_APP_SECRET="test_secret",
|
||||||
|
KIS_ACCOUNT_NO="12345678-01",
|
||||||
|
GEMINI_API_KEY="test_gemini_key",
|
||||||
|
DASHBOARD_ENABLED=False,
|
||||||
|
)
|
||||||
|
thread = _start_dashboard_server(settings)
|
||||||
|
assert thread is None
|
||||||
|
|
||||||
|
|
||||||
|
def test_start_dashboard_server_enabled_starts_thread() -> None:
|
||||||
|
settings = Settings(
|
||||||
|
KIS_APP_KEY="test_key",
|
||||||
|
KIS_APP_SECRET="test_secret",
|
||||||
|
KIS_ACCOUNT_NO="12345678-01",
|
||||||
|
GEMINI_API_KEY="test_gemini_key",
|
||||||
|
DASHBOARD_ENABLED=True,
|
||||||
|
)
|
||||||
|
mock_thread = MagicMock()
|
||||||
|
with patch("src.main.threading.Thread", return_value=mock_thread) as mock_thread_cls:
|
||||||
|
thread = _start_dashboard_server(settings)
|
||||||
|
|
||||||
|
assert thread == mock_thread
|
||||||
|
mock_thread_cls.assert_called_once()
|
||||||
|
mock_thread.start.assert_called_once()
|
||||||
|
|||||||
@@ -9,6 +9,7 @@ from unittest.mock import AsyncMock, MagicMock
|
|||||||
import pytest
|
import pytest
|
||||||
|
|
||||||
from src.analysis.smart_scanner import ScanCandidate
|
from src.analysis.smart_scanner import ScanCandidate
|
||||||
|
from src.brain.context_selector import DecisionType
|
||||||
from src.brain.gemini_client import TradeDecision
|
from src.brain.gemini_client import TradeDecision
|
||||||
from src.config import Settings
|
from src.config import Settings
|
||||||
from src.context.store import ContextLayer
|
from src.context.store import ContextLayer
|
||||||
@@ -16,12 +17,10 @@ from src.strategy.models import (
|
|||||||
CrossMarketContext,
|
CrossMarketContext,
|
||||||
DayPlaybook,
|
DayPlaybook,
|
||||||
MarketOutlook,
|
MarketOutlook,
|
||||||
PlaybookStatus,
|
|
||||||
ScenarioAction,
|
ScenarioAction,
|
||||||
)
|
)
|
||||||
from src.strategy.pre_market_planner import PreMarketPlanner
|
from src.strategy.pre_market_planner import PreMarketPlanner
|
||||||
|
|
||||||
|
|
||||||
# ---------------------------------------------------------------------------
|
# ---------------------------------------------------------------------------
|
||||||
# Fixtures
|
# Fixtures
|
||||||
# ---------------------------------------------------------------------------
|
# ---------------------------------------------------------------------------
|
||||||
@@ -89,6 +88,7 @@ def _make_planner(
|
|||||||
token_count: int = 200,
|
token_count: int = 200,
|
||||||
context_data: dict | None = None,
|
context_data: dict | None = None,
|
||||||
scorecard_data: dict | None = None,
|
scorecard_data: dict | None = None,
|
||||||
|
scorecard_map: dict[tuple[str, str, str], dict | None] | None = None,
|
||||||
) -> PreMarketPlanner:
|
) -> PreMarketPlanner:
|
||||||
"""Create a PreMarketPlanner with mocked dependencies."""
|
"""Create a PreMarketPlanner with mocked dependencies."""
|
||||||
if not gemini_response:
|
if not gemini_response:
|
||||||
@@ -107,11 +107,20 @@ def _make_planner(
|
|||||||
|
|
||||||
# Mock ContextStore
|
# Mock ContextStore
|
||||||
store = MagicMock()
|
store = MagicMock()
|
||||||
store.get_context = MagicMock(return_value=scorecard_data)
|
if scorecard_map is not None:
|
||||||
|
store.get_context = MagicMock(
|
||||||
|
side_effect=lambda layer, timeframe, key: scorecard_map.get(
|
||||||
|
(layer.value if hasattr(layer, "value") else layer, timeframe, key)
|
||||||
|
)
|
||||||
|
)
|
||||||
|
else:
|
||||||
|
store.get_context = MagicMock(return_value=scorecard_data)
|
||||||
|
|
||||||
# Mock ContextSelector
|
# Mock ContextSelector
|
||||||
selector = MagicMock()
|
selector = MagicMock()
|
||||||
selector.select_layers = MagicMock(return_value=[ContextLayer.L7_REALTIME, ContextLayer.L6_DAILY])
|
selector.select_layers = MagicMock(
|
||||||
|
return_value=[ContextLayer.L7_REALTIME, ContextLayer.L6_DAILY]
|
||||||
|
)
|
||||||
selector.get_context_data = MagicMock(return_value=context_data or {})
|
selector.get_context_data = MagicMock(return_value=context_data or {})
|
||||||
|
|
||||||
settings = Settings(
|
settings = Settings(
|
||||||
@@ -220,11 +229,25 @@ class TestGeneratePlaybook:
|
|||||||
stocks = [
|
stocks = [
|
||||||
{
|
{
|
||||||
"stock_code": "005930",
|
"stock_code": "005930",
|
||||||
"scenarios": [{"condition": {"rsi_below": 30}, "action": "BUY", "confidence": 85, "rationale": "ok"}],
|
"scenarios": [
|
||||||
|
{
|
||||||
|
"condition": {"rsi_below": 30},
|
||||||
|
"action": "BUY",
|
||||||
|
"confidence": 85,
|
||||||
|
"rationale": "ok",
|
||||||
|
}
|
||||||
|
],
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
"stock_code": "UNKNOWN",
|
"stock_code": "UNKNOWN",
|
||||||
"scenarios": [{"condition": {"rsi_below": 20}, "action": "BUY", "confidence": 90, "rationale": "bad"}],
|
"scenarios": [
|
||||||
|
{
|
||||||
|
"condition": {"rsi_below": 20},
|
||||||
|
"action": "BUY",
|
||||||
|
"confidence": 90,
|
||||||
|
"rationale": "bad",
|
||||||
|
}
|
||||||
|
],
|
||||||
},
|
},
|
||||||
]
|
]
|
||||||
planner = _make_planner(gemini_response=_gemini_response_json(stocks=stocks))
|
planner = _make_planner(gemini_response=_gemini_response_json(stocks=stocks))
|
||||||
@@ -254,6 +277,43 @@ class TestGeneratePlaybook:
|
|||||||
|
|
||||||
assert pb.token_count == 450
|
assert pb.token_count == 450
|
||||||
|
|
||||||
|
@pytest.mark.asyncio
|
||||||
|
async def test_generate_playbook_uses_strategic_context_selector(self) -> None:
|
||||||
|
planner = _make_planner()
|
||||||
|
candidates = [_candidate()]
|
||||||
|
|
||||||
|
await planner.generate_playbook("KR", candidates, today=date(2026, 2, 8))
|
||||||
|
|
||||||
|
planner._context_selector.select_layers.assert_called_once_with(
|
||||||
|
decision_type=DecisionType.STRATEGIC,
|
||||||
|
include_realtime=True,
|
||||||
|
)
|
||||||
|
planner._context_selector.get_context_data.assert_called_once()
|
||||||
|
|
||||||
|
@pytest.mark.asyncio
|
||||||
|
async def test_generate_playbook_injects_self_and_cross_scorecards(self) -> None:
|
||||||
|
scorecard_map = {
|
||||||
|
(ContextLayer.L6_DAILY.value, "2026-02-07", "scorecard_KR"): {
|
||||||
|
"total_pnl": -1.0,
|
||||||
|
"win_rate": 40,
|
||||||
|
"lessons": ["Tighten entries"],
|
||||||
|
},
|
||||||
|
(ContextLayer.L6_DAILY.value, "2026-02-07", "scorecard_US"): {
|
||||||
|
"total_pnl": 1.5,
|
||||||
|
"win_rate": 62,
|
||||||
|
"index_change_pct": 0.9,
|
||||||
|
"lessons": ["Follow momentum"],
|
||||||
|
},
|
||||||
|
}
|
||||||
|
planner = _make_planner(scorecard_map=scorecard_map)
|
||||||
|
|
||||||
|
await planner.generate_playbook("KR", [_candidate()], today=date(2026, 2, 8))
|
||||||
|
|
||||||
|
call_market_data = planner._gemini.decide.call_args.args[0]
|
||||||
|
prompt = call_market_data["prompt_override"]
|
||||||
|
assert "My Market Previous Day (KR)" in prompt
|
||||||
|
assert "Other Market (US)" in prompt
|
||||||
|
|
||||||
|
|
||||||
# ---------------------------------------------------------------------------
|
# ---------------------------------------------------------------------------
|
||||||
# _parse_response
|
# _parse_response
|
||||||
@@ -402,7 +462,12 @@ class TestParseResponse:
|
|||||||
|
|
||||||
class TestBuildCrossMarketContext:
|
class TestBuildCrossMarketContext:
|
||||||
def test_kr_reads_us_scorecard(self) -> None:
|
def test_kr_reads_us_scorecard(self) -> None:
|
||||||
scorecard = {"total_pnl": 2.5, "win_rate": 65, "index_change_pct": 0.8, "lessons": ["Stay patient"]}
|
scorecard = {
|
||||||
|
"total_pnl": 2.5,
|
||||||
|
"win_rate": 65,
|
||||||
|
"index_change_pct": 0.8,
|
||||||
|
"lessons": ["Stay patient"],
|
||||||
|
}
|
||||||
planner = _make_planner(scorecard_data=scorecard)
|
planner = _make_planner(scorecard_data=scorecard)
|
||||||
|
|
||||||
ctx = planner.build_cross_market_context("KR", today=date(2026, 2, 8))
|
ctx = planner.build_cross_market_context("KR", today=date(2026, 2, 8))
|
||||||
@@ -415,8 +480,9 @@ class TestBuildCrossMarketContext:
|
|||||||
|
|
||||||
# Verify it queried scorecard_US
|
# Verify it queried scorecard_US
|
||||||
planner._context_store.get_context.assert_called_once_with(
|
planner._context_store.get_context.assert_called_once_with(
|
||||||
ContextLayer.L6_DAILY, "2026-02-08", "scorecard_US"
|
ContextLayer.L6_DAILY, "2026-02-07", "scorecard_US"
|
||||||
)
|
)
|
||||||
|
assert ctx.date == "2026-02-07"
|
||||||
|
|
||||||
def test_us_reads_kr_scorecard(self) -> None:
|
def test_us_reads_kr_scorecard(self) -> None:
|
||||||
scorecard = {"total_pnl": -1.0, "win_rate": 40, "index_change_pct": -0.5}
|
scorecard = {"total_pnl": -1.0, "win_rate": 40, "index_change_pct": -0.5}
|
||||||
@@ -447,6 +513,32 @@ class TestBuildCrossMarketContext:
|
|||||||
assert ctx is None
|
assert ctx is None
|
||||||
|
|
||||||
|
|
||||||
|
# ---------------------------------------------------------------------------
|
||||||
|
# build_self_market_scorecard
|
||||||
|
# ---------------------------------------------------------------------------
|
||||||
|
|
||||||
|
|
||||||
|
class TestBuildSelfMarketScorecard:
|
||||||
|
def test_reads_previous_day_scorecard(self) -> None:
|
||||||
|
scorecard = {"total_pnl": -1.2, "win_rate": 45, "lessons": ["Reduce overtrading"]}
|
||||||
|
planner = _make_planner(scorecard_data=scorecard)
|
||||||
|
|
||||||
|
data = planner.build_self_market_scorecard("KR", today=date(2026, 2, 8))
|
||||||
|
|
||||||
|
assert data is not None
|
||||||
|
assert data["date"] == "2026-02-07"
|
||||||
|
assert data["total_pnl"] == -1.2
|
||||||
|
assert data["win_rate"] == 45
|
||||||
|
assert "Reduce overtrading" in data["lessons"]
|
||||||
|
planner._context_store.get_context.assert_called_once_with(
|
||||||
|
ContextLayer.L6_DAILY, "2026-02-07", "scorecard_KR"
|
||||||
|
)
|
||||||
|
|
||||||
|
def test_missing_scorecard_returns_none(self) -> None:
|
||||||
|
planner = _make_planner(scorecard_data=None)
|
||||||
|
assert planner.build_self_market_scorecard("US", today=date(2026, 2, 8)) is None
|
||||||
|
|
||||||
|
|
||||||
# ---------------------------------------------------------------------------
|
# ---------------------------------------------------------------------------
|
||||||
# _build_prompt
|
# _build_prompt
|
||||||
# ---------------------------------------------------------------------------
|
# ---------------------------------------------------------------------------
|
||||||
@@ -457,7 +549,7 @@ class TestBuildPrompt:
|
|||||||
planner = _make_planner()
|
planner = _make_planner()
|
||||||
candidates = [_candidate(code="005930", name="Samsung")]
|
candidates = [_candidate(code="005930", name="Samsung")]
|
||||||
|
|
||||||
prompt = planner._build_prompt("KR", candidates, {}, None)
|
prompt = planner._build_prompt("KR", candidates, {}, None, None)
|
||||||
|
|
||||||
assert "005930" in prompt
|
assert "005930" in prompt
|
||||||
assert "Samsung" in prompt
|
assert "Samsung" in prompt
|
||||||
@@ -471,7 +563,7 @@ class TestBuildPrompt:
|
|||||||
win_rate=60, index_change_pct=0.8, lessons=["Cut losses early"],
|
win_rate=60, index_change_pct=0.8, lessons=["Cut losses early"],
|
||||||
)
|
)
|
||||||
|
|
||||||
prompt = planner._build_prompt("KR", [_candidate()], {}, cross)
|
prompt = planner._build_prompt("KR", [_candidate()], {}, None, cross)
|
||||||
|
|
||||||
assert "Other Market (US)" in prompt
|
assert "Other Market (US)" in prompt
|
||||||
assert "+1.50%" in prompt
|
assert "+1.50%" in prompt
|
||||||
@@ -481,7 +573,7 @@ class TestBuildPrompt:
|
|||||||
planner = _make_planner()
|
planner = _make_planner()
|
||||||
context = {"L6_DAILY": {"win_rate": 0.65, "total_pnl": 2.5}}
|
context = {"L6_DAILY": {"win_rate": 0.65, "total_pnl": 2.5}}
|
||||||
|
|
||||||
prompt = planner._build_prompt("KR", [_candidate()], context, None)
|
prompt = planner._build_prompt("KR", [_candidate()], context, None, None)
|
||||||
|
|
||||||
assert "Strategic Context" in prompt
|
assert "Strategic Context" in prompt
|
||||||
assert "L6_DAILY" in prompt
|
assert "L6_DAILY" in prompt
|
||||||
@@ -489,15 +581,30 @@ class TestBuildPrompt:
|
|||||||
|
|
||||||
def test_prompt_contains_max_scenarios(self) -> None:
|
def test_prompt_contains_max_scenarios(self) -> None:
|
||||||
planner = _make_planner()
|
planner = _make_planner()
|
||||||
prompt = planner._build_prompt("KR", [_candidate()], {}, None)
|
prompt = planner._build_prompt("KR", [_candidate()], {}, None, None)
|
||||||
|
|
||||||
assert f"Max {planner._settings.MAX_SCENARIOS_PER_STOCK} scenarios" in prompt
|
assert f"Max {planner._settings.MAX_SCENARIOS_PER_STOCK} scenarios" in prompt
|
||||||
|
|
||||||
def test_prompt_market_name(self) -> None:
|
def test_prompt_market_name(self) -> None:
|
||||||
planner = _make_planner()
|
planner = _make_planner()
|
||||||
prompt = planner._build_prompt("US", [_candidate()], {}, None)
|
prompt = planner._build_prompt("US", [_candidate()], {}, None, None)
|
||||||
assert "US market" in prompt
|
assert "US market" in prompt
|
||||||
|
|
||||||
|
def test_prompt_contains_self_market_scorecard(self) -> None:
|
||||||
|
planner = _make_planner()
|
||||||
|
self_scorecard = {
|
||||||
|
"date": "2026-02-07",
|
||||||
|
"total_pnl": -0.8,
|
||||||
|
"win_rate": 45.0,
|
||||||
|
"lessons": ["Avoid midday entries"],
|
||||||
|
}
|
||||||
|
prompt = planner._build_prompt("KR", [_candidate()], {}, self_scorecard, None)
|
||||||
|
|
||||||
|
assert "My Market Previous Day (KR)" in prompt
|
||||||
|
assert "2026-02-07" in prompt
|
||||||
|
assert "-0.80%" in prompt
|
||||||
|
assert "Avoid midday entries" in prompt
|
||||||
|
|
||||||
|
|
||||||
# ---------------------------------------------------------------------------
|
# ---------------------------------------------------------------------------
|
||||||
# _extract_json
|
# _extract_json
|
||||||
|
|||||||
Reference in New Issue
Block a user