initial commit
This commit is contained in:
@@ -0,0 +1,73 @@
|
||||
"""
|
||||
Created on 20250601
|
||||
"""
|
||||
|
||||
import sys
|
||||
import logging
|
||||
|
||||
import pandas as pd
|
||||
|
||||
sys.path.extend(['../..', '.'])
|
||||
import kis_auth as ka
|
||||
from intstock_grouplist import intstock_grouplist
|
||||
|
||||
# 로깅 설정
|
||||
logging.basicConfig(level=logging.INFO)
|
||||
|
||||
##############################################################################################
|
||||
# [국내주식] 시세분석 > 관심종목 그룹조회 [국내주식-204]
|
||||
##############################################################################################
|
||||
|
||||
COLUMN_MAPPING = {
|
||||
'date': '일자',
|
||||
'trnm_hour': '전송 시간',
|
||||
'data_rank': '데이터 순위',
|
||||
'inter_grp_code': '관심 그룹 코드',
|
||||
'inter_grp_name': '관심 그룹 명',
|
||||
'ask_cnt': '요청 개수'
|
||||
}
|
||||
|
||||
NUMERIC_COLUMNS = []
|
||||
|
||||
def main():
|
||||
"""
|
||||
관심종목 그룹조회 테스트 함수
|
||||
|
||||
이 함수는 관심종목 그룹조회 API를 호출하여 결과를 출력합니다.
|
||||
|
||||
Returns:
|
||||
None
|
||||
"""
|
||||
|
||||
# pandas 출력 옵션 설정
|
||||
pd.set_option('display.max_columns', None) # 모든 컬럼 표시
|
||||
pd.set_option('display.width', None) # 출력 너비 제한 해제
|
||||
pd.set_option('display.max_rows', None) # 모든 행 표시
|
||||
|
||||
# 인증 토큰 발급
|
||||
ka.auth()
|
||||
trenv = ka.getTREnv()
|
||||
|
||||
# case1 테스트
|
||||
logging.info("=== case1 테스트 ===")
|
||||
try:
|
||||
result = intstock_grouplist(type="1", fid_etc_cls_code="00", user_id=trenv.my_htsid)
|
||||
except ValueError as e:
|
||||
logging.error("에러 발생: %s" % str(e))
|
||||
return
|
||||
|
||||
logging.info("사용 가능한 컬럼: %s", 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)
|
||||
|
||||
logging.info("결과:")
|
||||
print(result)
|
||||
|
||||
if __name__ == "__main__":
|
||||
main()
|
||||
@@ -0,0 +1,75 @@
|
||||
"""
|
||||
Created on 20250601
|
||||
"""
|
||||
|
||||
|
||||
import sys
|
||||
import logging
|
||||
|
||||
import pandas as pd
|
||||
|
||||
sys.path.extend(['../..', '.'])
|
||||
import kis_auth as ka
|
||||
|
||||
# 로깅 설정
|
||||
logging.basicConfig(level=logging.INFO)
|
||||
|
||||
##############################################################################################
|
||||
# [국내주식] 시세분석 > 관심종목 그룹조회 [국내주식-204]
|
||||
##############################################################################################
|
||||
|
||||
# 상수 정의
|
||||
API_URL = "/uapi/domestic-stock/v1/quotations/intstock-grouplist"
|
||||
|
||||
def intstock_grouplist(
|
||||
type: str, # [필수] 관심종목구분코드 (ex. 1)
|
||||
fid_etc_cls_code: str, # [필수] FID 기타 구분 코드 (ex. 00)
|
||||
user_id: str # [필수] 사용자 ID
|
||||
) -> pd.DataFrame:
|
||||
"""
|
||||
관심종목 그룹조회 API입니다.
|
||||
① 관심종목 그룹조회 → ② 관심종목 그룹별 종목조회 → ③ 관심종목(멀티종목) 시세조회 순서대로 호출하셔서 관심종목 시세 조회 가능합니다.
|
||||
|
||||
※ 한 번의 호출에 최대 30종목의 시세 확인 가능합니다.
|
||||
|
||||
한국투자증권 Github 에서 관심종목 복수시세조회 파이썬 샘플코드를 참고하실 수 있습니다.
|
||||
https://github.com/koreainvestment/open-trading-api/blob/main/rest/interest_stocks_price.py
|
||||
|
||||
Args:
|
||||
type (str): [필수] 관심종목구분코드 (ex. 1)
|
||||
fid_etc_cls_code (str): [필수] FID 기타 구분 코드 (ex. 00)
|
||||
user_id (str): [필수] 사용자 ID
|
||||
|
||||
Returns:
|
||||
pd.DataFrame: 관심종목 그룹 정보를 담은 DataFrame
|
||||
|
||||
Example:
|
||||
>>> df = intstock_grouplist(type="1", fid_etc_cls_code="00", user_id=trenv.my_htsid)
|
||||
>>> print(df)
|
||||
"""
|
||||
|
||||
if type == "":
|
||||
raise ValueError("type is required (e.g. '1')")
|
||||
|
||||
if fid_etc_cls_code == "":
|
||||
raise ValueError("fid_etc_cls_code is required (e.g. '00')")
|
||||
|
||||
if user_id == "":
|
||||
raise ValueError("user_id is required")
|
||||
|
||||
tr_id = "HHKCM113004C7" # 관심종목 그룹조회
|
||||
|
||||
params = {
|
||||
"TYPE": type, # 관심종목구분코드
|
||||
"FID_ETC_CLS_CODE": fid_etc_cls_code, # FID 기타 구분 코드
|
||||
"USER_ID": user_id # 사용자 ID
|
||||
}
|
||||
|
||||
res = ka._url_fetch(API_URL, tr_id, "", params)
|
||||
|
||||
if res.isOK():
|
||||
current_data = pd.DataFrame(res.getBody().output2)
|
||||
return current_data
|
||||
else:
|
||||
res.printError(url=API_URL)
|
||||
return pd.DataFrame()
|
||||
Reference in New Issue
Block a user