initial commit
This commit is contained in:
@@ -0,0 +1,103 @@
|
||||
# -*- coding: utf-8 -*-
|
||||
"""
|
||||
Created on 2025-06-18
|
||||
|
||||
"""
|
||||
|
||||
import sys
|
||||
import logging
|
||||
|
||||
import pandas as pd
|
||||
|
||||
sys.path.extend(['../..', '.']) # kis_auth 파일 경로 추가
|
||||
import kis_auth as ka
|
||||
from volatility_trend_minute import volatility_trend_minute
|
||||
|
||||
# 로깅 설정
|
||||
logging.basicConfig(level=logging.INFO, format='%(levelname)s - %(message)s')
|
||||
logger = logging.getLogger(__name__)
|
||||
|
||||
##############################################################################################
|
||||
# [국내주식] ELW시세 - ELW 변동성 추이(분별)[국내주식-179]
|
||||
##############################################################################################
|
||||
|
||||
COLUMN_MAPPING = {
|
||||
'stck_bsop_date': '주식 영업 일자',
|
||||
'stck_cntg_hour': '주식 체결 시간',
|
||||
'stck_prpr': '주식 현재가',
|
||||
'elw_oprc': 'ELW 시가2',
|
||||
'elw_hgpr': 'ELW 최고가',
|
||||
'elw_lwpr': 'ELW 최저가',
|
||||
'hts_ints_vltl': 'HTS 내재 변동성',
|
||||
'hist_vltl': '역사적 변동성'
|
||||
}
|
||||
|
||||
NUMERIC_COLUMNS = [
|
||||
'주식 현재가', 'ELW 시가2', 'ELW 최고가', 'ELW 최저가', 'HTS 내재 변동성', '역사적 변동성'
|
||||
]
|
||||
|
||||
def main():
|
||||
"""
|
||||
[국내주식] ELW시세
|
||||
ELW 변동성 추이(분별)[국내주식-179]
|
||||
|
||||
ELW 변동성 추이(분별) 테스트 함수
|
||||
|
||||
Parameters:
|
||||
- fid_cond_mrkt_div_code (str): 조건시장분류코드 (W(Unique key))
|
||||
- fid_input_iscd (str): 입력종목코드 (ex) 58J297(KBJ297삼성전자콜))
|
||||
- fid_hour_cls_code (str): 시간구분코드 ('60(1분), 180(3분), 300(5분), 600(10분), 1800(30분), 3600(60분) ')
|
||||
- fid_pw_data_incu_yn (str): 과거데이터 포함 여부 (N(과거데이터포함X),Y(과거데이터포함O))
|
||||
Returns:
|
||||
- DataFrame: ELW 변동성 추이(분별) 결과
|
||||
|
||||
Example:
|
||||
>>> df = volatility_trend_minute(fid_cond_mrkt_div_code="W", fid_input_iscd="58J297", fid_hour_cls_code="60", fid_pw_data_incu_yn="N")
|
||||
"""
|
||||
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 = volatility_trend_minute(
|
||||
fid_cond_mrkt_div_code="W", # 조건시장분류코드
|
||||
fid_input_iscd="57LA50", # 입력종목코드
|
||||
fid_hour_cls_code="60", # 시간구분코드
|
||||
fid_pw_data_incu_yn="N", # 과거데이터 포함 여부
|
||||
)
|
||||
|
||||
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,142 @@
|
||||
# [국내주식] ELW시세 - ELW 변동성 추이(분별)
|
||||
# Generated by KIS API Generator (Single API Mode)
|
||||
# -*- coding: utf-8 -*-
|
||||
"""
|
||||
Created on 2025-06-18
|
||||
|
||||
"""
|
||||
|
||||
import logging
|
||||
import time
|
||||
from typing import Optional, Tuple
|
||||
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 변동성추이(분별)[국내주식-179]
|
||||
##############################################################################################
|
||||
|
||||
# 상수 정의
|
||||
API_URL = "/uapi/elw/v1/quotations/volatility-trend-minute"
|
||||
|
||||
def volatility_trend_minute(
|
||||
fid_cond_mrkt_div_code: str, # 조건시장분류코드
|
||||
fid_input_iscd: str, # 입력종목코드
|
||||
fid_hour_cls_code: str, # 시간구분코드
|
||||
fid_pw_data_incu_yn: str, # 과거데이터 포함 여부
|
||||
tr_cont: str = "", # 연속 거래 여부
|
||||
dataframe: Optional[pd.DataFrame] = None, # 누적 데이터프레임
|
||||
depth: int = 0, # 현재 재귀 깊이
|
||||
max_depth: int = 10 # 최대 재귀 깊이
|
||||
) -> Optional[pd.DataFrame]:
|
||||
"""
|
||||
[국내주식] ELW시세
|
||||
ELW 변동성 추이(분별)[국내주식-179]
|
||||
ELW 변동성 추이(분별) API를 호출하여 DataFrame으로 반환합니다.
|
||||
|
||||
Args:
|
||||
fid_cond_mrkt_div_code (str): 조건시장분류코드 (예: 'W')
|
||||
fid_input_iscd (str): 입력종목코드 (예: '58J297')
|
||||
fid_hour_cls_code (str): 시간구분코드 (예: '60', '180', '300', '600', '1800', '3600')
|
||||
fid_pw_data_incu_yn (str): 과거데이터 포함 여부 ('N' 또는 'Y')
|
||||
tr_cont (str): 연속 거래 여부 (기본값: "")
|
||||
dataframe (Optional[pd.DataFrame]): 누적 데이터프레임
|
||||
depth (int): 현재 재귀 깊이
|
||||
max_depth (int): 최대 재귀 깊이 (기본값: 10)
|
||||
|
||||
Returns:
|
||||
Optional[pd.DataFrame]: ELW 변동성 추이(분별) 데이터
|
||||
|
||||
Example:
|
||||
>>> df = volatility_trend_minute(
|
||||
... fid_cond_mrkt_div_code='W',
|
||||
... fid_input_iscd='58J297',
|
||||
... fid_hour_cls_code='60',
|
||||
... fid_pw_data_incu_yn='N'
|
||||
... )
|
||||
>>> 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_input_iscd:
|
||||
logger.error("fid_input_iscd is required. (e.g. '58J297')")
|
||||
raise ValueError("fid_input_iscd is required. (e.g. '58J297')")
|
||||
|
||||
if not fid_hour_cls_code:
|
||||
logger.error("fid_hour_cls_code is required. (e.g. '60')")
|
||||
raise ValueError("fid_hour_cls_code is required. (e.g. '60')")
|
||||
|
||||
if not fid_pw_data_incu_yn:
|
||||
logger.error("fid_pw_data_incu_yn is required. (e.g. 'N')")
|
||||
raise ValueError("fid_pw_data_incu_yn is required. (e.g. 'N')")
|
||||
|
||||
# 최대 재귀 깊이 체크
|
||||
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 설정
|
||||
url = API_URL
|
||||
tr_id = "FHPEW02840300"
|
||||
|
||||
# 요청 파라미터 설정
|
||||
params = {
|
||||
"FID_COND_MRKT_DIV_CODE": fid_cond_mrkt_div_code,
|
||||
"FID_INPUT_ISCD": fid_input_iscd,
|
||||
"FID_HOUR_CLS_CODE": fid_hour_cls_code,
|
||||
"FID_PW_DATA_INCU_YN": fid_pw_data_incu_yn,
|
||||
}
|
||||
|
||||
# API 호출
|
||||
res = ka._url_fetch(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 volatility_trend_minute(
|
||||
fid_cond_mrkt_div_code,
|
||||
fid_input_iscd,
|
||||
fid_hour_cls_code,
|
||||
fid_pw_data_incu_yn,
|
||||
"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