Skip to content

Commit e03dde7

Browse files
committed
Log KIS API responses and handle zero current price fallback
- Add debug logging of full KIS API responses for better traceability. - Introduce fallback to `base` price when `last` (current price) is zero; log warnings accordingly. - Update overseas stock price handling logic to use corrected current price values. - Add `repro_kis_price.py` script for debugging and testing KIS API price retrieval.
1 parent 6526496 commit e03dde7

File tree

2 files changed

+47
-1
lines changed

2 files changed

+47
-1
lines changed

app/services/kis.py

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -985,6 +985,9 @@ async def inquire_overseas_price(
985985
if not out:
986986
return pd.DataFrame(columns=["code", "date", "time", "open", "high", "low", "close", "volume"])
987987

988+
# 디버깅을 위해 전체 응답 로깅
989+
logging.debug(f"해외주식 현재가 API 응답 ({symbol}): {out}")
990+
988991
# 현재 시간 정보
989992
now = datetime.datetime.now()
990993

@@ -1005,14 +1008,24 @@ def safe_int(val, default=0):
10051008
except (ValueError, TypeError):
10061009
return default
10071010

1011+
# 현재가(last)가 0이면 전일종가(base)를 사용
1012+
current_price = safe_float(out.get("last"))
1013+
if current_price == 0:
1014+
base_price = safe_float(out.get("base"))
1015+
if base_price > 0:
1016+
logging.warning(f"현재가가 0이어서 전일종가를 사용합니다 ({symbol}): {base_price}")
1017+
current_price = base_price
1018+
else:
1019+
logging.warning(f"현재가와 전일종가 모두 0입니다 ({symbol})")
1020+
10081021
row = {
10091022
"code": symbol,
10101023
"date": pd.Timestamp(now.date()), # Timestamp로 변환하여 일봉 데이터와 타입 일치
10111024
"time": now.time(),
10121025
"open": safe_float(out.get("open")),
10131026
"high": safe_float(out.get("high")),
10141027
"low": safe_float(out.get("low")),
1015-
"close": safe_float(out.get("last")), # 현재가
1028+
"close": current_price, # 수정된 현재가
10161029
"volume": safe_int(out.get("tvol")),
10171030
"value": 0, # 해외주식은 거래대금 정보 없음
10181031
}

repro_kis_price.py

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
import asyncio
2+
import logging
3+
import sys
4+
import os
5+
6+
# Add project root to path
7+
sys.path.append(os.getcwd())
8+
9+
from app.services.kis import KISClient
10+
from app.core.config import settings
11+
12+
# Configure logging
13+
logging.basicConfig(level=logging.INFO)
14+
15+
async def main():
16+
kis = KISClient()
17+
symbol = "CONY"
18+
19+
print(f"Fetching price for {symbol}...")
20+
try:
21+
# Call the low-level inquire method to get the DataFrame
22+
df = await kis.inquire_overseas_price(symbol)
23+
print("\nDataFrame result:")
24+
print(df)
25+
26+
if not df.empty:
27+
print(f"\nClose price: {df.iloc[0]['close']}")
28+
29+
except Exception as e:
30+
print(f"Error: {e}")
31+
32+
if __name__ == "__main__":
33+
asyncio.run(main())

0 commit comments

Comments
 (0)