Skip to content

Commit fca0c05

Browse files
committed
Adjust sellable quantity check to use ord_psbl_qty from KIS account; add tests for overseas sell scenarios
- Ensure `ord_psbl_qty` is prioritized over `balance_qty` to avoid overestimating sellable quantities. - Add new tests to handle scenarios with zero sellable quantities, pending orders, split orders, and missing stocks in KIS accounts.
1 parent b2a9e1d commit fca0c05

File tree

2 files changed

+394
-0
lines changed

2 files changed

+394
-0
lines changed

app/services/kis_trading_service.py

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -392,6 +392,19 @@ async def process_kis_overseas_sell_orders_with_analysis(
392392
settings = await settings_service.get_by_symbol(symbol)
393393
actual_exchange_code = (settings.exchange_code if settings and settings.exchange_code else exchange_code)
394394

395+
# KIS 계좌의 실제 주문가능수량 조회 (토스 등 수동 잔고 제외)
396+
my_stocks = await kis_client.fetch_my_overseas_stocks()
397+
target_stock = next((s for s in my_stocks if s.get('ovrs_pdno') == symbol), None)
398+
if target_stock:
399+
# ord_psbl_qty가 있으면 사용, 없으면 ovrs_cblc_qty 사용
400+
actual_qty = int(float(target_stock.get('ord_psbl_qty', target_stock.get('ovrs_cblc_qty', 0))))
401+
if actual_qty < balance_qty:
402+
logger.info(f"[{symbol}] 주문가능수량 조정: {balance_qty} -> {actual_qty} (KIS 계좌 기준)")
403+
balance_qty = actual_qty
404+
405+
if balance_qty <= 0:
406+
return {'success': False, 'message': "주문가능수량 없음", 'orders_placed': 0}
407+
395408
sell_prices = []
396409
if analysis.appropriate_sell_min:
397410
sell_prices.append(analysis.appropriate_sell_min)

0 commit comments

Comments
 (0)