initial commit
This commit is contained in:
@@ -0,0 +1,99 @@
|
||||
# -*- coding: utf-8 -*-
|
||||
"""
|
||||
Created on 2025-06-17
|
||||
|
||||
"""
|
||||
|
||||
import logging
|
||||
import sys
|
||||
|
||||
import pandas as pd
|
||||
|
||||
sys.path.extend(['../..', '.']) # kis_auth 파일 경로 추가
|
||||
import kis_auth as ka
|
||||
from search_info import search_info
|
||||
|
||||
# 로깅 설정
|
||||
logging.basicConfig(level=logging.INFO, format='%(levelname)s - %(message)s')
|
||||
logger = logging.getLogger(__name__)
|
||||
|
||||
##############################################################################################
|
||||
# [국내주식] 종목정보 > 상품기본조회[v1_국내주식-029]
|
||||
##############################################################################################
|
||||
|
||||
COLUMN_MAPPING = {
|
||||
'pdno': '상품번호',
|
||||
'prdt_type_cd': '상품유형코드',
|
||||
'std_pdno': '표준상품번호',
|
||||
'shtn_pdno': '단축상품번호',
|
||||
'prdt_sale_stat_cd': '상품판매상태코드',
|
||||
'prdt_risk_grad_cd': '상품위험등급코드',
|
||||
'prdt_clsf_cd': '상품분류코드',
|
||||
'sale_strt_dt': '판매시작일자',
|
||||
'sale_end_dt': '판매종료일자',
|
||||
'wrap_asst_type_cd': '랩어카운트자산유형코드',
|
||||
'ivst_prdt_type_cd': '투자상품유형코드',
|
||||
'frst_erlm_dt': '최초등록일자'
|
||||
}
|
||||
|
||||
NUMERIC_COLUMNS = []
|
||||
|
||||
|
||||
def main():
|
||||
"""
|
||||
[국내주식] 종목정보
|
||||
상품기본조회[v1_국내주식-029]
|
||||
|
||||
상품기본조회 테스트 함수
|
||||
|
||||
Parameters:
|
||||
- pdno (str): 상품번호 ('주식(하이닉스) : 000660 (코드 : 300) 선물(101S12) : KR4101SC0009 (코드 : 301) 미국(AAPL) : AAPL (코드 : 512)')
|
||||
- prdt_type_cd (str): 상품유형코드 ('300 주식 301 선물옵션 302 채권 512 미국 나스닥 / 513 미국 뉴욕 / 529 미국 아멕스 515 일본 501 홍콩 / 543 홍콩CNY / 558 홍콩USD 507 베트남 하노이 / 508 베트남 호치민 551 중국 상해A / 552 중국 심천A')
|
||||
Returns:
|
||||
- DataFrame: 상품기본조회 결과
|
||||
|
||||
Example:
|
||||
>>> df = search_info(pdno="000660", prdt_type_cd="300")
|
||||
"""
|
||||
try:
|
||||
# pandas 출력 옵션 설정
|
||||
pd.set_option('display.max_columns', None) # 모든 컬럼 표시
|
||||
pd.set_option('display.width', None) # 출력 너비 제한 해제
|
||||
pd.set_option('display.max_rows', None) # 모든 행 표시
|
||||
|
||||
# 토큰 발급
|
||||
logger.info("토큰 발급 중...")
|
||||
ka.auth()
|
||||
logger.info("토큰 발급 완료")
|
||||
# API 호출
|
||||
result = search_info(
|
||||
pdno="000660", # pdno = "AAPL", # 상품번호
|
||||
prdt_type_cd="300", # prdt_type_cd = "512", # 상품유형코드
|
||||
)
|
||||
|
||||
if result is None or result.empty:
|
||||
logger.warning("조회된 데이터가 없습니다.")
|
||||
return
|
||||
|
||||
# 컬럼명 출력
|
||||
logger.info("사용 가능한 컬럼 목록:")
|
||||
logger.info(result.columns.tolist())
|
||||
|
||||
# 한글 컬럼명으로 변환
|
||||
result = result.rename(columns=COLUMN_MAPPING)
|
||||
|
||||
for col in NUMERIC_COLUMNS:
|
||||
if col in result.columns:
|
||||
result[col] = pd.to_numeric(result[col], errors='coerce').round(2)
|
||||
|
||||
# 결과 출력
|
||||
logger.info("=== 상품기본조회 결과 ===")
|
||||
logger.info("조회된 데이터 건수: %d", len(result))
|
||||
print(result)
|
||||
|
||||
except Exception as e:
|
||||
logger.error("에러 발생: %s", str(e))
|
||||
raise
|
||||
|
||||
if __name__ == "__main__":
|
||||
main()
|
||||
@@ -0,0 +1,121 @@
|
||||
# [국내주식] 종목정보 - 상품기본조회
|
||||
# Generated by KIS API Generator (Single API Mode)
|
||||
# -*- coding: utf-8 -*-
|
||||
"""
|
||||
Created on 2025-06-17
|
||||
|
||||
"""
|
||||
|
||||
import logging
|
||||
import sys
|
||||
import time
|
||||
from typing import Optional
|
||||
|
||||
import pandas as pd
|
||||
|
||||
sys.path.extend(['../..', '.'])
|
||||
import kis_auth as ka
|
||||
|
||||
# 로깅 설정
|
||||
logging.basicConfig(level=logging.INFO, format='%(levelname)s - %(message)s')
|
||||
logger = logging.getLogger(__name__)
|
||||
|
||||
##############################################################################################
|
||||
# [국내주식] 종목정보 > 상품기본조회[v1_국내주식-029]
|
||||
##############################################################################################
|
||||
|
||||
# 상수 정의
|
||||
API_URL = "/uapi/domestic-stock/v1/quotations/search-info"
|
||||
|
||||
def search_info(
|
||||
pdno: str, # 상품번호
|
||||
prdt_type_cd: str, # 상품유형코드
|
||||
tr_cont: str = "", # 연속 거래 여부
|
||||
dataframe: Optional[pd.DataFrame] = None, # 누적 데이터프레임
|
||||
depth: int = 0, # 현재 재귀 깊이
|
||||
max_depth: int = 10 # 최대 재귀 깊이
|
||||
) -> Optional[pd.DataFrame]:
|
||||
"""
|
||||
[국내주식] 종목정보
|
||||
상품기본조회[v1_국내주식-029]
|
||||
상품기본조회 API를 호출하여 DataFrame으로 반환합니다.
|
||||
|
||||
Args:
|
||||
pdno (str): 상품번호 (예: '000660', 'KR4101SC0009', 'AAPL')
|
||||
prdt_type_cd (str): 상품유형코드 (예: '300', '301', '512')
|
||||
tr_cont (str): 연속 거래 여부 (기본값: 공백)
|
||||
dataframe (Optional[pd.DataFrame]): 누적 데이터프레임
|
||||
depth (int): 현재 재귀 깊이
|
||||
max_depth (int): 최대 재귀 깊이 (기본값: 10)
|
||||
|
||||
Returns:
|
||||
Optional[pd.DataFrame]: 상품기본조회 데이터
|
||||
|
||||
Example:
|
||||
>>> df = search_info('AAPL', '512')
|
||||
>>> print(df)
|
||||
"""
|
||||
# 로깅 설정
|
||||
logger = logging.getLogger(__name__)
|
||||
|
||||
# 필수 파라미터 검증
|
||||
if not pdno:
|
||||
logger.error("pdno is required. (e.g. '000660')")
|
||||
raise ValueError("pdno is required. (e.g. '000660')")
|
||||
|
||||
if not prdt_type_cd:
|
||||
logger.error("prdt_type_cd is required. (e.g. '300')")
|
||||
raise ValueError("prdt_type_cd is required. (e.g. '300')")
|
||||
|
||||
# 최대 재귀 깊이 체크
|
||||
if depth >= max_depth:
|
||||
logger.warning("Maximum recursion depth (%d) reached. Stopping further requests.", max_depth)
|
||||
return dataframe if dataframe is not None else pd.DataFrame()
|
||||
|
||||
# API 호출 URL 및 거래 ID 설정
|
||||
|
||||
tr_id = "CTPF1604R"
|
||||
|
||||
# 요청 파라미터 설정
|
||||
params = {
|
||||
"PDNO": pdno,
|
||||
"PRDT_TYPE_CD": prdt_type_cd,
|
||||
}
|
||||
|
||||
# API 호출
|
||||
res = ka._url_fetch(API_URL, tr_id, tr_cont, params)
|
||||
|
||||
# API 응답 처리
|
||||
if res.isOK():
|
||||
if hasattr(res.getBody(), 'output'):
|
||||
output_data = res.getBody().output
|
||||
if not isinstance(output_data, list):
|
||||
output_data = [output_data]
|
||||
current_data = pd.DataFrame(output_data)
|
||||
else:
|
||||
current_data = pd.DataFrame()
|
||||
|
||||
# 데이터프레임 병합
|
||||
if dataframe is not None:
|
||||
dataframe = pd.concat([dataframe, current_data], ignore_index=True)
|
||||
else:
|
||||
dataframe = current_data
|
||||
|
||||
# 연속 거래 여부 확인
|
||||
tr_cont = res.getHeader().tr_cont
|
||||
|
||||
if tr_cont == "M":
|
||||
logger.info("Calling next page...")
|
||||
ka.smart_sleep()
|
||||
return search_info(
|
||||
pdno,
|
||||
prdt_type_cd,
|
||||
"N", dataframe, depth + 1, max_depth
|
||||
)
|
||||
else:
|
||||
logger.info("Data fetch complete.")
|
||||
return dataframe
|
||||
else:
|
||||
logger.error("API call failed: %s - %s", res.getErrorCode(), res.getErrorMessage())
|
||||
res.printError(API_URL)
|
||||
return pd.DataFrame()
|
||||
Reference in New Issue
Block a user