2.3 KiB
2.3 KiB
Context Tree: Multi-Layered Memory Management
Summary
컨텍스트 트리는 L7(실시간)부터 L1(레거시)까지 계층화된 메모리 구조입니다.
- L7~L5: 시장별 독립 데이터 중심
- L4~L1: 글로벌 포트폴리오 통합 데이터
Layer Policy
L7_REALTIME (시장+종목 스코프)
- 주요 키 패턴:
volatility_{market}_{stock_code}price_{market}_{stock_code}rsi_{market}_{stock_code}volume_ratio_{market}_{stock_code}
trading_cycle()에서 실시간으로 기록합니다.
L6_DAILY (시장 스코프)
EOD 집계 결과를 시장별 키로 저장합니다.
trade_count_KR,buys_KR,sells_KR,holds_KRavg_confidence_US,total_pnl_US,win_rate_US- scorecard 저장 키:
scorecard_KR,scorecard_US
L5_WEEKLY
L6 일일 데이터에서 시장별 주간 합계를 생성합니다.
weekly_pnl_KR,weekly_pnl_USavg_confidence_KR,avg_confidence_US
L4_MONTHLY 이상
글로벌 통합 롤업입니다.
- L5 → L4:
monthly_pnl - L4 → L3:
quarterly_pnl - L3 → L2:
annual_pnl - L2 → L1:
total_pnl,years_traded,avg_annual_pnl
Aggregation Flow
- EOD:
ContextAggregator.aggregate_daily_from_trades(date, market) - 주기 롤업:
ContextScheduler.run_if_due()
ContextScheduler는 다음을 처리합니다.
- weekly/monthly/quarterly/annual/legacy 집계
- 일 1회
ContextStore.cleanup_expired_contexts()실행 - 동일 날짜 중복 실행 방지(
_last_run)
Usage
from datetime import UTC, datetime
from src.context.aggregator import ContextAggregator
from src.context.scheduler import ContextScheduler
aggregator = ContextAggregator(conn)
scheduler = ContextScheduler(aggregator=aggregator, store=context_store)
# EOD market-scoped daily aggregation
aggregator.aggregate_daily_from_trades(date="2026-02-16", market="KR")
# Run scheduled rollups when due
scheduler.run_if_due(now=datetime.now(UTC))
Retention
src/context/layer.py 기준:
- L1: Forever
- L2: 10 years
- L3: 3 years
- L4: 2 years
- L5: 1 year
- L6: 90 days
- L7: 7 days
Current Notes (2026-02-16)
- L7 쓰기와 L6 시장별 집계는
main.py에 연결됨 - scheduler 기반 cleanup/rollup도 연결됨
- cross-market scorecard 조회는
PreMarketPlanner에서 사용 중