fix: persist uncertain cancel outcomes
This commit is contained in:
@@ -12,7 +12,11 @@ import httpx
|
||||
import pytest
|
||||
|
||||
from trex_core.adapters.broker.kis import KISBroker
|
||||
from trex_core.application.broker_port import BrokerBalanceError, OrderRequest
|
||||
from trex_core.application.broker_port import (
|
||||
BrokerBalanceError,
|
||||
CancelState,
|
||||
OrderRequest,
|
||||
)
|
||||
|
||||
_MOCK_URL = "https://openapivts.koreainvestment.com:29443"
|
||||
|
||||
@@ -179,15 +183,13 @@ def test_submit_transport_failure_blocks_automatic_duplicate(monkeypatch):
|
||||
assert ack.accepted is True
|
||||
assert ack.broker_order_id.startswith("submission-unknown:")
|
||||
assert ack.reason == "submit_outcome_unknown:ReadTimeout"
|
||||
assert (
|
||||
b.cancel_order(
|
||||
ack.broker_order_id,
|
||||
side="buy",
|
||||
symbol="005930",
|
||||
qty=4,
|
||||
)
|
||||
is False
|
||||
cancel = b.cancel_order(
|
||||
ack.broker_order_id,
|
||||
side="buy",
|
||||
symbol="005930",
|
||||
qty=4,
|
||||
)
|
||||
assert cancel.state is CancelState.UNKNOWN
|
||||
|
||||
|
||||
def test_submit_gateway_5xx_blocks_automatic_duplicate(monkeypatch):
|
||||
|
||||
@@ -9,6 +9,8 @@ from trex_core.adapters.broker.ownership import (
|
||||
from trex_core.application.broker_port import (
|
||||
BalanceItem,
|
||||
BrokerBalanceError,
|
||||
CancelOutcome,
|
||||
CancelState,
|
||||
FillStatus,
|
||||
OrderAck,
|
||||
)
|
||||
@@ -29,7 +31,7 @@ class FakeBroker:
|
||||
return self.fill
|
||||
|
||||
def cancel_order(self, broker_order_id, *, side, symbol, qty, venue="KRX"):
|
||||
return True
|
||||
return CancelOutcome(CancelState.REQUESTED)
|
||||
|
||||
def get_balance(self):
|
||||
return self.balance
|
||||
|
||||
@@ -26,12 +26,13 @@ from __future__ import annotations
|
||||
from datetime import datetime, timedelta, timezone
|
||||
from decimal import Decimal
|
||||
|
||||
import httpx
|
||||
import pytest
|
||||
|
||||
from trex_core.adapters.broker import kis as kis_mod
|
||||
from trex_core.adapters.broker.kis import KISBroker, resolve_order_venue
|
||||
from trex_core.adapters.broker.paper import PaperBroker
|
||||
from trex_core.application.broker_port import OrderRequest
|
||||
from trex_core.application.broker_port import CancelState, OrderRequest
|
||||
|
||||
_MOCK_URL = "https://openapivts.koreainvestment.com:29443"
|
||||
_LIVE_URL = "https://openapi.koreainvestment.com:9443"
|
||||
@@ -218,7 +219,7 @@ def test_paper_cancel_venue_ignored():
|
||||
ack = b.submit_order(req)
|
||||
# NXT venue로 cancel — paper는 venue 무관하게 취소 성공
|
||||
result = b.cancel_order(ack.broker_order_id, side="buy", symbol="005930", qty=5, venue="NXT")
|
||||
assert result is True
|
||||
assert result.state is CancelState.REQUESTED
|
||||
|
||||
|
||||
def test_paper_cancel_default_venue():
|
||||
@@ -227,7 +228,7 @@ def test_paper_cancel_default_venue():
|
||||
req = OrderRequest(slot_id="s1", side="buy", symbol="005930", qty=3)
|
||||
ack = b.submit_order(req)
|
||||
result = b.cancel_order(ack.broker_order_id, side="buy", symbol="005930", qty=3)
|
||||
assert result is True
|
||||
assert result.state is CancelState.REQUESTED
|
||||
|
||||
|
||||
# ---------------------------------------------------------------------------
|
||||
@@ -301,6 +302,40 @@ def test_cancel_order_krx_includes_excg(monkeypatch):
|
||||
assert captured["body"]["EXCG_ID_DVSN_CD"] == "KRX"
|
||||
|
||||
|
||||
def test_cancel_explicit_broker_rejection_is_distinct(monkeypatch):
|
||||
def fake_post(url, headers=None, json=None, timeout=None):
|
||||
return _FakeResp({"rt_cd": "1", "msg_cd": "ORDER_NOT_CANCELLABLE"})
|
||||
|
||||
b = _mock_broker()
|
||||
monkeypatch.setattr(kis_mod.httpx, "post", fake_post)
|
||||
outcome = b.cancel_order(
|
||||
"06010:0000123456",
|
||||
side="buy",
|
||||
symbol="005930",
|
||||
qty=1,
|
||||
)
|
||||
|
||||
assert outcome.state is CancelState.REJECTED
|
||||
assert outcome.reason == "ORDER_NOT_CANCELLABLE"
|
||||
|
||||
|
||||
def test_cancel_transport_loss_is_unknown(monkeypatch):
|
||||
def fake_post(url, headers=None, json=None, timeout=None):
|
||||
raise httpx.ReadTimeout("timed out")
|
||||
|
||||
b = _mock_broker()
|
||||
monkeypatch.setattr(kis_mod.httpx, "post", fake_post)
|
||||
outcome = b.cancel_order(
|
||||
"06010:0000123456",
|
||||
side="buy",
|
||||
symbol="005930",
|
||||
qty=1,
|
||||
)
|
||||
|
||||
assert outcome.state is CancelState.UNKNOWN
|
||||
assert outcome.reason == "transport_failed"
|
||||
|
||||
|
||||
# ---------------------------------------------------------------------------
|
||||
# REQ-118 AC-5: get_current_price 기본 market=J
|
||||
# ---------------------------------------------------------------------------
|
||||
@@ -422,4 +457,4 @@ def test_paper_cancel_backward_compat_no_venue():
|
||||
ack = b.submit_order(req)
|
||||
# 기존 스타일 호출 (venue 없음)
|
||||
result = b.cancel_order(ack.broker_order_id, side="sell", symbol="005930", qty=2)
|
||||
assert result is True
|
||||
assert result.state is CancelState.REQUESTED
|
||||
|
||||
@@ -35,7 +35,8 @@ _SLOT_COLUMNS = (
|
||||
"order_id, strategy_id, broker_order_id, portfolio_id, "
|
||||
"sell_strategy_id, forced_close_at, stop_loss_price, take_profit_price, "
|
||||
"created_at, updated_at, instance_key, retired_at, "
|
||||
"timeout_sec, slippage_pct, occupancy_seq, venue"
|
||||
"timeout_sec, slippage_pct, occupancy_seq, venue, "
|
||||
"cancel_state, cancel_requested_at"
|
||||
)
|
||||
_MANAGED_SLOT_SQL = (
|
||||
f"SELECT {_SLOT_COLUMNS} FROM daily_slot "
|
||||
@@ -146,6 +147,8 @@ def _row(slot: Slot) -> tuple:
|
||||
Decimal(str(slot.slippage_pct)),
|
||||
slot.occupancy_seq,
|
||||
slot.venue,
|
||||
slot.cancel_state,
|
||||
slot.cancel_requested_at,
|
||||
)
|
||||
|
||||
|
||||
|
||||
Reference in New Issue
Block a user