fix: use actual held quantity for SELL orders instead of hardcoded 1 (#164) #167

Closed
agentson wants to merge 1 commits from feature/issue-164-sell-quantity-fix into main
Collaborator

Summary

  • _determine_order_quantity()에서 SELL 시 항상 1을 반환하던 버그 수정
  • 실제 DB 보유 수량을 참조해 전량 청산이 가능하도록 변경
  • trading_cycle, run_daily_session 두 호출 지점 모두 수정

변경 내용

# Before: SELL이면 무조건 1
if action != "BUY":
    return 1

# After: 실제 보유 수량 사용
if action == "SELL":
    if open_position is None:
        return 0
    return int(open_position.get("quantity") or 0)

호출 지점 변경

# trading_cycle / run_daily_session 모두 동일하게 적용
sell_position = (
    get_open_position(db_conn, stock_code, market.code)
    if decision.action == "SELL"
    else None
)
quantity = _determine_order_quantity(
    ...
    open_position=sell_position,
)

Test plan

  • TestDetermineOrderQuantity::test_sell_returns_position_quantity — quantity=7 반환
  • TestDetermineOrderQuantity::test_sell_without_position_returns_zero — 포지션 없으면 0
  • TestDetermineOrderQuantity::test_sell_with_zero_quantity_returns_zero — quantity=0이면 0
  • BUY 관련 3개 기존 동작 유지 검증
  • test_sell_order_uses_actual_held_quantity — 5주 보유 → SELL quantity=5 확인
  • 677 tests passed

Closes #164

🤖 Generated with Claude Code

## Summary - `_determine_order_quantity()`에서 SELL 시 항상 1을 반환하던 버그 수정 - 실제 DB 보유 수량을 참조해 전량 청산이 가능하도록 변경 - `trading_cycle`, `run_daily_session` 두 호출 지점 모두 수정 ## 변경 내용 ```python # Before: SELL이면 무조건 1 if action != "BUY": return 1 # After: 실제 보유 수량 사용 if action == "SELL": if open_position is None: return 0 return int(open_position.get("quantity") or 0) ``` ## 호출 지점 변경 ```python # trading_cycle / run_daily_session 모두 동일하게 적용 sell_position = ( get_open_position(db_conn, stock_code, market.code) if decision.action == "SELL" else None ) quantity = _determine_order_quantity( ... open_position=sell_position, ) ``` ## Test plan - [x] `TestDetermineOrderQuantity::test_sell_returns_position_quantity` — quantity=7 반환 - [x] `TestDetermineOrderQuantity::test_sell_without_position_returns_zero` — 포지션 없으면 0 - [x] `TestDetermineOrderQuantity::test_sell_with_zero_quantity_returns_zero` — quantity=0이면 0 - [x] BUY 관련 3개 기존 동작 유지 검증 - [x] `test_sell_order_uses_actual_held_quantity` — 5주 보유 → SELL quantity=5 확인 - [x] 677 tests passed Closes #164 🤖 Generated with [Claude Code](https://claude.com/claude-code)
agentson added 1 commit 2026-02-20 03:04:04 +09:00
fix: use actual held quantity for SELL orders instead of hardcoded 1 (#164)
Some checks failed
CI / test (pull_request) Has been cancelled
1adb85926d
_determine_order_quantity()에서 SELL 시 항상 1을 반환하던 버그 수정.
DB에서 실제 보유 수량을 조회해 전량 청산이 가능하도록 변경.

- _determine_order_quantity에 open_position 파라미터 추가
- SELL 시 open_position["quantity"] 반환, 포지션 없으면 0 반환
- trading_cycle 및 run_daily_session 호출 지점 모두 수정
- _determine_order_quantity 임포트 및 유닛 테스트 클래스 추가 (6개)
- SELL 실제 수량 사용 통합 테스트 추가 (quantity=5 검증)

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
Owner

db만 확인해도 될까? buy할때 주문량과 체결량이 다를 수 있는데, 그 부분도 고려해서 db에 기록하고 있나?

db만 확인해도 될까? buy할때 주문량과 체결량이 다를 수 있는데, 그 부분도 고려해서 db에 기록하고 있나?
agentson closed this pull request 2026-02-20 07:40:55 +09:00
Some checks failed
CI / test (pull_request) Has been cancelled

Pull request closed

Sign in to join this conversation.
No Reviewers
2 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: jihoson/The-Ouroboros#167