initial commit
This commit is contained in:
@@ -0,0 +1,137 @@
|
||||
# -*- coding: utf-8 -*-
|
||||
"""
|
||||
Created on 2025-06-17
|
||||
|
||||
"""
|
||||
|
||||
import sys
|
||||
import logging
|
||||
|
||||
import pandas as pd
|
||||
|
||||
sys.path.extend(['../..', '.']) # kis_auth 파일 경로 추가
|
||||
import kis_auth as ka
|
||||
from credit_balance import credit_balance
|
||||
|
||||
# 로깅 설정
|
||||
logging.basicConfig(level=logging.INFO, format='%(levelname)s - %(message)s')
|
||||
logger = logging.getLogger(__name__)
|
||||
|
||||
##############################################################################################
|
||||
# [국내주식] 순위분석 > 국내주식 신용잔고 상위 [국내주식-109]
|
||||
##############################################################################################
|
||||
|
||||
# 통합 컬럼 매핑 (모든 output에서 공통 사용)
|
||||
COLUMN_MAPPING = {
|
||||
'bstp_cls_code': '업종 구분 코드',
|
||||
'hts_kor_isnm': 'HTS 한글 종목명',
|
||||
'stnd_date1': '기준 일자1',
|
||||
'stnd_date2': '기준 일자2',
|
||||
'mksc_shrn_iscd': '유가증권 단축 종목코드',
|
||||
'hts_kor_isnm': 'HTS 한글 종목명',
|
||||
'stck_prpr': '주식 현재가',
|
||||
'prdy_vrss': '전일 대비',
|
||||
'prdy_vrss_sign': '전일 대비 부호',
|
||||
'prdy_ctrt': '전일 대비율',
|
||||
'acml_vol': '누적 거래량',
|
||||
'whol_loan_rmnd_stcn': '전체 융자 잔고 주수',
|
||||
'whol_loan_rmnd_amt': '전체 융자 잔고 금액',
|
||||
'whol_loan_rmnd_rate': '전체 융자 잔고 비율',
|
||||
'whol_stln_rmnd_stcn': '전체 대주 잔고 주수',
|
||||
'whol_stln_rmnd_amt': '전체 대주 잔고 금액',
|
||||
'whol_stln_rmnd_rate': '전체 대주 잔고 비율',
|
||||
'nday_vrss_loan_rmnd_inrt': 'N일 대비 융자 잔고 증가율',
|
||||
'nday_vrss_stln_rmnd_inrt': 'N일 대비 대주 잔고 증가율'
|
||||
}
|
||||
|
||||
NUMERIC_COLUMNS = []
|
||||
|
||||
def main():
|
||||
"""
|
||||
[국내주식] 순위분석
|
||||
국내주식 신용잔고 상위[국내주식-109]
|
||||
|
||||
국내주식 신용잔고 상위 테스트 함수
|
||||
|
||||
Parameters:
|
||||
- fid_cond_scr_div_code (str): 조건 화면 분류 코드 (Unique key(11701))
|
||||
- fid_input_iscd (str): 입력 종목코드 (0000:전체, 0001:거래소, 1001:코스닥, 2001:코스피200,)
|
||||
- fid_option (str): 증가율기간 (2~999)
|
||||
- fid_cond_mrkt_div_code (str): 조건 시장 분류 코드 (시장구분코드 (주식 J))
|
||||
- fid_rank_sort_cls_code (str): 순위 정렬 구분 코드 ('(융자)0:잔고비율 상위, 1: 잔고수량 상위, 2: 잔고금액 상위, 3: 잔고비율 증가상위, 4: 잔고비율 감소상위 (대주)5:잔고비율 상위, 6: 잔고수량 상위, 7: 잔고금액 상위, 8: 잔고비율 증가상위, 9: 잔고비율 감소상위 ')
|
||||
|
||||
Returns:
|
||||
- Tuple[DataFrame, ...]: 국내주식 신용잔고 상위 결과
|
||||
|
||||
Example:
|
||||
>>> df1, df2 = credit_balance(fid_cond_scr_div_code="11701", fid_input_iscd="0000", fid_option="2", fid_cond_mrkt_div_code="J", fid_rank_sort_cls_code="0")
|
||||
"""
|
||||
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 호출 시작: 국내주식 신용잔고 상위")
|
||||
result1, result2 = credit_balance(
|
||||
fid_cond_scr_div_code="11701", # 조건 화면 분류 코드
|
||||
fid_input_iscd="0000", # 입력 종목코드
|
||||
fid_option="2", # 증가율기간
|
||||
fid_cond_mrkt_div_code="J", # 조건 시장 분류 코드
|
||||
fid_rank_sort_cls_code="0", # 순위 정렬 구분 코드
|
||||
)
|
||||
|
||||
# 결과 확인
|
||||
results = [result1, result2]
|
||||
if all(result is None or result.empty for result in results):
|
||||
logger.warning("조회된 데이터가 없습니다.")
|
||||
return
|
||||
|
||||
|
||||
# output1 결과 처리
|
||||
logger.info("=== output1 조회 ===")
|
||||
if not result1.empty:
|
||||
logger.info("사용 가능한 컬럼: %s", result1.columns.tolist())
|
||||
|
||||
# 통합 컬럼명 한글 변환 (필요한 컬럼만 자동 매핑됨)
|
||||
result1 = result1.rename(columns=COLUMN_MAPPING)
|
||||
|
||||
for col in NUMERIC_COLUMNS:
|
||||
if col in result1.columns:
|
||||
result1[col] = pd.to_numeric(result1[col], errors='coerce').round(2)
|
||||
|
||||
logger.info("output1 결과:")
|
||||
print(result1)
|
||||
else:
|
||||
logger.info("output1 데이터가 없습니다.")
|
||||
|
||||
# output2 결과 처리
|
||||
logger.info("=== output2 조회 ===")
|
||||
if not result2.empty:
|
||||
logger.info("사용 가능한 컬럼: %s", result2.columns.tolist())
|
||||
|
||||
# 통합 컬럼명 한글 변환 (필요한 컬럼만 자동 매핑됨)
|
||||
result2 = result2.rename(columns=COLUMN_MAPPING)
|
||||
|
||||
for col in NUMERIC_COLUMNS:
|
||||
if col in result2.columns:
|
||||
result2[col] = pd.to_numeric(result2[col], errors='coerce').round(2)
|
||||
|
||||
logger.info("output2 결과:")
|
||||
print(result2)
|
||||
else:
|
||||
logger.info("output2 데이터가 없습니다.")
|
||||
|
||||
|
||||
except Exception as e:
|
||||
logger.error("에러 발생: %s", str(e))
|
||||
raise
|
||||
|
||||
if __name__ == "__main__":
|
||||
main()
|
||||
@@ -0,0 +1,164 @@
|
||||
"""
|
||||
Created on 2025-06-17
|
||||
|
||||
"""
|
||||
|
||||
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)
|
||||
logger = logging.getLogger(__name__)
|
||||
|
||||
##############################################################################################
|
||||
# [국내주식] 순위분석 > 국내주식 신용잔고 상위 [국내주식-109]
|
||||
##############################################################################################
|
||||
|
||||
# 상수 정의
|
||||
API_URL = "/uapi/domestic-stock/v1/ranking/credit-balance"
|
||||
|
||||
def credit_balance(
|
||||
fid_cond_scr_div_code: str, # 조건 화면 분류 코드
|
||||
fid_input_iscd: str, # 입력 종목코드
|
||||
fid_option: str, # 증가율기간
|
||||
fid_cond_mrkt_div_code: str, # 조건 시장 분류 코드
|
||||
fid_rank_sort_cls_code: str, # 순위 정렬 구분 코드
|
||||
dataframe1: Optional[pd.DataFrame] = None, # 누적 데이터프레임 (output1)
|
||||
dataframe2: Optional[pd.DataFrame] = None, # 누적 데이터프레임 (output2)
|
||||
tr_cont: str = "",
|
||||
depth: int = 0,
|
||||
max_depth: int = 10
|
||||
) -> Tuple[pd.DataFrame, pd.DataFrame]:
|
||||
"""
|
||||
[국내주식] 순위분석
|
||||
국내주식 신용잔고 상위[국내주식-109]
|
||||
국내주식 신용잔고 상위 API를 호출하여 DataFrame으로 반환합니다.
|
||||
|
||||
Args:
|
||||
fid_cond_scr_div_code (str): Unique key(11701)
|
||||
fid_input_iscd (str): 0000:전체, 0001:거래소, 1001:코스닥, 2001:코스피200,
|
||||
fid_option (str): 2~999
|
||||
fid_cond_mrkt_div_code (str): 시장구분코드 (주식 J)
|
||||
fid_rank_sort_cls_code (str): '(융자)0:잔고비율 상위, 1: 잔고수량 상위, 2: 잔고금액 상위, 3: 잔고비율 증가상위, 4: 잔고비율 감소상위 (대주)5:잔고비율 상위, 6: 잔고수량 상위, 7: 잔고금액 상위, 8: 잔고비율 증가상위, 9: 잔고비율 감소상위 '
|
||||
dataframe1 (Optional[pd.DataFrame]): 누적 데이터프레임 (output1)
|
||||
dataframe2 (Optional[pd.DataFrame]): 누적 데이터프레임 (output2)
|
||||
tr_cont (str): 연속 거래 여부
|
||||
depth (int): 현재 재귀 깊이
|
||||
max_depth (int): 최대 재귀 깊이 (기본값: 10)
|
||||
|
||||
Returns:
|
||||
Tuple[pd.DataFrame, pd.DataFrame]: 국내주식 신용잔고 상위 데이터
|
||||
|
||||
Example:
|
||||
>>> df1, df2 = credit_balance('11701', '0000', '2', 'J', '0')
|
||||
>>> print(df1)
|
||||
>>> print(df2)
|
||||
"""
|
||||
# 필수 파라미터 검증
|
||||
if not fid_cond_scr_div_code:
|
||||
logger.error("fid_cond_scr_div_code is required. (e.g. '11701')")
|
||||
raise ValueError("fid_cond_scr_div_code is required. (e.g. '11701')")
|
||||
|
||||
if not fid_input_iscd:
|
||||
logger.error("fid_input_iscd is required. (e.g. '0000')")
|
||||
raise ValueError("fid_input_iscd is required. (e.g. '0000')")
|
||||
|
||||
if not fid_option:
|
||||
logger.error("fid_option is required. (e.g. '2')")
|
||||
raise ValueError("fid_option is required. (e.g. '2')")
|
||||
|
||||
if not fid_cond_mrkt_div_code:
|
||||
logger.error("fid_cond_mrkt_div_code is required. (e.g. 'J')")
|
||||
raise ValueError("fid_cond_mrkt_div_code is required. (e.g. 'J')")
|
||||
|
||||
if fid_rank_sort_cls_code not in ['0', '1', '2', '3', '4', '5', '6', '7', '8', '9']:
|
||||
logger.error("fid_rank_sort_cls_code is required. (e.g. '0')")
|
||||
raise ValueError("fid_rank_sort_cls_code is required. (e.g. '0')")
|
||||
|
||||
# 최대 재귀 깊이 체크
|
||||
if depth >= max_depth:
|
||||
logger.warning("Maximum recursion depth (%d) reached. Stopping further requests.", max_depth)
|
||||
return dataframe1 if dataframe1 is not None else pd.DataFrame(), dataframe2 if dataframe2 is not None else pd.DataFrame()
|
||||
|
||||
tr_id = "FHKST17010000"
|
||||
|
||||
params = {
|
||||
"FID_COND_SCR_DIV_CODE": fid_cond_scr_div_code,
|
||||
"FID_INPUT_ISCD": fid_input_iscd,
|
||||
"FID_OPTION": fid_option,
|
||||
"FID_COND_MRKT_DIV_CODE": fid_cond_mrkt_div_code,
|
||||
"FID_RANK_SORT_CLS_CODE": fid_rank_sort_cls_code,
|
||||
}
|
||||
|
||||
res = ka._url_fetch(API_URL, tr_id, tr_cont, params)
|
||||
|
||||
if res.isOK():
|
||||
# output1 처리
|
||||
if hasattr(res.getBody(), 'output1'):
|
||||
output_data = res.getBody().output1
|
||||
if output_data:
|
||||
# output1은 단일 객체, output2는 배열일 수 있음
|
||||
if isinstance(output_data, list):
|
||||
current_data1 = pd.DataFrame(output_data)
|
||||
else:
|
||||
# 단일 객체인 경우 리스트로 감싸서 DataFrame 생성
|
||||
current_data1 = pd.DataFrame([output_data])
|
||||
|
||||
if dataframe1 is not None:
|
||||
dataframe1 = pd.concat([dataframe1, current_data1], ignore_index=True)
|
||||
else:
|
||||
dataframe1 = current_data1
|
||||
else:
|
||||
if dataframe1 is None:
|
||||
dataframe1 = pd.DataFrame()
|
||||
else:
|
||||
if dataframe1 is None:
|
||||
dataframe1 = pd.DataFrame()
|
||||
# output2 처리
|
||||
if hasattr(res.getBody(), 'output2'):
|
||||
output_data = res.getBody().output2
|
||||
if output_data:
|
||||
# output1은 단일 객체, output2는 배열일 수 있음
|
||||
if isinstance(output_data, list):
|
||||
current_data2 = pd.DataFrame(output_data)
|
||||
else:
|
||||
# 단일 객체인 경우 리스트로 감싸서 DataFrame 생성
|
||||
current_data2 = pd.DataFrame([output_data])
|
||||
|
||||
if dataframe2 is not None:
|
||||
dataframe2 = pd.concat([dataframe2, current_data2], ignore_index=True)
|
||||
else:
|
||||
dataframe2 = current_data2
|
||||
else:
|
||||
if dataframe2 is None:
|
||||
dataframe2 = pd.DataFrame()
|
||||
else:
|
||||
if dataframe2 is None:
|
||||
dataframe2 = pd.DataFrame()
|
||||
tr_cont = res.getHeader().tr_cont
|
||||
|
||||
if tr_cont in ["M", "F"]:
|
||||
logger.info("Calling next page...")
|
||||
ka.smart_sleep()
|
||||
return credit_balance(
|
||||
fid_cond_scr_div_code,
|
||||
fid_input_iscd,
|
||||
fid_option,
|
||||
fid_cond_mrkt_div_code,
|
||||
fid_rank_sort_cls_code,
|
||||
"N", dataframe1, dataframe2, depth + 1, max_depth
|
||||
)
|
||||
else:
|
||||
logger.info("Data fetch complete.")
|
||||
return dataframe1, dataframe2
|
||||
else:
|
||||
logger.error("API call failed: %s - %s", res.getErrorCode(), res.getErrorMessage())
|
||||
res.printError(API_URL)
|
||||
return pd.DataFrame(), pd.DataFrame()
|
||||
Reference in New Issue
Block a user