initial commit
This commit is contained in:
110
한국투자증권(API)/examples_llm/overseas_stock/algo_ordno/algo_ordno.py
Normal file
110
한국투자증권(API)/examples_llm/overseas_stock/algo_ordno/algo_ordno.py
Normal file
@@ -0,0 +1,110 @@
|
||||
"""
|
||||
Created on 20250601
|
||||
"""
|
||||
|
||||
|
||||
import sys
|
||||
import time
|
||||
from typing import Optional
|
||||
import logging
|
||||
|
||||
import pandas as pd
|
||||
|
||||
sys.path.extend(['../..', '.'])
|
||||
import kis_auth as ka
|
||||
|
||||
# 로깅 설정
|
||||
logging.basicConfig(level=logging.INFO)
|
||||
|
||||
##############################################################################################
|
||||
# [해외주식] 주문/계좌 > 해외주식 지정가주문번호조회 [해외주식-071]
|
||||
##############################################################################################
|
||||
|
||||
# 상수 정의
|
||||
API_URL = "/uapi/overseas-stock/v1/trading/algo-ordno"
|
||||
|
||||
def algo_ordno(
|
||||
cano: str, # [필수] 종합계좌번호
|
||||
acnt_prdt_cd: str, # [필수] 계좌상품코드 (ex. 01)
|
||||
trad_dt: str, # [필수] 거래일자
|
||||
FK200: str = "", # 연속조회검색조건200
|
||||
NK200: str = "", # 연속조회키200
|
||||
tr_cont: str = "", # 연속거래여부
|
||||
dataframe: Optional[pd.DataFrame] = None, # 누적 데이터프레임
|
||||
depth: int = 0, # 내부 재귀깊이 (자동관리)
|
||||
max_depth: int = 10 # 최대 재귀 횟수 제한
|
||||
) -> pd.DataFrame:
|
||||
"""
|
||||
TWAP, VWAP 주문에 대한 주문번호를 조회하는 API
|
||||
|
||||
Args:
|
||||
cano (str): [필수] 종합계좌번호
|
||||
acnt_prdt_cd (str): [필수] 계좌상품코드 (ex. 01)
|
||||
trad_dt (str): [필수] 거래일자
|
||||
FK200 (str): 연속조회검색조건200
|
||||
NK200 (str): 연속조회키200
|
||||
tr_cont (str): 연속거래여부
|
||||
dataframe (Optional[pd.DataFrame]): 누적 데이터프레임
|
||||
depth (int): 내부 재귀깊이 (자동관리)
|
||||
max_depth (int): 최대 재귀 횟수 제한
|
||||
|
||||
Returns:
|
||||
pd.DataFrame: 해외주식 지정가주문번호 데이터
|
||||
|
||||
Example:
|
||||
>>> df = algo_ordno(cano=trenv.my_acct, acnt_prdt_cd=trenv.my_prod, trad_dt="20250619")
|
||||
>>> print(df)
|
||||
"""
|
||||
|
||||
if cano == "":
|
||||
raise ValueError("cano is required")
|
||||
|
||||
if acnt_prdt_cd == "":
|
||||
raise ValueError("acnt_prdt_cd is required (e.g. '01')")
|
||||
|
||||
if trad_dt == "":
|
||||
raise ValueError("trad_dt is required")
|
||||
|
||||
if depth > max_depth:
|
||||
logging.warning("Max recursive depth reached.")
|
||||
if dataframe is None:
|
||||
return pd.DataFrame()
|
||||
else:
|
||||
return dataframe
|
||||
|
||||
tr_id = "TTTS6058R" # 해외주식 지정가주문번호조회
|
||||
|
||||
params = {
|
||||
"CANO": cano, # 종합계좌번호
|
||||
"ACNT_PRDT_CD": acnt_prdt_cd, # 계좌상품코드
|
||||
"TRAD_DT": trad_dt, # 거래일자
|
||||
"CTX_AREA_FK200": FK200, # 연속조회검색조건200
|
||||
"CTX_AREA_NK200": NK200 # 연속조회키200
|
||||
}
|
||||
|
||||
res = ka._url_fetch(API_URL, tr_id, tr_cont, params)
|
||||
|
||||
if res.isOK():
|
||||
current_data = pd.DataFrame(res.getBody().output)
|
||||
|
||||
if dataframe is not None:
|
||||
dataframe = pd.concat([dataframe, current_data], ignore_index=True)
|
||||
else:
|
||||
dataframe = current_data
|
||||
|
||||
tr_cont = res.getHeader().tr_cont
|
||||
FK200 = res.getBody().ctx_area_fk200
|
||||
NK200 = res.getBody().ctx_area_nk200
|
||||
|
||||
if tr_cont in ["M", "F"]: # 다음 페이지 존재
|
||||
logging.info("Call Next page...")
|
||||
ka.smart_sleep() # 시스템 안정적 운영을 위한 지연
|
||||
return algo_ordno(
|
||||
cano, acnt_prdt_cd, trad_dt, FK200, NK200, "N", dataframe, depth + 1, max_depth
|
||||
)
|
||||
else:
|
||||
logging.info("Data fetch complete.")
|
||||
return dataframe
|
||||
else:
|
||||
res.printError(url=API_URL)
|
||||
return pd.DataFrame()
|
||||
@@ -0,0 +1,79 @@
|
||||
"""
|
||||
Created on 20250601
|
||||
"""
|
||||
|
||||
import sys
|
||||
import logging
|
||||
|
||||
import pandas as pd
|
||||
|
||||
sys.path.extend(['../..', '.'])
|
||||
import kis_auth as ka
|
||||
from algo_ordno import algo_ordno
|
||||
|
||||
# 로깅 설정
|
||||
logging.basicConfig(level=logging.INFO)
|
||||
|
||||
##############################################################################################
|
||||
# [해외주식] 주문/계좌 > 해외주식 지정가주문번호조회 [해외주식-071]
|
||||
##############################################################################################
|
||||
|
||||
# 컬럼 매핑 정의
|
||||
COLUMN_MAPPING = {
|
||||
'ODNO': '주문번호',
|
||||
'TRAD_DVSN_NAME': '매매구분명',
|
||||
'PDNO': '상품번호',
|
||||
'ITEM_NAME': '종목명',
|
||||
'FT_ORD_QTY': 'FT주문수량',
|
||||
'FT_ORD_UNPR3': 'FT주문단가',
|
||||
'SPLT_BUY_ATTR_NAME': '분할매수속성명',
|
||||
'FT_CCLD_QTY': 'FT체결수량',
|
||||
'ORD_GNO_BRNO': '주문채번지점번호'
|
||||
}
|
||||
|
||||
# 숫자형 컬럼 정의
|
||||
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 = algo_ordno(cano=trenv.my_acct, acnt_prdt_cd=trenv.my_prod, trad_dt="20250619")
|
||||
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()
|
||||
Reference in New Issue
Block a user