initial commit

This commit is contained in:
2026-02-04 00:16:34 +09:00
commit ae11528dd9
867 changed files with 209640 additions and 0 deletions

View File

@@ -0,0 +1,105 @@
# -*- 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 invest_opinion import invest_opinion
# 로깅 설정
logging.basicConfig(level=logging.INFO, format='%(levelname)s - %(message)s')
logger = logging.getLogger(__name__)
##############################################################################################
# [국내주식] 종목정보 > 국내주식 종목투자의견[국내주식-188]
##############################################################################################
COLUMN_MAPPING = {
'stck_bsop_date': '주식영업일자',
'invt_opnn': '투자의견',
'invt_opnn_cls_code': '투자의견구분코드',
'rgbf_invt_opnn': '직전투자의견',
'rgbf_invt_opnn_cls_code': '직전투자의견구분코드',
'hts_goal_prc': 'HTS목표가격',
'stck_prdy_clpr': '주식전일종가',
'stck_nday_esdg': '주식N일괴리도',
'nday_dprt': 'N일괴리율',
'stft_esdg': '주식선물괴리도',
'dprt': '괴리율'
}
NUMERIC_COLUMNS = []
def main():
"""
[국내주식] 종목정보
국내주식 종목투자의견[국내주식-188]
국내주식 종목투자의견 테스트 함수
Parameters:
- fid_cond_mrkt_div_code (str): 조건시장분류코드 (J(시장 구분 코드))
- fid_cond_scr_div_code (str): 조건화면분류코드 (16633(Primary key))
- fid_input_iscd (str): 입력종목코드 (종목코드(ex) 005930(삼성전자)))
- fid_input_date_1 (str): 입력날짜1 (이후 ~(ex) 0020231113))
- fid_input_date_2 (str): 입력날짜2 (~ 이전(ex) 0020240513))
Returns:
- DataFrame: 국내주식 종목투자의견 결과
Example:
>>> df = invest_opinion(fid_cond_mrkt_div_code="J", fid_cond_scr_div_code="16633", fid_input_iscd="005930", fid_input_date_1="20231113", fid_input_date_2="20240513")
"""
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 = invest_opinion(
fid_cond_mrkt_div_code="J", # 조건시장분류코드
fid_cond_scr_div_code="16633", # 조건화면분류코드
fid_input_iscd="005930", # 입력종목코드
fid_input_date_1="20250101", # 입력날짜1
fid_input_date_2="20250617", # 입력날짜2
)
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()

View File

@@ -0,0 +1,150 @@
# [국내주식] 종목정보 - 국내주식 종목투자의견
# 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__)
##############################################################################################
# [국내주식] 종목정보 > 국내주식 종목투자의견[국내주식-188]
##############################################################################################
# 상수 정의
API_URL = "/uapi/domestic-stock/v1/quotations/invest-opinion"
def invest_opinion(
fid_cond_mrkt_div_code: str, # 조건시장분류코드
fid_cond_scr_div_code: str, # 조건화면분류코드
fid_input_iscd: str, # 입력종목코드
fid_input_date_1: str, # 입력날짜1
fid_input_date_2: str, # 입력날짜2
tr_cont: str = "", # 연속 거래 여부
dataframe: Optional[pd.DataFrame] = None, # 누적 데이터프레임
depth: int = 0, # 현재 재귀 깊이
max_depth: int = 10 # 최대 재귀 깊이
) -> Optional[pd.DataFrame]:
"""
[국내주식] 종목정보
국내주식 종목투자의견[국내주식-188]
국내주식 종목투자의견 API를 호출하여 DataFrame으로 반환합니다.
Args:
fid_cond_mrkt_div_code (str): J(시장 구분 코드)
fid_cond_scr_div_code (str): 16633(Primary key)
fid_input_iscd (str): 종목코드(ex) 005930(삼성전자))
fid_input_date_1 (str): 이후 ~(ex) 0020231113)
fid_input_date_2 (str): ~ 이전(ex) 0020240513)
tr_cont (str): 연속 거래 여부
dataframe (Optional[pd.DataFrame]): 누적 데이터프레임
depth (int): 현재 재귀 깊이
max_depth (int): 최대 재귀 깊이 (기본값: 10)
Returns:
Optional[pd.DataFrame]: 국내주식 종목투자의견 데이터
Example:
>>> df = invest_opinion(
... fid_cond_mrkt_div_code="J",
... fid_cond_scr_div_code="16633",
... fid_input_iscd="005930",
... fid_input_date_1="20231113",
... fid_input_date_2="20240513"
... )
>>> print(df)
"""
# 로깅 설정
logger = logging.getLogger(__name__)
# 필수 파라미터 검증
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 not fid_cond_scr_div_code:
logger.error("fid_cond_scr_div_code is required. (e.g. '16633')")
raise ValueError("fid_cond_scr_div_code is required. (e.g. '16633')")
if not fid_input_iscd:
logger.error("fid_input_iscd is required. (e.g. '005930')")
raise ValueError("fid_input_iscd is required. (e.g. '005930')")
if not fid_input_date_1:
logger.error("fid_input_date_1 is required. (e.g. '20231113')")
raise ValueError("fid_input_date_1 is required. (e.g. '20231113')")
if not fid_input_date_2:
logger.error("fid_input_date_2 is required. (e.g. '20240513')")
raise ValueError("fid_input_date_2 is required. (e.g. '20240513')")
# 최대 재귀 깊이 체크
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 = "FHKST663300C0"
# 요청 파라미터 설정
params = {
"FID_COND_MRKT_DIV_CODE": fid_cond_mrkt_div_code,
"FID_COND_SCR_DIV_CODE": fid_cond_scr_div_code,
"FID_INPUT_ISCD": fid_input_iscd,
"FID_INPUT_DATE_1": fid_input_date_1,
"FID_INPUT_DATE_2": fid_input_date_2,
}
# 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 invest_opinion(
fid_cond_mrkt_div_code,
fid_cond_scr_div_code,
fid_input_iscd,
fid_input_date_1,
fid_input_date_2,
"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()