Skip to content

"Vibe-Trading: Your Personal Trading Agent"

fix(kis): see SOR fills and recover from a server-invalidated token

jacob-clobote9f8d1c145d3
diff --git a/agent/src/trading/connectors/kis/sdk.py b/agent/src/trading/connectors/kis/sdk.py
index c2125e6fd..44b2ae750 100644
--- a/agent/src/trading/connectors/kis/sdk.py
+++ b/agent/src/trading/connectors/kis/sdk.py
@@ -69,6 +69,15 @@ _TR_CHART = "FHKST03010100"
_MARKET_DIV_KRX = "J"
#: Exchange id division code required by order-cash since the 2024 API rev.
_EXCHANGE_ID_KRX = "KRX"
+#: Exchange ids the daily order/execution inquiry must be asked for separately.
+#
+# Since Nextrade (NXT) opened as a second Korean venue, brokers route most
+# retail orders through SOR (smart order routing over KRX+NXT). ``daily-ccld``
+# filters on EXCG_ID_DVSN_CD and the buckets are DISJOINT, not nested: a live
+# account returned 6 rows for KRX and 33 for SOR with ZERO overlap. Asking only
+# for KRX (the previous behaviour) silently dropped most fills, so open_orders
+# and executions looked empty. Ask each venue and merge on (ord_dt, odno).
+_EXCHANGE_IDS_INQUIRY = ("KRX", "SOR", "NXT")
#: Safety margin subtracted from the token's reported TTL before it is
#: treated as expired, so a request never races an in-flight expiry.
@@ -309,33 +318,55 @@ def get_open_orders(
*,
include_executions: bool = False,
) -> dict[str, Any]:
- """Fetch today's orders, split into still-open vs already-executed."""
+ """Fetch today's orders, split into still-open vs already-executed.
+
+ Asks every venue in ``_EXCHANGE_IDS_INQUIRY`` and merges, because the
+ endpoint's EXCG_ID_DVSN_CD buckets do not overlap — see that constant.
+ """
cfg = config or load_config()
today = _kst_today()
- payload = _get_paginated(
- cfg,
- "/uapi/domestic-stock/v1/trading/inquire-daily-ccld",
- tr_id=_TR_DAILY_CCLD[cfg.environment],
- params={
- "CANO": cfg.account_no,
- "ACNT_PRDT_CD": cfg.account_product_code,
- "INQR_STRT_DT": today,
- "INQR_END_DT": today,
- "SLL_BUY_DVSN_CD": "00",
- "CCLD_DVSN": "00",
- "ORD_GNO_BRNO": "",
- "ODNO": "",
- "INQR_DVSN_3": "00",
- "INQR_DVSN_1": "",
- "INQR_DVSN": "00",
- "EXCG_ID_DVSN_CD": _EXCHANGE_ID_KRX,
- "CTX_AREA_FK100": "",
- "CTX_AREA_NK100": "",
- },
- )
+
+ items: list[Mapping[str, Any]] = []
+ seen: set[tuple[str, str]] = set()
+ venue_errors: list[str] = []
+ for exchange_id in _EXCHANGE_IDS_INQUIRY:
+ try:
+ payload = _get_paginated(
+ cfg,
+ "/uapi/domestic-stock/v1/trading/inquire-daily-ccld",
+ tr_id=_TR_DAILY_CCLD[cfg.environment],
+ params={
+ "CANO": cfg.account_no,
+ "ACNT_PRDT_CD": cfg.account_product_code,
+ "INQR_STRT_DT": today,
+ "INQR_END_DT": today,
+ "SLL_BUY_DVSN_CD": "00",
+ "CCLD_DVSN": "00",
+ "ORD_GNO_BRNO": "",
+ "ODNO": "",
+ "INQR_DVSN_3": "00",
+ "INQR_DVSN_1": "",
+ "INQR_DVSN": "00",
+ "EXCG_ID_DVSN_CD": exchange_id,
+ "CTX_AREA_FK100": "",
+ "CTX_AREA_NK100": "",
+ },
+ )
+ except KISAPIError as exc:
+ # One venue rejecting the filter must not hide the others' fills.
+ venue_errors.append(f"{exchange_id}: {exc}")
+ continue
+ for item in _as_list(payload.get("output1")):
+ # odno is unique per order per day; ord_dt guards a day rollover
+ # mid-call. A row without one is kept rather than silently dropped.
+ key = (str(item.get("ord_dt") or ""), str(item.get("odno") or ""))
+ if key[1] and key in seen:
+ continue
+ seen.add(key)
+ items.append(item)
open_orders, executions = [], []
- for item in _as_list(payload.get("output1")):
+ for item in items:
row = _order_to_dict(item)
remaining = _as_float(item.get("rmn_qty"))
if remaining > 0:
@@ -351,6 +382,8 @@ def get_open_orders(
}
if include_executions:
result["executions"] = executions
+ if venue_errors:
+ result["venue_errors"] = venue_errors
return result
@@ -788,26 +821,49 @@ def _request(
if missing:
raise KISConfigError(f"KIS connector not configured: missing {', '.join(missing)}.")
- try:
- response = requests.request(
- method.upper(),
- f"{cfg.base_url}{path}",
- headers=_headers(cfg, tr_id, tr_cont),
- params=dict(params or {}),
- json=dict(body) if body is not None else None,
- timeout=cfg.timeout,
- )
- except requests.RequestException as exc:
- raise KISAPIError(f"KIS request failed: {exc}") from exc
+ for attempt in (0, 1):
+ try:
+ response = requests.request(
+ method.upper(),
+ f"{cfg.base_url}{path}",
+ headers=_headers(cfg, tr_id, tr_cont),
+ params=dict(params or {}),
+ json=dict(body) if body is not None else None,
+ timeout=cfg.timeout,
+ )
+ except requests.RequestException as exc:
+ raise KISAPIError(f"KIS request failed: {exc}") from exc
+
+ if response.status_code in (401, 403):
+ raise KISAPIError("KIS API authentication failed: check app_key/app_secret.")
+ if response.status_code >= 400:
+ message = _error_message(response)
+ # KIS issues ONE token per app_key: a token minted for the other
+ # environment (or by another process sharing the key) silently
+ # invalidates this one long before the cached expires_at. The cache
+ # still looks valid, so without this the connector fails every call
+ # until the file ages out — which would silently mute monitoring.
+ if attempt == 0 and _is_expired_token(message):
+ _clear_token_cache(cfg)
+ continue
+ raise KISAPIError(f"KIS API returned HTTP {response.status_code}: {message}")
+ try:
+ return response.json(), response.headers
+ except ValueError as exc:
+ raise KISAPIError("KIS API returned invalid JSON.") from exc
+ raise KISAPIError("KIS API rejected the token twice; re-issue failed.")
- if response.status_code in (401, 403):
- raise KISAPIError("KIS API authentication failed: check app_key/app_secret.")
- if response.status_code >= 400:
- raise KISAPIError(f"KIS API returned HTTP {response.status_code}: {_error_message(response)}")
+
+def _is_expired_token(message: str) -> bool:
+ low = message.lower()
+ return "만료된 token" in message or "expired" in low and "token" in low
+
+
+def _clear_token_cache(cfg: KISConfig) -> None:
try:
- return response.json(), response.headers
- except ValueError as exc:
- raise KISAPIError("KIS API returned invalid JSON.") from exc
+ _token_cache_path(cfg).unlink(missing_ok=True)
+ except OSError:
+ pass
def _error_message(response: requests.Response) -> str:
diff --git a/agent/tests/test_sdk_connectors_kis.py b/agent/tests/test_sdk_connectors_kis.py
index 29d7e1442..e13408f50 100644
--- a/agent/tests/test_sdk_connectors_kis.py
+++ b/agent/tests/test_sdk_connectors_kis.py
@@ -365,3 +365,55 @@ def test_kis_order_ops_classified_write() -> None:
assert KIS_TOOL_CLASS[name] is ToolClass.WRITE
for name in ("get_positions", "get_account_snapshot"):
assert KIS_TOOL_CLASS[name] is ToolClass.READ
+
+
+def test_kis_open_orders_merges_disjoint_exchange_buckets(monkeypatch) -> None:
+ """daily-ccld's EXCG_ID_DVSN_CD buckets do not overlap.
+
+ A live account returned 6 rows for KRX and 33 for SOR with zero overlap, so
+ asking only for KRX dropped most fills and the orders view looked empty.
+ """
+ cfg = kis.KISConfig(app_key="k", app_secret="s", account_no="12345678", profile="paper")
+ by_venue = {
+ "KRX": [{"ord_dt": "20261006", "odno": "1", "pdno": "069500", "rmn_qty": "0"}],
+ "SOR": [
+ {"ord_dt": "20261006", "odno": "2", "pdno": "000660", "rmn_qty": "0"},
+ {"ord_dt": "20261006", "odno": "3", "pdno": "005930", "rmn_qty": "5"},
+ ],
+ "NXT": [],
+ }
+ asked: list[str] = []
+
+ def fake_paginated(_cfg, _path, *, tr_id, params):
+ asked.append(params["EXCG_ID_DVSN_CD"])
+ return {"output1": by_venue[params["EXCG_ID_DVSN_CD"]]}
+
+ monkeypatch.setattr(kis, "_get_paginated", fake_paginated)
+ result = kis.get_open_orders(cfg, include_executions=True)
+
+ assert asked == list(kis._EXCHANGE_IDS_INQUIRY)
+ assert [r["symbol"] for r in result["executions"]] == ["069500", "000660"]
+ assert [r["symbol"] for r in result["open_orders"]] == ["005930"]
+
+
+def test_kis_open_orders_dedupes_an_order_reported_by_two_venues(monkeypatch) -> None:
+ cfg = kis.KISConfig(app_key="k", app_secret="s", account_no="12345678", profile="paper")
+ row = {"ord_dt": "20261006", "odno": "7", "pdno": "000660", "rmn_qty": "0"}
+ monkeypatch.setattr(kis, "_get_paginated", lambda *a, **k: {"output1": [dict(row)]})
+ result = kis.get_open_orders(cfg, include_executions=True)
+ assert len(result["executions"]) == 1
+
+
+def test_kis_open_orders_survives_one_venue_rejecting_the_filter(monkeypatch) -> None:
+ """A venue the broker does not accept must not hide the other venues' fills."""
+ cfg = kis.KISConfig(app_key="k", app_secret="s", account_no="12345678", profile="paper")
+
+ def fake_paginated(_cfg, _path, *, tr_id, params):
+ if params["EXCG_ID_DVSN_CD"] != "KRX":
+ raise kis.KISAPIError("unsupported exchange")
+ return {"output1": [{"ord_dt": "20261006", "odno": "1", "pdno": "069500", "rmn_qty": "0"}]}
+
+ monkeypatch.setattr(kis, "_get_paginated", fake_paginated)
+ result = kis.get_open_orders(cfg, include_executions=True)
+ assert [r["symbol"] for r in result["executions"]] == ["069500"]
+ assert len(result["venue_errors"]) == 2