trade: apply runtime strategy/fx pnl split on sell paths (#370) #383
Reference in New Issue
Block a user
Delete Branch "feature/issue-370-fx-pnl-runtime-split"
Deleting a branch is permanent. Although the deleted branch may continue to exist for a short time before it actually gets removed, it CANNOT be undone in most cases. Continue?
Summary
trading_cycle/run_daily_sessionSELL 경로에서strategy_pnl/fx_pnl을 런타임 계산해log_trade()에 명시 전달하도록 반영했습니다.selection_context)에 해외fx_rate스냅샷을 남기고, SELL 시 buy/sellfx_rate가 모두 있으면 FX 성분을 분리 계산합니다.get_latest_buy_trade()가 BUY의selection_context를 함께 조회하도록 확장했습니다.strategy_pnl + fx_pnl == pnl무결성을 유지하는 테스트를 보강했습니다.Scope Mapping
REQ-V3-007TASK-CODE-011TEST-ACC-006Validation
python3 -m py_compile src/main.py src/db.pyruff check src/main.py src/db.py tests/test_main.pypytest -q tests/test_main.py -k "sell_order_uses_broker_balance_qty_not_db or split_trade_pnl_components_overseas_fx_split_preserves_total"pytest -q tests/test_db.pypython3 scripts/validate_ouroboros_docs.pypython3 scripts/validate_governance_assets.py origin/feature/v3-session-policy-stream...HEADpython3 scripts/validate_docs_sync.pyPR 리뷰 (Claude Code)
전체 판정: ✅ LGTM
CI 통과, 테스트 179개 전체 통과 확인.
로직 검증
_split_trade_pnl_components계산 정확성테스트 케이스(buy_price=100, sell_price=110, qty=2, buy_fx=1200, sell_fx=1260) 수동 검증:
strategy_pnl + fx_pnl = trade_pnl무결성이strategy_pnl = trade_pnl - fx_pnl정의에 의해 항상 보장됩니다.SELL 경로 흐름 (trading_cycle)
buy_trade = None으로 초기화 → SELL + order_succeeded 시get_latest_buy_trade()호출 →_extract_buy_fx_rate(buy_trade)로 BUY 시 저장된 fx_rate 추출 →_split_trade_pnl_components()호출. BUY-SELL 간 fx_rate 전달 경로가 완결됩니다.사이드 이펙트: 버그 수정 포함
balance_info: dict[str, Any] = {}가trading_cycle과run_daily_session양쪽에 추가되었습니다. 기존에는 overseas 조건 미진입 시 변수가 미정의 상태로 아래 코드에서NameError위험이 있었습니다. 이것은 이 PR의 암묵적 버그 수정입니다.소견
run_daily_sessionBUY 경로의 selection_contexttrading_cycle에서는 BUY 시 scanner 정보(rsi, volume_ratio 등)가 selection_context에 포함된 뒤 fx_rate가 추가되지만,run_daily_session에서는{fx_rate: snapshot_fx_rate}만 저장됩니다. daily 모드에서 real-time scanner가 없으므로 의도된 동작으로 보이나, 두 경로의 selection_context 구조가 다르다는 점은 감사 문서나 주석에 명시하면 좋습니다._extract_fx_rate_from_sources키 탐색 순서7개 키(
frst_bltn_exrt,bass_exrt,ovrs_exrt등)가 API 출처 주석 없이 hardcoded되어 있습니다. best-effort 방식이라 안전하지만, 브로커 API 업데이트 시 유지보수가 어려울 수 있습니다. 블로킹 사항은 아닙니다.추가 리뷰 (Claude Code)
이전 소견 반영 현황
run_daily_sessionBUY 경로 selection_context 구조 차이 명시_extract_fx_rate_from_sources키 탐색 출처 주석 추가판정: ✅ LGTM
두 소견이 모두 반영되었으며, 추가 지적 사항 없습니다.
_extract_fx_rate_from_sources주석이 키 목록의 존재 이유와 단일 수정 지점임을 명확히 설명합니다.run_daily_sessionBUY/SELL 경로에 scanner 컨텍스트 없이 FX 스냅샷만 기록한다는 의도가 주석과 감사 문서 모두에 반영되었습니다.