initial commit
This commit is contained in:
@@ -0,0 +1,128 @@
|
||||
"""
|
||||
Created on 2025-06-19
|
||||
|
||||
"""
|
||||
|
||||
import sys
|
||||
import logging
|
||||
|
||||
import pandas as pd
|
||||
|
||||
sys.path.extend(['../..', '.']) # kis_auth 파일 경로 추가
|
||||
import kis_auth as ka
|
||||
from expiration_stocks import expiration_stocks
|
||||
|
||||
# 로깅 설정
|
||||
logging.basicConfig(level=logging.INFO, format='%(levelname)s - %(message)s')
|
||||
logger = logging.getLogger(__name__)
|
||||
|
||||
##############################################################################################
|
||||
# [국내주식] ELW시세 - ELW 만기예정/만기종목[국내주식-184]
|
||||
##############################################################################################
|
||||
|
||||
COLUMN_MAPPING = {
|
||||
'elw_shrn_iscd': 'ELW단축종목코드',
|
||||
'elw_kor_isnm': 'ELW한글종목명',
|
||||
'unas_isnm': '기초자산종목명',
|
||||
'unas_prpr': '기초자산현재가',
|
||||
'acpr': '행사가',
|
||||
'stck_cnvr_rate': '주식전환비율',
|
||||
'elw_prpr': 'ELW현재가',
|
||||
'stck_lstn_date': '주식상장일자',
|
||||
'stck_last_tr_date': '주식최종거래일자',
|
||||
'total_rdmp_amt': '총상환금액',
|
||||
'rdmp_amt': '상환금액',
|
||||
'lstn_stcn': '상장주수',
|
||||
'lp_hvol': 'LP보유량',
|
||||
'ccls_paym_prc': '확정지급2가격',
|
||||
'mtrt_vltn_amt': '만기평가금액',
|
||||
'evnt_prd_fin_date': '행사2기간종료일자',
|
||||
'stlm_date': '결제일자',
|
||||
'pblc_prc': '발행가격',
|
||||
'unas_shrn_iscd': '기초자산단축종목코드',
|
||||
'stnd_iscd': '표준종목코드',
|
||||
'rdmp_ask_amt': '상환청구금액'
|
||||
}
|
||||
|
||||
NUMERIC_COLUMNS = [
|
||||
'기초자산현재가', '행사가', '주식전환비율', 'ELW현재가', '총상환금액', '상환금액',
|
||||
'상장주수', 'LP보유량', '확정지급2가격', '만기평가금액', '발행가격', '상환청구금액'
|
||||
]
|
||||
|
||||
def main():
|
||||
"""
|
||||
[국내주식] ELW시세
|
||||
ELW 만기예정_만기종목[국내주식-184]
|
||||
|
||||
ELW 만기예정_만기종목 테스트 함수
|
||||
|
||||
Parameters:
|
||||
- fid_cond_mrkt_div_code (str): 조건시장분류코드 (W 입력)
|
||||
- fid_cond_scr_div_code (str): 조건화면분류코드 (11547 입력)
|
||||
- fid_input_date_1 (str): 입력날짜1 (입력날짜 ~ (ex) 20240402))
|
||||
- fid_input_date_2 (str): 입력날짜2 (~입력날짜 (ex) 20240408))
|
||||
- fid_div_cls_code (str): 분류구분코드 (0(콜),1(풋),2(전체))
|
||||
- fid_etc_cls_code (str): 기타구분코드 (공백 입력)
|
||||
- fid_unas_input_iscd (str): 기초자산입력종목코드 (000000(전체), 2001(KOSPI 200), 기초자산코드(종목코드 ex. 삼성전자-005930))
|
||||
- fid_input_iscd_2 (str): 발행회사코드 (00000(전체), 00003(한국투자증권), 00017(KB증권), 00005(미래에셋증권))
|
||||
- fid_blng_cls_code (str): 결제방법 (0(전체),1(일반),2(조기종료))
|
||||
- fid_input_option_1 (str): 입력옵션1 (공백 입력)
|
||||
Returns:
|
||||
- DataFrame: ELW 만기예정_만기종목 결과
|
||||
|
||||
Example:
|
||||
>>> df = expiration_stocks(fid_cond_mrkt_div_code="W", fid_cond_scr_div_code="11547", fid_input_date_1="20240402", fid_input_date_2="20240408", fid_div_cls_code="2", fid_etc_cls_code="", fid_unas_input_iscd="000000", fid_input_iscd_2="00000", fid_blng_cls_code="0", fid_input_option_1="")
|
||||
"""
|
||||
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 = expiration_stocks(
|
||||
fid_cond_mrkt_div_code="W", # 조건시장분류코드
|
||||
fid_cond_scr_div_code="11547", # 조건화면분류코드
|
||||
fid_input_date_1="20250101", # 입력날짜1
|
||||
fid_input_date_2="20250930", # 입력날짜2
|
||||
fid_div_cls_code="2", # 분류구분코드
|
||||
fid_etc_cls_code="", # 기타구분코드
|
||||
fid_unas_input_iscd="000000", # 기초자산입력종목코드
|
||||
fid_input_iscd_2="00000", # 발행회사코드
|
||||
fid_blng_cls_code="0", # 결제방법
|
||||
fid_input_option_1="", # 입력옵션1
|
||||
)
|
||||
|
||||
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("=== ELW 만기예정_만기종목 결과 ===")
|
||||
logger.info("조회된 데이터 건수: %d", len(result))
|
||||
print(result)
|
||||
|
||||
except Exception as e:
|
||||
logger.error("에러 발생: %s", str(e))
|
||||
raise
|
||||
|
||||
if __name__ == "__main__":
|
||||
main()
|
||||
@@ -0,0 +1,179 @@
|
||||
"""
|
||||
Created on 2025-06-19
|
||||
|
||||
"""
|
||||
|
||||
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__)
|
||||
|
||||
##############################################################################################
|
||||
# [국내주식] ELW시세 - ELW 만기예정/만기종목[국내주식-184]
|
||||
##############################################################################################
|
||||
|
||||
# 상수 정의
|
||||
API_URL = "/uapi/elw/v1/quotations/expiration-stocks"
|
||||
|
||||
def expiration_stocks(
|
||||
fid_cond_mrkt_div_code: str, # 조건시장분류코드
|
||||
fid_cond_scr_div_code: str, # 조건화면분류코드
|
||||
fid_input_date_1: str, # 입력날짜1
|
||||
fid_input_date_2: str, # 입력날짜2
|
||||
fid_div_cls_code: str, # 분류구분코드
|
||||
fid_etc_cls_code: str, # 기타구분코드
|
||||
fid_unas_input_iscd: str, # 기초자산입력종목코드
|
||||
fid_input_iscd_2: str, # 발행회사코드
|
||||
fid_blng_cls_code: str, # 결제방법
|
||||
fid_input_option_1: str, # 입력옵션1
|
||||
tr_cont: str = "",
|
||||
dataframe: Optional[pd.DataFrame] = None,
|
||||
depth: int = 0,
|
||||
max_depth: int = 10
|
||||
) -> Optional[pd.DataFrame]:
|
||||
"""
|
||||
[국내주식] ELW시세
|
||||
ELW 만기예정_만기종목[국내주식-184]
|
||||
ELW 만기예정_만기종목 API를 호출하여 DataFrame으로 반환합니다.
|
||||
|
||||
Args:
|
||||
fid_cond_mrkt_div_code (str): W 입력
|
||||
fid_cond_scr_div_code (str): 11547 입력
|
||||
fid_input_date_1 (str): 입력날짜 ~ (ex) 20240402)
|
||||
fid_input_date_2 (str): ~입력날짜 (ex) 20240408)
|
||||
fid_div_cls_code (str): 0(콜),1(풋),2(전체)
|
||||
fid_etc_cls_code (str): 공백 입력
|
||||
fid_unas_input_iscd (str): 000000(전체), 2001(KOSPI 200), 기초자산코드(종목코드 ex. 삼성전자-005930)
|
||||
fid_input_iscd_2 (str): 00000(전체), 00003(한국투자증권), 00017(KB증권), 00005(미래에셋증권)
|
||||
fid_blng_cls_code (str): 0(전체),1(일반),2(조기종료)
|
||||
fid_input_option_1 (str): 공백 입력
|
||||
tr_cont (str): 연속 거래 여부
|
||||
dataframe (Optional[pd.DataFrame]): 누적 데이터프레임
|
||||
depth (int): 현재 재귀 깊이
|
||||
max_depth (int): 최대 재귀 깊이 (기본값: 10)
|
||||
|
||||
Returns:
|
||||
Optional[pd.DataFrame]: ELW 만기예정_만기종목 데이터
|
||||
|
||||
Example:
|
||||
>>> df = expiration_stocks(
|
||||
... fid_cond_mrkt_div_code='W',
|
||||
... fid_cond_scr_div_code='11547',
|
||||
... fid_input_date_1='20240402',
|
||||
... fid_input_date_2='20240408',
|
||||
... fid_div_cls_code='0',
|
||||
... fid_etc_cls_code='',
|
||||
... fid_unas_input_iscd='000000',
|
||||
... fid_input_iscd_2='00000',
|
||||
... fid_blng_cls_code='0',
|
||||
... fid_input_option_1='',
|
||||
... )
|
||||
>>> print(df)
|
||||
"""
|
||||
# 로깅 설정
|
||||
logger = logging.getLogger(__name__)
|
||||
|
||||
# 필수 파라미터 검증
|
||||
if not fid_cond_mrkt_div_code:
|
||||
logger.error("fid_cond_mrkt_div_code is required. (e.g. 'W')")
|
||||
raise ValueError("fid_cond_mrkt_div_code is required. (e.g. 'W')")
|
||||
|
||||
if not fid_cond_scr_div_code:
|
||||
logger.error("fid_cond_scr_div_code is required. (e.g. '11547')")
|
||||
raise ValueError("fid_cond_scr_div_code is required. (e.g. '11547')")
|
||||
|
||||
if not fid_input_date_1:
|
||||
logger.error("fid_input_date_1 is required. (e.g. '20240402')")
|
||||
raise ValueError("fid_input_date_1 is required. (e.g. '20240402')")
|
||||
|
||||
if not fid_input_date_2:
|
||||
logger.error("fid_input_date_2 is required. (e.g. '20240408')")
|
||||
raise ValueError("fid_input_date_2 is required. (e.g. '20240408')")
|
||||
|
||||
if not fid_div_cls_code:
|
||||
logger.error("fid_div_cls_code is required. (e.g. '0')")
|
||||
raise ValueError("fid_div_cls_code is required. (e.g. '0')")
|
||||
|
||||
if not fid_unas_input_iscd:
|
||||
logger.error("fid_unas_input_iscd is required. (e.g. '000000')")
|
||||
raise ValueError("fid_unas_input_iscd is required. (e.g. '000000')")
|
||||
|
||||
if not fid_input_iscd_2:
|
||||
logger.error("fid_input_iscd_2 is required. (e.g. '00000')")
|
||||
raise ValueError("fid_input_iscd_2 is required. (e.g. '00000')")
|
||||
|
||||
if not fid_blng_cls_code:
|
||||
logger.error("fid_blng_cls_code is required. (e.g. '0')")
|
||||
raise ValueError("fid_blng_cls_code is required. (e.g. '0')")
|
||||
|
||||
# 최대 재귀 깊이 체크
|
||||
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 = "FHKEW154700C0"
|
||||
|
||||
params = {
|
||||
"FID_COND_MRKT_DIV_CODE": fid_cond_mrkt_div_code,
|
||||
"FID_COND_SCR_DIV_CODE": fid_cond_scr_div_code,
|
||||
"FID_INPUT_DATE_1": fid_input_date_1,
|
||||
"FID_INPUT_DATE_2": fid_input_date_2,
|
||||
"FID_DIV_CLS_CODE": fid_div_cls_code,
|
||||
"FID_ETC_CLS_CODE": fid_etc_cls_code,
|
||||
"FID_UNAS_INPUT_ISCD": fid_unas_input_iscd,
|
||||
"FID_INPUT_ISCD_2": fid_input_iscd_2,
|
||||
"FID_BLNG_CLS_CODE": fid_blng_cls_code,
|
||||
"FID_INPUT_OPTION_1": fid_input_option_1,
|
||||
}
|
||||
|
||||
# API 호출
|
||||
res = ka._url_fetch(API_URL, tr_id, tr_cont, params)
|
||||
|
||||
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 expiration_stocks(
|
||||
fid_cond_mrkt_div_code,
|
||||
fid_cond_scr_div_code,
|
||||
fid_input_date_1,
|
||||
fid_input_date_2,
|
||||
fid_div_cls_code,
|
||||
fid_etc_cls_code,
|
||||
fid_unas_input_iscd,
|
||||
fid_input_iscd_2,
|
||||
fid_blng_cls_code,
|
||||
fid_input_option_1,
|
||||
"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