fix: domestic current price fetching and KRX tick unit rounding (#157) #158

Merged
jihoson merged 1 commits from feature/issue-157-fix-domestic-price-and-tick into main 2026-02-19 16:25:59 +09:00
Collaborator

Summary

  • 현재가 0 문제: get_orderbook()output1.stck_prpr 필드 없음 → get_current_price() 메서드 추가 (inquire-price API 사용)
  • 호가단위 오류: 주문 전 가격 미정렬 → kr_round_down() 함수 추가 + send_order() 내 적용
  • ORD_DVSN 오류: "01" (시장가)로 지정가 주문 전송 → "00" (지정가)로 수정

Changes

src/broker/kis_api.py

  • kr_tick_unit(price) — KRX 7단계 호가단위 반환 함수 추가
  • kr_round_down(price) — 호가단위 내림 함수 추가
  • get_current_price(stock_code)FHKST01010100 / inquire-price API 사용, (price, change_pct, foreigner_net) 반환 (VTS에서 정상 작동 확인)
  • send_order() — ORD_DVSN 수정 ("00"=지정가, "01"=시장가) + 지정가 주문 시 kr_round_down() 적용

src/main.py

  • trading_cycle() (realtime mode): get_orderbookget_current_price
  • run_daily_session() (daily mode): get_orderbookget_current_price

tests/test_broker.py

  • TestKrTickUnit: 13개 경계값 + 7개 내림 케이스 (parametrized)
  • TestGetCurrentPrice: 올바른 필드 반환, API 경로/TR_ID, HTTP 오류 처리
  • TestSendOrderTickRounding: 호가단위 내림, ORD_DVSN 00/01

tests/test_main.py

  • 4곳의 broker.get_orderbook mock → broker.get_current_price AsyncMock으로 교체

Test Results

646 passed, 4 warnings. 25개 신규 테스트 추가.

Verification

VTS 직접 테스트:

  • 188,150원 → kr_round_down 적용 → 188,100원 → 주문 성공 (rt_cd=0)
  • 적용 전: 호가단위 오류 / 적용 후: 모의투자 매수주문이 완료 되었습니다.

Closes #157

## Summary - **현재가 0 문제**: `get_orderbook()`의 `output1.stck_prpr` 필드 없음 → `get_current_price()` 메서드 추가 (inquire-price API 사용) - **호가단위 오류**: 주문 전 가격 미정렬 → `kr_round_down()` 함수 추가 + `send_order()` 내 적용 - **ORD_DVSN 오류**: `"01"` (시장가)로 지정가 주문 전송 → `"00"` (지정가)로 수정 ## Changes ### `src/broker/kis_api.py` - `kr_tick_unit(price)` — KRX 7단계 호가단위 반환 함수 추가 - `kr_round_down(price)` — 호가단위 내림 함수 추가 - `get_current_price(stock_code)` — `FHKST01010100` / `inquire-price` API 사용, `(price, change_pct, foreigner_net)` 반환 (VTS에서 정상 작동 확인) - `send_order()` — ORD_DVSN 수정 (`"00"`=지정가, `"01"`=시장가) + 지정가 주문 시 `kr_round_down()` 적용 ### `src/main.py` - `trading_cycle()` (realtime mode): `get_orderbook` → `get_current_price` - `run_daily_session()` (daily mode): `get_orderbook` → `get_current_price` ### `tests/test_broker.py` - `TestKrTickUnit`: 13개 경계값 + 7개 내림 케이스 (parametrized) - `TestGetCurrentPrice`: 올바른 필드 반환, API 경로/TR_ID, HTTP 오류 처리 - `TestSendOrderTickRounding`: 호가단위 내림, ORD_DVSN 00/01 ### `tests/test_main.py` - 4곳의 `broker.get_orderbook` mock → `broker.get_current_price` AsyncMock으로 교체 ## Test Results 646 passed, 4 warnings. 25개 신규 테스트 추가. ## Verification VTS 직접 테스트: - 188,150원 → kr_round_down 적용 → 188,100원 → 주문 성공 (rt_cd=0) - 적용 전: 호가단위 오류 / 적용 후: 모의투자 매수주문이 완료 되었습니다. Closes #157
agentson added 1 commit 2026-02-19 12:41:28 +09:00
fix: domestic current price fetching and KRX tick unit rounding (#157)
Some checks failed
CI / test (pull_request) Has been cancelled
7834b89f10
**Problem 1 — Current price always 0**
get_orderbook() used inquire-asking-price-exp-ccn which has no stck_prpr
in output1 (only askp/bidp data). This caused every domestic BUY to be
skipped with "no affordable quantity (cash=..., price=0.00)".

**Problem 2 — KRX tick unit error on limit orders**
Limit order prices were passed unrounded, triggering 호가단위 오류 in VTS.
Also ORD_DVSN was wrongly set to "01" (시장가) for limit orders.

**Fix**
- Add kr_tick_unit(price) and kr_round_down(price) module-level helpers
  implementing KRX 7-tier price tick rules (1/5/10/50/100/500/1000원).
- Add get_current_price(stock_code) → (price, change_pct, foreigner_net)
  using FHKST01010100 / inquire-price API (works in VTS, returns correct
  stck_prpr, prdy_ctrt, frgn_ntby_qty).
- Fix send_order() ORD_DVSN: "00"=지정가, "01"=시장가 (was "01"/"06").
- Apply kr_round_down() to limit order price inside send_order().
- Replace both get_orderbook() calls in main.py with get_current_price().
- Update all 4 test_main.py mock sites to use get_current_price AsyncMock.

**Tests added** (25 new tests, all 646 pass)
- TestKrTickUnit: 13 parametrized boundary cases + 7 round-down cases
- TestGetCurrentPrice: correct fields, correct API path/TR_ID, HTTP error
- TestSendOrderTickRounding: tick rounding, ORD_DVSN 00/01

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
jihoson merged commit ce5773ba45 into main 2026-02-19 16:25:59 +09:00
Sign in to join this conversation.
No Reviewers
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: jihoson/The-Ouroboros#158