fix(broker): enforce operator broker truth boundary
This commit is contained in:
@@ -18,6 +18,7 @@ import httpx
|
||||
|
||||
from trex_core.application.broker_port import (
|
||||
BalanceItem,
|
||||
BrokerBalanceError,
|
||||
FillStatus,
|
||||
OrderAck,
|
||||
OrderRequest,
|
||||
@@ -285,8 +286,9 @@ class KISBroker:
|
||||
"""KIS 주식잔고조회 (`TTTC8434R`/`VTTC8434R`).
|
||||
|
||||
Aggregates `output1[*].pdno` + `hldg_qty`, drops zero-qty rows
|
||||
(D-2 leftover, Design Decision 7). Network/credential errors map to
|
||||
an empty list (caller treats as no broker positions).
|
||||
(D-2 leftover, Design Decision 7). Only a validated `rt_cd=0` response
|
||||
may return an empty list; token/HTTP/shape/KIS failures raise a sanitized
|
||||
:class:`BrokerBalanceError` so reconciliation cannot infer false zero.
|
||||
"""
|
||||
cano, product = self._account()
|
||||
tr_id = "VTTC8434R" if self._mock else "TTTC8434R"
|
||||
@@ -298,21 +300,44 @@ class KISBroker:
|
||||
"PRCS_DVSN": "01",
|
||||
"CTX_AREA_FK100": "", "CTX_AREA_NK100": "",
|
||||
}
|
||||
try:
|
||||
headers = self._headers(tr_id)
|
||||
except (httpx.HTTPError, KeyError, TypeError, ValueError):
|
||||
# Token issuance/header construction is distinct from a rejected
|
||||
# balance endpoint response and must never look like an empty book.
|
||||
raise BrokerBalanceError("token_issuance_failed") from None
|
||||
try:
|
||||
resp = httpx.get(
|
||||
f"{self._base_url}/uapi/domestic-stock/v1/trading/inquire-balance",
|
||||
headers=self._headers(tr_id), params=params, timeout=self._timeout,
|
||||
headers=headers, params=params, timeout=self._timeout,
|
||||
)
|
||||
resp.raise_for_status()
|
||||
payload = resp.json()
|
||||
except (httpx.HTTPError, ValueError):
|
||||
return []
|
||||
except httpx.HTTPStatusError as exc:
|
||||
status = exc.response.status_code if exc.response is not None else None
|
||||
code = "authentication_failed" if status == 401 else (
|
||||
"authorization_failed" if status == 403 else "transport_failed"
|
||||
)
|
||||
raise BrokerBalanceError(code) from None
|
||||
except httpx.HTTPError:
|
||||
raise BrokerBalanceError("transport_failed") from None
|
||||
except (ValueError, TypeError):
|
||||
raise BrokerBalanceError("malformed_response") from None
|
||||
if not isinstance(payload, dict):
|
||||
raise BrokerBalanceError("malformed_response")
|
||||
if str(payload.get("rt_cd")) != "0":
|
||||
return []
|
||||
rows = payload.get("output1") or []
|
||||
raise BrokerBalanceError("broker_rejected")
|
||||
rows = payload.get("output1")
|
||||
if not isinstance(rows, list):
|
||||
raise BrokerBalanceError("malformed_response")
|
||||
out: list[BalanceItem] = []
|
||||
for row in rows:
|
||||
if not isinstance(row, dict):
|
||||
raise BrokerBalanceError("malformed_response")
|
||||
symbol = str(row.get("pdno") or "").strip()
|
||||
qty = _safe_int(row.get("hldg_qty")) or 0
|
||||
qty = _safe_int(row.get("hldg_qty"))
|
||||
if qty is None:
|
||||
raise BrokerBalanceError("malformed_response")
|
||||
if not symbol or qty <= 0:
|
||||
continue
|
||||
out.append(BalanceItem(symbol=symbol, hldg_qty=qty))
|
||||
|
||||
@@ -134,6 +134,18 @@ class PostgreSQLSlotRepository:
|
||||
)
|
||||
return [self._row_to_slot(r) for r in cur.fetchall()]
|
||||
|
||||
def find_managed_slot_by_symbol(self, instance_key: str, symbol: str) -> Optional[Slot]:
|
||||
with self._psycopg.connect(self._dsn) as conn:
|
||||
with conn.cursor() as cur:
|
||||
cur.execute(
|
||||
f"SELECT {_SLOT_COLUMNS} FROM daily_slot "
|
||||
"WHERE instance_key = %s AND symbol = %s "
|
||||
"ORDER BY updated_at DESC NULLS LAST, slot_id ASC LIMIT 1",
|
||||
(instance_key, symbol),
|
||||
)
|
||||
row = cur.fetchone()
|
||||
return self._row_to_slot(row) if row else None
|
||||
|
||||
def reconcile_universe(
|
||||
self, instance_key: str, universe: dict[str, str],
|
||||
*, allow_retire: bool = True,
|
||||
|
||||
Reference in New Issue
Block a user