kiwoompy

계좌·잔고 조회

계좌번호 목록, 예수금, 보유 종목 잔고, 수익률, 미체결·체결 내역을 조회하는 방법을 안내합니다.

계좌번호 조회

토큰에 연결된 계좌번호 목록을 가져옵니다.

from kiwoompy import KiwoomClient

client = KiwoomClient(env="demo", appkey="...", secretkey="...")

accounts = client.query.get_account_numbers()
print(accounts.account_numbers)  # ['12345678-01', '12345678-02']

KiwoomClient는 초기화 시 계좌번호를 자동으로 설정합니다. 별도로 계좌번호를 지정하지 않아도 대부분의 조회 메서드가 동작합니다.

예수금 조회

# 일반 조회
deposit = client.query.get_deposit(query_type="normal")
print(deposit.entr)            # 예수금
print(deposit.pymn_alow_amt)   # 출금가능금액
print(deposit.ord_alow_amt)    # 주문가능금액
print(deposit.d2_entra)        # D+2 예수금

# 추정 조회 (미체결 주문 반영)
deposit_est = client.query.get_deposit(query_type="estimated")
필드 설명
entr 예수금
pymn_alow_amt 출금가능금액
ord_alow_amt 주문가능금액
d1_entra D+1 예수금
d2_entra D+2 예수금
nrpy_loan 미상환융자금

보유 종목 잔고 조회

get_account_balance()는 보유 종목별 매입금액, 평가금액, 평가손익, 수익률을 반환합니다.

balance = client.query.get_account_balance()

# 계좌 전체 요약
print(balance.tot_pur_amt)   # 총매입금액
print(balance.tot_evlt_amt)  # 총평가금액
print(balance.tot_evlt_pl)   # 총평가손익
print(balance.tot_prft_rt)   # 총수익률(%)

# 종목별 상세
for item in balance.holdings:
    print(f"{item.stk_nm}({item.stk_cd})")
    print(f"  보유수량: {item.rmnd_qty}주")
    print(f"  매입단가: {item.pur_pric}원")
    print(f"  현재가:   {item.cur_prc}원")
    print(f"  평가손익: {item.evltv_prft}원 ({item.prft_rt}%)")

NXT 거래소 잔고를 함께 조회하려면 exchange 파라미터를 변경합니다.

balance_nxt = client.query.get_account_balance(exchange="NXT")

AccountBalance 주요 필드

필드 설명
tot_pur_amt 총매입금액
tot_evlt_amt 총평가금액
tot_evlt_pl 총평가손익
tot_prft_rt 총수익률(%)
prsm_dpst_aset_amt 추정예탁자산
tot_loan_amt 총대출금액
holdings 보유종목 목록 (list[HoldingItem])

HoldingItem 주요 필드

필드 설명
stk_cd 종목코드
stk_nm 종목명
rmnd_qty 보유수량
trde_able_qty 매도가능수량
pur_pric 매입단가
cur_prc 현재가
evlt_amt 평가금액
evltv_prft 평가손익
prft_rt 수익률(%)

계좌평가현황 조회

get_account_evaluation()은 계좌 전체 평가 현황과 종목별 손익을 반환합니다.

evaluation = client.query.get_account_evaluation()

print(evaluation.acnt_nm)         # 계좌명
print(evaluation.entr)            # 예수금
print(evaluation.tot_est_amt)     # 총평가금액
print(evaluation.aset_evlt_amt)   # 자산평가금액
print(evaluation.tdy_lspft)       # 당일손익
print(evaluation.tdy_lspft_rt)    # 당일손익률(%)

for item in evaluation.holdings:
    print(f"{item.stk_nm}: 손익 {item.pl_amt}원 ({item.pl_rt}%)")

상장폐지 종목을 제외하고 조회하려면:

evaluation = client.query.get_account_evaluation(query_type="exclude_delisted")

미체결 주문 조회

# 전체 종목, 전체 매매구분
unfilled = client.query.get_unfilled_orders(
    all_stock_type="0",   # "0": 전체, "1": 특정 종목
    trade_type="all",     # "all" / "sell" / "buy"
)

for order in unfilled:
    print(f"주문번호: {order.ord_no}")
    print(f"  종목: {order.stk_nm}({order.stk_cd})")
    print(f"  구분: {order.io_tp_nm}")
    print(f"  주문수량: {order.ord_qty}주 / 미체결: {order.oso_qty}주")
    print(f"  주문단가: {order.ord_pric}원")

특정 종목의 미체결만 조회:

unfilled = client.query.get_unfilled_orders(
    all_stock_type="1",
    trade_type="all",
    stock_code="005930",
)

체결 내역 조회

filled = client.query.get_filled_orders(
    query_type="all",   # "all": 전체, "by_stock": 종목별
    sell_type="all",    # "all" / "sell" / "buy"
)

for order in filled:
    print(f"주문번호: {order.ord_no} / 종목: {order.stk_nm}")
    print(f"  체결가: {order.cntr_pric}원 / 체결수량: {order.cntr_qty}주")
    print(f"  수수료: {order.tdy_trde_cmsn}원 / 세금: {order.tdy_trde_tax}원")

계좌 수익률 조회

보유 종목별 수익률 목록을 조회합니다.

returns = client.query.get_account_return()

for item in returns:
    print(f"{item.stk_nm}({item.stk_cd})")
    print(f"  매입금액: {item.pur_amt}원 / 당일실현손익: {item.tdy_sel_pl}원")

다음 단계