매수·매도·정정·취소 주문 흐름과 각 메서드 사용법을 안내합니다.
!!! warning “실계좌 주의”
env="real" 환경에서는 실제 계좌에 주문이 접수됩니다.
반드시 env="demo" (모의투자)로 먼저 테스트하세요.
from kiwoompy import KiwoomClient
# 모의투자 환경 (테스트용)
client = KiwoomClient(env="demo", appkey="...", secretkey="...")
# 실계좌 환경 (실제 주문 발생)
# client = KiwoomClient(env="real", appkey="...", secretkey="...")
result = client.order.buy(
stock_code="005930", # 삼성전자
quantity="1",
trade_type="market",
)
print(result.ord_no) # 주문번호
print(result.dmst_stex_tp) # 거래소 (KRX / NXT)
result = client.order.buy(
stock_code="005930",
quantity="10",
trade_type="limit",
price="70000", # 주문단가 (원)
)
result = client.order.buy(
stock_code="005930",
quantity="5",
trade_type="best",
)
result = client.order.buy(
stock_code="005930",
quantity="1",
trade_type="stop",
price="72000", # 주문단가
condition_price="71000", # 스톱가 (이 가격 이상이 되면 주문 발동)
)
# 시장가 매도
result = client.order.sell(
stock_code="005930",
quantity="1",
trade_type="market",
)
# 지정가 매도
result = client.order.sell(
stock_code="005930",
quantity="1",
trade_type="limit",
price="75000",
)
미체결 주문의 가격이나 수량을 변경합니다.
# 매수 주문 후 가격 정정
buy_result = client.order.buy(
stock_code="005930",
quantity="10",
trade_type="limit",
price="70000",
)
# 70,000원 → 71,000원으로 정정
modified = client.order.modify(
original_order_no=buy_result.ord_no,
stock_code="005930",
modify_quantity="10",
modify_price="71000",
)
print(modified.ord_no) # 새 주문번호
print(modified.mdfy_qty) # 정정수량
!!! note “정정 후 주문번호”
정정 주문이 접수되면 새 주문번호(ord_no)가 발급됩니다.
이후 추가 정정·취소 시에는 새 주문번호를 사용해야 합니다.
# 잔량 전부 취소
client.order.cancel(
original_order_no=buy_result.ord_no,
stock_code="005930",
cancel_quantity="0", # "0" 입력 시 잔량 전부 취소
)
# 일부 취소 (5주만)
client.order.cancel(
original_order_no=buy_result.ord_no,
stock_code="005930",
cancel_quantity="5",
)
주문 접수 후 체결 여부를 확인하는 일반적인 흐름입니다.
import time
# 1. 매수 주문
result = client.order.buy(
stock_code="005930",
quantity="1",
trade_type="limit",
price="70000",
)
order_no = result.ord_no
# 2. 미체결 확인
unfilled = client.query.get_unfilled_orders(
all_stock_type="0",
trade_type="buy",
)
is_unfilled = any(o.ord_no == order_no for o in unfilled)
print("미체결 중:", is_unfilled)
# 3. 체결 내역 확인
filled = client.query.get_filled_orders(
query_type="all",
sell_type="buy",
)
for order in filled:
if order.ord_no == order_no:
print(f"체결가: {order.cntr_pric}원 / 체결수량: {order.cntr_qty}주")
trade_type) 전체 목록| 값 | 한글명 | 비고 |
|---|---|---|
"limit" |
보통(지정가) | price 필수 |
"market" |
시장가 | price 생략 |
"conditional" |
조건부지정가 | |
"best" |
최유리지정가 | |
"priority" |
최우선지정가 | |
"stop" |
스톱지정가 | condition_price 필수 |
"mid" |
중간가 | |
"limit_ioc" |
보통(IOC) | |
"market_ioc" |
시장가(IOC) | |
"best_ioc" |
최유리(IOC) | |
"limit_fok" |
보통(FOK) | |
"market_fok" |
시장가(FOK) | |
"best_fok" |
최유리(FOK) | |
"pre_market" |
장시작전시간외 | |
"after_hours" |
시간외단일가 | |
"post_market" |
장마감후시간외 |
exchange)| 값 | 설명 |
|---|---|
"KRX" |
한국거래소 (기본값) |
"NXT" |
넥스트트레이드 |
"SOR" |
최선주문집행 |
신용융자 매수·매도도 동일한 인터페이스로 사용합니다.
# 신용 매수
result = client.order.credit_buy(
stock_code="005930",
quantity="10",
trade_type="limit",
price="70000",
)
# 신용 매도 (융자 상환)
result = client.order.credit_sell(
stock_code="005930",
quantity="10",
trade_type="market",
credit_deal_type="33", # "33": 융자, "99": 융자합
credit_loan_date="20240101", # 융자 시 대출일 필수 (YYYYMMDD)
)
from kiwoompy import KiwoomAuthError, KiwoomApiError
try:
result = client.order.buy(
stock_code="005930",
quantity="1",
trade_type="market",
)
except KiwoomAuthError as e:
print(f"인증 오류: {e}") # 토큰 만료, 잘못된 키 등
except KiwoomApiError as e:
print(f"주문 실패: {e}") # 잔고 부족, 종목 오류 등