Co-authored-by: genichin <4282227+genichin@users.noreply.github.com>
244 lines
7.3 KiB
Python
244 lines
7.3 KiB
Python
"""KIS poll_fill terminal-order detection (dcy-013 / REQ-012, issue #9).
|
|
|
|
inquire-daily-ccld marks a cancelled order with cncl_yn="Y" and an
|
|
exchange-rejected order with rjct_qty. These tests stub the HTTP layer (token
|
|
pre-seeded, httpx.get faked) and assert the FillStatus mapping.
|
|
"""
|
|
|
|
from __future__ import annotations
|
|
|
|
from datetime import datetime, timedelta, timezone
|
|
|
|
from trex_core.adapters.broker import kis as kis_mod
|
|
from trex_core.adapters.broker.kis import KISBroker
|
|
|
|
ODNO = "0000123456"
|
|
|
|
|
|
def _broker():
|
|
b = KISBroker(
|
|
"12345678-01",
|
|
app_key="k",
|
|
app_secret="s",
|
|
base_url="https://openapivts.koreainvestment.com:29443", # mock endpoint
|
|
)
|
|
b._token = "tok"
|
|
b._token_exp = datetime.now(tz=timezone.utc) + timedelta(hours=1)
|
|
return b
|
|
|
|
|
|
class _Resp:
|
|
def __init__(self, payload):
|
|
self._payload = payload
|
|
|
|
def json(self):
|
|
return self._payload
|
|
|
|
|
|
def _fake_get(payload):
|
|
def _get(*a, **k):
|
|
return _Resp(payload)
|
|
|
|
return _get
|
|
|
|
|
|
def _row(**kw):
|
|
base = {
|
|
"odno": ODNO,
|
|
"ord_qty": "10",
|
|
"tot_ccld_qty": "0",
|
|
"avg_prvs": "0",
|
|
"cncl_yn": "N",
|
|
"rjct_qty": "0",
|
|
"ord_dt": "20260608",
|
|
"ord_gno_brno": "06010",
|
|
}
|
|
base.update(kw)
|
|
return {"rt_cd": "0", "output1": [base]}
|
|
|
|
|
|
def test_full_external_cancel_maps_to_cancelled(monkeypatch):
|
|
monkeypatch.setattr(kis_mod.httpx, "get", _fake_get(_row(tot_ccld_qty="0", cncl_yn="Y")))
|
|
fs = _broker().poll_fill(
|
|
f"06010:{ODNO}", side="sell", symbol="005930", order_date="20260608"
|
|
)
|
|
assert fs.state == "cancelled" and fs.is_terminal_reject and not fs.is_partial_cancel
|
|
|
|
|
|
def test_zero_fill_not_cancelled_stays_accepted(monkeypatch):
|
|
monkeypatch.setattr(kis_mod.httpx, "get", _fake_get(_row(tot_ccld_qty="0", cncl_yn="N")))
|
|
fs = _broker().poll_fill(
|
|
f"06010:{ODNO}", side="sell", symbol="005930", order_date="20260608"
|
|
)
|
|
assert fs.state == "accepted" and not fs.is_terminal_reject
|
|
|
|
|
|
def test_zero_fill_full_rejection_maps_to_rejected(monkeypatch):
|
|
monkeypatch.setattr(
|
|
kis_mod.httpx,
|
|
"get",
|
|
_fake_get(
|
|
_row(
|
|
ord_qty="4",
|
|
tot_ccld_qty="0",
|
|
rmn_qty="0",
|
|
rjct_qty="4",
|
|
cncl_yn="",
|
|
)
|
|
),
|
|
)
|
|
fs = _broker().poll_fill(
|
|
f"06010:{ODNO}", side="buy", symbol="005930", order_date="20260608"
|
|
)
|
|
assert fs.state == "rejected" and fs.is_terminal_reject
|
|
|
|
|
|
def test_zero_fill_partial_rejection_does_not_infer_terminal(monkeypatch):
|
|
monkeypatch.setattr(
|
|
kis_mod.httpx,
|
|
"get",
|
|
_fake_get(_row(ord_qty="10", tot_ccld_qty="0", rjct_qty="4", cncl_yn="")),
|
|
)
|
|
fs = _broker().poll_fill(
|
|
f"06010:{ODNO}", side="buy", symbol="005930", order_date="20260608"
|
|
)
|
|
assert fs.state == "accepted" and not fs.is_terminal_reject
|
|
|
|
|
|
def test_partial_fill_then_cancel_flags_partial_cancel(monkeypatch):
|
|
monkeypatch.setattr(
|
|
kis_mod.httpx,
|
|
"get",
|
|
_fake_get(_row(ord_qty="10", tot_ccld_qty="4", avg_prvs="70000", cncl_yn="Y")),
|
|
)
|
|
fs = _broker().poll_fill(
|
|
f"06010:{ODNO}", side="sell", symbol="005930", order_date="20260608"
|
|
)
|
|
assert fs.state == "partially_filled"
|
|
assert fs.cancelled and fs.is_partial_cancel
|
|
assert fs.filled_qty == 4 and fs.avg_price == 70000.0
|
|
assert fs.execution_id == "kis-daily-ccld:20260608:06010:0000123456:4"
|
|
|
|
|
|
def test_full_fill_unaffected_by_cancel_flag(monkeypatch):
|
|
monkeypatch.setattr(
|
|
kis_mod.httpx,
|
|
"get",
|
|
_fake_get(_row(ord_qty="10", tot_ccld_qty="10", avg_prvs="70000", cncl_yn="N")),
|
|
)
|
|
fs = _broker().poll_fill(
|
|
f"06010:{ODNO}", side="sell", symbol="005930", order_date="20260608"
|
|
)
|
|
assert fs.is_filled and fs.filled_qty == 10 and not fs.is_partial_cancel
|
|
|
|
|
|
# ── Regression: KIS child cancel row (orgn_odno) ─────────────────────────────
|
|
|
|
def _payload_with_child_cancel(parent_odno: str, child_odno: str, ord_qty: str = "89"):
|
|
"""Simulate KIS returning original row (cncl_yn='') + separate cancel child."""
|
|
parent = {
|
|
"odno": parent_odno,
|
|
"orgn_odno": "",
|
|
"ord_qty": ord_qty,
|
|
"tot_ccld_qty": "0",
|
|
"avg_prvs": "0",
|
|
"cncl_yn": "",
|
|
"rjct_qty": "0",
|
|
"rmn_qty": "0",
|
|
"ord_dt": "20260901",
|
|
"ord_gno_brno": "06010",
|
|
}
|
|
child = {
|
|
"odno": child_odno,
|
|
"orgn_odno": parent_odno,
|
|
"ord_qty": ord_qty,
|
|
"tot_ccld_qty": "0",
|
|
"avg_prvs": "0",
|
|
"cncl_yn": "Y",
|
|
"rjct_qty": "0",
|
|
"rmn_qty": "0",
|
|
"ord_dt": "20260901",
|
|
"ord_gno_brno": "06010",
|
|
}
|
|
return {"rt_cd": "0", "output1": [parent, child]}
|
|
|
|
|
|
def test_child_cancel_row_orgn_odno_maps_to_cancelled(monkeypatch):
|
|
"""KIS child row with orgn_odno=<original> + cncl_yn=Y → full cancel."""
|
|
payload = _payload_with_child_cancel(ODNO, "0000123457")
|
|
monkeypatch.setattr(kis_mod.httpx, "get", _fake_get(payload))
|
|
fs = _broker().poll_fill(
|
|
f"06010:{ODNO}", side="buy", symbol="225190", order_date="20260901"
|
|
)
|
|
assert fs.state == "cancelled", f"expected cancelled, got {fs.state}"
|
|
assert fs.is_terminal_reject
|
|
|
|
|
|
def test_correction_child_row_not_misclassified_as_cancel(monkeypatch):
|
|
"""Correction child (cncl_yn='N') must not be treated as a cancel."""
|
|
parent = {
|
|
"odno": ODNO,
|
|
"orgn_odno": "",
|
|
"ord_qty": "10",
|
|
"tot_ccld_qty": "0",
|
|
"avg_prvs": "0",
|
|
"cncl_yn": "",
|
|
"rjct_qty": "0",
|
|
"ord_dt": "20260901",
|
|
"ord_gno_brno": "06010",
|
|
}
|
|
correction_child = {
|
|
"odno": "0000123458",
|
|
"orgn_odno": ODNO,
|
|
"ord_qty": "10",
|
|
"tot_ccld_qty": "0",
|
|
"avg_prvs": "0",
|
|
"cncl_yn": "N", # correction, not cancel
|
|
"rjct_qty": "0",
|
|
"ord_dt": "20260901",
|
|
"ord_gno_brno": "06010",
|
|
}
|
|
monkeypatch.setattr(
|
|
kis_mod.httpx, "get", _fake_get({"rt_cd": "0", "output1": [parent, correction_child]})
|
|
)
|
|
fs = _broker().poll_fill(
|
|
f"06010:{ODNO}", side="buy", symbol="225190", order_date="20260901"
|
|
)
|
|
# Correction child must not infer cancellation; original has no fill evidence
|
|
assert fs.state == "accepted", f"expected accepted, got {fs.state}"
|
|
|
|
|
|
def test_partial_fill_with_child_cancel_row(monkeypatch):
|
|
"""Partial fill on parent + cancel child → partial_cancel."""
|
|
parent = {
|
|
"odno": ODNO,
|
|
"orgn_odno": "",
|
|
"ord_qty": "10",
|
|
"tot_ccld_qty": "4",
|
|
"avg_prvs": "70000",
|
|
"cncl_yn": "",
|
|
"rjct_qty": "0",
|
|
"ord_dt": "20260901",
|
|
"ord_gno_brno": "06010",
|
|
}
|
|
child = {
|
|
"odno": "0000123459",
|
|
"orgn_odno": ODNO,
|
|
"ord_qty": "6",
|
|
"tot_ccld_qty": "0",
|
|
"avg_prvs": "0",
|
|
"cncl_yn": "Y",
|
|
"rjct_qty": "0",
|
|
"ord_dt": "20260901",
|
|
"ord_gno_brno": "06010",
|
|
}
|
|
monkeypatch.setattr(
|
|
kis_mod.httpx, "get", _fake_get({"rt_cd": "0", "output1": [parent, child]})
|
|
)
|
|
fs = _broker().poll_fill(
|
|
f"06010:{ODNO}", side="sell", symbol="225190", order_date="20260901"
|
|
)
|
|
assert fs.state == "partially_filled"
|
|
assert fs.cancelled and fs.is_partial_cancel
|
|
assert fs.filled_qty == 4
|