Skip to content

refactor: engine.py God Object 분해 (C2) + 선물/스캘핑 시스템#7

Open
nunionda wants to merge 6 commits into
mainfrom
feat/esf-live-trading
Open

refactor: engine.py God Object 분해 (C2) + 선물/스캘핑 시스템#7
nunionda wants to merge 6 commits into
mainfrom
feat/esf-live-trading

Conversation

@nunionda
Copy link
Copy Markdown
Owner

Summary

  • C2 Engine Decomposition: simulation/engine.py 6,689줄 → 1,942줄 (71% 감소)
    • 16개 모듈로 분해: constants, models, allocator, regime, risk_gates, indicators, sizing + 7개 전략 모듈
    • Strategy registry 패턴으로 디스패처 단순화
    • 58/58 테스트 전체 통과, 동작 변경 없음
  • ES-F 인트라데이 스캘핑 시스템 구현 (AMT 3-Stage + Triple-A Model)
  • S&P500 선물 매매 시스템 고도화 (증거금/CB/롤오버/상품규격)
  • 선물 백테스트 + Monte Carlo 시뮬레이션 완전 구현
  • 코드 품질 개선: critical bugs 수정, 브라우저 캐시 최적화

Engine Decomposition 상세

모듈 용도 Lines
constants.py 상수/설정 딕셔너리 350
models.py Pydantic 데이터 모델 140
allocator.py 멀티전략 자본배분 340
regime.py 마켓 레짐 판정 383
risk_gates.py RG1-RG5 리스크 게이트 171
indicators.py 기술 지표 계산 205
sizing.py VIX 사이징 배율 36
strategies/momentum.py 모멘텀 스윙 전략 483
strategies/smc.py SMC 4-Layer 전략 403
strategies/mean_reversion.py 평균회귀 전략 499
strategies/breakout_retest.py 돌파-리테스트 전략 704
strategies/arbitrage.py 페어 아비트라지 1,041
strategies/defensive.py 방어 전략 158
strategies/volatility.py 변동성 프리미엄 154

Test plan

  • python3 run_tests.py — 58/58 테스트 통과
  • 모든 추출 단계에서 테스트 통과 확인 (Phase A → B → C → D)
  • 전략 레지스트리 디스패처 동작 확인
  • 백테스트 결과 파리티 검증 (수동)

🤖 Generated with Claude Code

신대승 and others added 6 commits March 15, 2026 17:05
4-phase plan to reduce engine.py from 6689 to ~1900 lines:
- Phase A: constants, models, allocator extraction (zero risk)
- Phase B: regime, risk gates, indicators, sizing
- Phase C: 7 strategy modules to simulation/strategies/
- Phase D: dispatcher cleanup with strategy registry

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
…se A)

Phase A of engine.py decomposition - zero-risk extractions:
- constants.py: regime params, strategy weights, ETF universes (~345 lines)
- models.py: 8 Pydantic models (SimPosition, SimSignal, etc.) (~130 lines)
- allocator.py: StrategyAllocator class + _compute_adx helper (~330 lines)

All 58 tests pass. No behavioral changes.

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
…e B)

Phase B domain logic extraction:
- regime.py: market regime, index trend, stock regime classification (~300 lines)
- risk_gates.py: RG1-RG5, bearish divergence, S/R detection (~170 lines)

All 58 tests pass. No behavioral changes.

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
Phase B of C2 decomposition — domain logic extraction:
- regime.py: market regime detection, index trend analysis, stock regime classification
- risk_gates.py: RG1-RG5 checks, bearish divergence, S/R detection
- indicators.py: MA/RSI/MACD/BB/ATR/ADX calculations, trend confirmation, stage estimation
- sizing.py: VIX-based sizing multiplier
All replaced with thin delegating wrappers in engine.py. 58/58 tests pass.

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
Phase C of C2 decomposition — strategy extraction:
- strategies/momentum.py: 6-Phase pipeline scan + 7-tier exit cascade
- strategies/smc.py: SMC 4-Layer scoring + CHoCH exits
- strategies/mean_reversion.py: MR indicators, scoring, scan + exits
- strategies/breakout_retest.py: BRT 4-Layer + retest zone scoring
- strategies/arbitrage.py: pair discovery, basis gate, spread scoring
- strategies/defensive.py: safe-haven/inverse ETF rotation
- strategies/volatility.py: VIX premium capture
Engine.py reduced from 5208 to 2159 lines. 58/58 tests pass.

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
Phase D cleanup — strategy dispatch simplification:
- Add _STRATEGY_MODULES registry mapping strategy names to modules
- Replace if/elif chains in _scan_entries, _check_exits, _scan_entries_multi,
  _check_exits_multi with single registry lookups
- Remove 185 lines of dead delegating wrapper methods
Engine.py: 2159 → 1942 lines. 58/58 tests pass.

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant