initial commit
This commit is contained in:
@@ -0,0 +1,119 @@
|
||||
# -*- coding: utf-8 -*-
|
||||
"""
|
||||
Created on 2025-07-01
|
||||
|
||||
"""
|
||||
|
||||
import sys
|
||||
import logging
|
||||
|
||||
import pandas as pd
|
||||
|
||||
sys.path.extend(['../..', '.']) # kis_auth 파일 경로 추가
|
||||
import kis_auth as ka
|
||||
from search_contract_detail import search_contract_detail
|
||||
|
||||
# 로깅 설정
|
||||
logging.basicConfig(level=logging.INFO, format='%(levelname)s - %(message)s')
|
||||
logger = logging.getLogger(__name__)
|
||||
|
||||
##############################################################################################
|
||||
# [해외선물옵션] 기본시세 > 해외선물 상품기본정보[해외선물-023]
|
||||
##############################################################################################
|
||||
|
||||
# 컬럼명 매핑
|
||||
COLUMN_MAPPING = {
|
||||
'exch_cd': '거래소코드',
|
||||
'clas_cd': '품목종류',
|
||||
'crc_cd': '거래통화',
|
||||
'sttl_price': '정산가',
|
||||
'sttl_date': '정산일',
|
||||
'trst_mgn': '증거금',
|
||||
'disp_digit': '가격표시진법',
|
||||
'tick_sz': '틱사이즈',
|
||||
'tick_val': '틱가치',
|
||||
'mrkt_open_date': '장개시일자',
|
||||
'mrkt_open_time': '장개시시각',
|
||||
'mrkt_close_date': '장마감일자',
|
||||
'mrkt_close_time': '장마감시각',
|
||||
'trd_fr_date': '상장일',
|
||||
'expr_date': '만기일',
|
||||
'trd_to_date': '최종거래일',
|
||||
'remn_cnt': '잔존일수',
|
||||
'stat_tp': '매매여부',
|
||||
'ctrt_size': '계약크기',
|
||||
'stl_tp': '최종결제구분',
|
||||
'frst_noti_date': '최초식별일',
|
||||
'sub_exch_nm': '서브거래소코드'
|
||||
}
|
||||
|
||||
# 숫자형 컬럼
|
||||
NUMERIC_COLUMNS = ['정산가', '증거금', '틱사이즈', '틱가치', '잔존일수', '계약크기']
|
||||
|
||||
def main():
|
||||
"""
|
||||
[해외선물옵션] 기본시세
|
||||
해외선물 상품기본정보[해외선물-023]
|
||||
|
||||
해외선물 상품기본정보 테스트 함수
|
||||
|
||||
Parameters:
|
||||
- qry_cnt (str): 요청개수 (1~32 사이의 값)
|
||||
- srs_cd_01 (str): 종목코드 1 (예: BONU25)
|
||||
- srs_cd_02 (str): 종목코드 2
|
||||
- ...
|
||||
- srs_cd_32 (str): 종목코드 32 (최대 32개 종목코드까지 조회 가능)
|
||||
|
||||
Returns:
|
||||
- DataFrame: 해외선물 상품기본정보 결과
|
||||
|
||||
Example:
|
||||
>>> df = search_contract_detail(qry_cnt="1", srs_cd_01="BONU25", srs_cd_02="BONU25", srs_cd_03="BONU25")
|
||||
"""
|
||||
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 호출
|
||||
logger.info("API 호출")
|
||||
result = search_contract_detail(
|
||||
qry_cnt="3",
|
||||
srs_cd_01="BONU25",
|
||||
srs_cd_02="BONU25",
|
||||
srs_cd_03="BONU25"
|
||||
)
|
||||
|
||||
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,114 @@
|
||||
# -*- coding: utf-8 -*-
|
||||
"""
|
||||
Created on 2025-07-01
|
||||
|
||||
"""
|
||||
|
||||
import logging
|
||||
import time
|
||||
from typing import Optional
|
||||
import sys
|
||||
|
||||
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__)
|
||||
|
||||
# 상수 정의
|
||||
API_URL = "/uapi/overseas-futureoption/v1/quotations/search-contract-detail"
|
||||
|
||||
##############################################################################################
|
||||
# [해외선물옵션] 기본시세 > 해외선물 상품기본정보[해외선물-023]
|
||||
##############################################################################################
|
||||
|
||||
def search_contract_detail(
|
||||
qry_cnt: str, # 요청개수
|
||||
tr_cont: str = "",
|
||||
dataframe: Optional[pd.DataFrame] = None,
|
||||
depth: int = 0,
|
||||
max_depth: int = 10,
|
||||
**kwargs # srs_cd_01, srs_cd_02, ... srs_cd_32 등을 받음
|
||||
) -> Optional[pd.DataFrame]:
|
||||
"""
|
||||
[해외선물옵션] 기본시세
|
||||
해외선물 상품기본정보[해외선물-023]
|
||||
해외선물 상품기본정보 API를 호출하여 DataFrame으로 반환합니다.
|
||||
|
||||
Args:
|
||||
qry_cnt (str): 입력한 코드 개수
|
||||
tr_cont (str): 연속 거래 여부
|
||||
dataframe (Optional[pd.DataFrame]): 누적 데이터프레임
|
||||
depth (int): 현재 재귀 깊이
|
||||
max_depth (int): 최대 재귀 깊이 (기본값: 10)
|
||||
**kwargs: srs_cd_01, srs_cd_02, ... srs_cd_32 품목종류 코드들
|
||||
|
||||
Returns:
|
||||
Optional[pd.DataFrame]: 해외선물 상품기본정보 데이터
|
||||
|
||||
Example:
|
||||
>>> df = search_contract_detail(
|
||||
... qry_cnt="3",
|
||||
... srs_cd_01="SRS001",
|
||||
... srs_cd_02="SRS002",
|
||||
... srs_cd_03="SRS003"
|
||||
... )
|
||||
>>> print(df)
|
||||
"""
|
||||
# [필수 파라미터 검증]
|
||||
if not 1 <= int(qry_cnt) <= 32:
|
||||
logger.error("qry_cnt is required. (e.g. '1' ~ '32')")
|
||||
raise ValueError("qry_cnt is required. (e.g. '1' ~ '32')")
|
||||
|
||||
# 최대 재귀 깊이 체크
|
||||
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()
|
||||
|
||||
tr_id = "HHDFC55200000"
|
||||
|
||||
# 기본 파라미터
|
||||
params = {
|
||||
"QRY_CNT": qry_cnt,
|
||||
}
|
||||
|
||||
# SRS_CD_01 ~ SRS_CD_32 파라미터 동적 생성
|
||||
for i in range(1, 33):
|
||||
srs_key = f"srs_cd_{i:02d}"
|
||||
api_key = f"SRS_CD_{i:02d}"
|
||||
params[api_key] = kwargs.get(srs_key, "")
|
||||
|
||||
res = ka._url_fetch(API_URL, tr_id, tr_cont, params)
|
||||
|
||||
if res.isOK():
|
||||
if hasattr(res.getBody(), 'output2'):
|
||||
output_data = res.getBody().output2
|
||||
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 in ["M", "F"]:
|
||||
logger.info("Calling next page...")
|
||||
ka.smart_sleep()
|
||||
return search_contract_detail(
|
||||
qry_cnt, "N", dataframe, depth + 1, max_depth, **kwargs
|
||||
)
|
||||
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