feat: 시작 시 브로커 포지션 → DB 동기화 및 국내주식 이중 매수 방지 (#206) #228
Reference in New Issue
Block a user
Delete Branch "feature/issue-206-startup-position-sync"
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?
변경 내용
시스템 시작 시 실제 브로커 잔고를 로컬 DB와 동기화하는 루틴을 추가합니다.
아울러 국내주식 BUY 이중 방지 로직이 해외주식에만 적용되던 버그도 수정합니다.
문제 (issue #206)
해결
1. sync_positions_from_broker() 함수 추가 (src/main.py)
2. 국내주식 이중 매수 방지 로직 확장
기존 (해외 전용):
수정 후 (국내/해외 공통):
trading_cycle 및 run_daily_session 양쪽 모두 적용.
테스트 결과
827 passed, 4 warnings
추가된 테스트
Closes #206
- sync_positions_from_broker() 함수 추가 - 시스템 시작 시 브로커 잔고를 조회해 DB에 없는 포지션을 BUY 레코드로 삽입 - 국내: get_balance(), 해외: get_overseas_balance(exchange_code) 순회 - ConnectionError는 경고 로그만 남기고 계속 진행 (non-fatal) - 동일 exchange_code 중복 조회 방지 (seen_exchange_codes 집합) - run() 초기화 후 최초 한 번 자동 호출 - 국내주식 BUY 이중 방지 로직 확장 - trading_cycle 및 run_daily_session에서 기존에 해외 전용(not market.is_domestic) 으로만 적용하던 broker balance 체크를 국내/해외 공통으로 변경 - _extract_held_qty_from_balance(is_domestic=market.is_domestic) - 테스트 (827 passed) - TestSyncPositionsFromBroker (6개): 국내/해외 동기화, 중복 skip, 공란, ConnectionError, dedup - TestDomesticBuyDoublePreventionTradingCycle (1개): 국내 보유 주식 BUY 억제 Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>d01f4d93a3toff9c4d6082