"Vibe-Trading: Your Personal Trading Agent"
Python ai-agentalgorithmic-tradingbacktestingfintechllmmcpmulti-agentpythonquantitative-financetrading
fix(upbit): pin the paper-sim cancel id and stop an unneeded quote fetch
heonjinjeongbea316415323
diff --git a/.gitignore b/.gitignoreindex c0ca11f3a..3ce3c6552 100644--- a/.gitignore+++ b/.gitignore@@ -85,6 +85,7 @@ agent/uploads//shoonya.json/tiger.json/trading212.json+/upbit.json/zerodha.jsonagent/tests/*_result.json.ruff_cache/diff --git a/agent/src/trading/connectors/upbit/sdk.py b/agent/src/trading/connectors/upbit/sdk.pyindex 7816d7d73..619cbdf05 100644--- a/agent/src/trading/connectors/upbit/sdk.py+++ b/agent/src/trading/connectors/upbit/sdk.py@@ -35,6 +35,13 @@ PROFILE_ENVIRONMENTS = {UPBIT_API_URL = "https://api.upbit.com/v1"+#: Prefix of every order id the local paper simulator issues. ``cancel_order``+#: refuses any other id, because only these orders exist in the simulation —+#: reads (``get_open_orders``) hit the real account, so a real order's id+#: could otherwise reach here and be acknowledged as cancelled without ever+#: calling Upbit.+_PAPER_ORDER_PREFIX = "PAPER-"+#: Returned by order methods when a non-paper config reaches them. Upbit#: exposes no runtime paper/live discriminator (same key pair reads the same#: account), so — following the Dhan/Longbridge precedent — the connector is@@ -414,15 +421,17 @@ def place_order(return {"status": "error", "error": "limit order requires limit_price"}# Paper-only: simulate locally against a live quote (Upbit has no sandbox).+ # A quantity-sized order needs no price at all, so it never makes the+ # network call -- only notional sizing (converting a KRW spend into a+ # quantity) does, and only when no limit_price was given to use instead.fill_price = float(limit_price) if limit_price is not None else None- if fill_price is None:- quote = get_quote(market, config=cfg)- fill_price = _as_float(quote.get("quote", {}).get("last")) if quote.get("status") == "ok" else Noneif has_notional:- # notional sizing needs a price to convert KRW spend into a quantity —- # an unpriced fill here would silently fabricate a null quantity.+ if fill_price is None:+ quote = get_quote(market, config=cfg)+ fill_price = _as_float(quote.get("quote", {}).get("last")) if quote.get("status") == "ok" else Noneif not fill_price:+ # An unpriced fill here would silently fabricate a null quantity.return {"status": "error","error": "could not resolve a live quote to size this notional order; retry or pass limit_price",@@ -434,7 +443,7 @@ def place_order(return {"status": "ok",- "order_id": f"PAPER-{market}-{side_token}-{uuid.uuid4().hex[:8]}",+ "order_id": f"{_PAPER_ORDER_PREFIX}{market}-{side_token}-{uuid.uuid4().hex[:8]}","symbol": market,"side": side_token,"profile": cfg.profile,@@ -468,6 +477,17 @@ def cancel_order(clean_id = str(order_id or "").strip()if not clean_id:return {"status": "error", "error": "order_id is required"}+ if not clean_id.startswith(_PAPER_ORDER_PREFIX):+ # get_open_orders reads the real account, so a real order's id can+ # arrive here; acknowledging it would report a cancel that never+ # happened while the real order keeps working.+ return {+ "status": "error",+ "error": (+ f"order {clean_id!r} was not issued by this paper simulator, so it "+ "cannot be cancelled here; cancel a real order on Upbit directly"+ ),+ }return {"status": "ok",diff --git a/agent/tests/test_paper_sim_cancel_ids.py b/agent/tests/test_paper_sim_cancel_ids.pyindex b96747999..ee1136630 100644--- a/agent/tests/test_paper_sim_cancel_ids.py+++ b/agent/tests/test_paper_sim_cancel_ids.py@@ -1,13 +1,13 @@"""A locally simulated paper cancel may only acknowledge orders the simulator issued.-Zerodha, Dhan and Shoonya expose no sandbox, so their paper profiles simulate-orders locally — but their reads (``get_open_orders``, ``get_positions``) hit-the real account. A real open order's id can therefore reach the paper-``cancel_order``, which used to answer ``cancelled: True`` for any id while-never calling the broker: the agent was told a live order was gone while it-kept working. Both directions are pinned per connector, and the accepted id is-taken from ``place_order`` so the issuing and the checking side cannot drift-apart.+Zerodha, Dhan, Shoonya and Upbit expose no sandbox, so their paper profiles+simulate orders locally — but their reads (``get_open_orders``,+``get_positions``) hit the real account. A real open order's id can therefore+reach the paper ``cancel_order``, which used to answer ``cancelled: True`` for+any id while never calling the broker: the agent was told a live order was+gone while it kept working. Both directions are pinned per connector, and the+accepted id is taken from ``place_order`` so the issuing and the checking side+cannot drift apart."""from __future__ import annotations@@ -16,18 +16,23 @@ import pytestfrom src.trading.connectors.dhan import sdk as dhan_sdkfrom src.trading.connectors.shoonya import sdk as shoonya_sdk+from src.trading.connectors.upbit import sdk as upbit_sdkfrom src.trading.connectors.zerodha import sdk as zerodha_sdk_CONNECTORS = [- pytest.param(zerodha_sdk, zerodha_sdk.ZerodhaConfig, id="zerodha"),- pytest.param(dhan_sdk, dhan_sdk.DhanConfig, id="dhan"),- pytest.param(shoonya_sdk, shoonya_sdk.ShoonyaConfig, id="shoonya"),+ pytest.param(zerodha_sdk, zerodha_sdk.ZerodhaConfig, {"symbol": "RELIANCE"}, id="zerodha"),+ pytest.param(dhan_sdk, dhan_sdk.DhanConfig, {"symbol": "RELIANCE"}, id="dhan"),+ pytest.param(shoonya_sdk, shoonya_sdk.ShoonyaConfig, {"symbol": "RELIANCE"}, id="shoonya"),+ # quantity-based sizing needs no live quote, so Upbit's place_order takes+ # the same no-network path as the other three here; a notional-sized+ # order would call get_quote and needs a mocked one instead.+ pytest.param(upbit_sdk, upbit_sdk.UpbitConfig, {"symbol": "KRW-BTC"}, id="upbit"),][email protected]("mod, Config", _CONNECTORS)-def test_the_id_place_order_issued_can_be_cancelled(mod, Config) -> None:- placed = mod.place_order(Config(profile="paper"), symbol="RELIANCE", side="buy", quantity=1)[email protected]("mod, Config, order_kwargs", _CONNECTORS)+def test_the_id_place_order_issued_can_be_cancelled(mod, Config, order_kwargs) -> None:+ placed = mod.place_order(Config(profile="paper"), side="buy", quantity=1, **order_kwargs)assert placed["status"] == "ok"cancelled = mod.cancel_order(Config(profile="paper"), placed["order_id"])@@ -37,9 +42,9 @@ def test_the_id_place_order_issued_can_be_cancelled(mod, Config) -> None:assert cancelled["order_id"] == placed["order_id"][email protected]("mod, Config", _CONNECTORS)[email protected]("mod, Config, order_kwargs", _CONNECTORS)@pytest.mark.parametrize("foreign_id", ["250911000123456", "ORD1", "paper-RELIANCE-B-1", " 1234 "])-def test_an_id_the_simulator_never_issued_is_refused(mod, Config, foreign_id) -> None:+def test_an_id_the_simulator_never_issued_is_refused(mod, Config, order_kwargs, foreign_id) -> None:result = mod.cancel_order(Config(profile="paper"), foreign_id)assert result["status"] == "error"diff --git a/agent/tests/test_sdk_connectors_upbit.py b/agent/tests/test_sdk_connectors_upbit.pyindex 165e21154..d4fe166bb 100644--- a/agent/tests/test_sdk_connectors_upbit.py+++ b/agent/tests/test_sdk_connectors_upbit.py@@ -64,12 +64,24 @@ def test_upbit_paper_place_order_simulated_locally() -> None:def test_upbit_paper_cancel_order_simulated() -> None:cfg = up.UpbitConfig(access_key="ak", secret_key="sk", profile="paper")- result = up.cancel_order(cfg, "ORD1")+ placed = up.place_order(cfg, symbol="KRW-BTC", side="buy", quantity=1)+ result = up.cancel_order(cfg, placed["order_id"])assert result["status"] == "ok"assert result["cancelled"] is Trueassert result["is_paper"] is True+def test_upbit_paper_cancel_refuses_an_id_the_simulator_never_issued() -> None:+ """get_open_orders reads the real account, so a real order's id could+ reach here; it must not be acknowledged as cancelled (no Upbit call was+ ever made)."""+ cfg = up.UpbitConfig(access_key="ak", secret_key="sk", profile="paper")+ result = up.cancel_order(cfg, "250911000123456")+ assert result["status"] == "error"+ assert "cancelled" not in result+ assert "not issued by this paper simulator" in result["error"]++def test_upbit_live_order_hard_refused() -> None:"""The guard must be structurally impossible to bypass, not a checked flag:it runs as the first statement in place_order/cancel_order, before any