Skip to content

"Vibe-Trading: Your Personal Trading Agent"

fix(tools): scale southbound Eastmoney values and surface rejections

Haozhe Wue1b4c518dc20
diff --git a/agent/src/tools/southbound_tool.py b/agent/src/tools/southbound_tool.py
index a2736a231..cfd6f2b2b 100644
--- a/agent/src/tools/southbound_tool.py
+++ b/agent/src/tools/southbound_tool.py
@@ -13,8 +13,9 @@ Sources, in order:
* **Eastmoney datacenter** ``RPT_MUTUAL_DEAL_HISTORY`` (``MUTUAL_TYPE``
``002`` = 港股通沪, ``004`` = 港股通深) through the shared throttled
client: daily net buy (``NET_DEAL_AMT``), buy/sell turnover and the
- cumulative net series, unit 100M HKD (亿). Verified against HKEX
- official daily statistics to the cent on 2026-09-15/16/17.
+ cumulative net series. ``NET_DEAL_AMT`` is in million HKD and is scaled to
+ the envelope's 100M HKD (亿) unit. Verified against HKEX official daily
+ statistics to the cent on 2026-09-15/16/17.
* **HKEX official** Stock Connect Daily Statistics report (keyless JS
feed): latest available trading day only, net = Buy Turnover − Sell
Turnover per channel, converted from million HKD to the envelope unit.
@@ -45,8 +46,9 @@ from src.agent.tools import BaseTool
logger = logging.getLogger(__name__)
# Eastmoney datacenter report carrying the Stock-Connect daily history.
-# MUTUAL_TYPE: "002" = 港股通(沪), "004" = 港股通(深). Amounts are in
-# 100M HKD (亿); NET_DEAL_AMT cross-checks against HKEX Buy − Sell exactly.
+# MUTUAL_TYPE: "002" = 港股通(沪), "004" = 港股通(深). NET_DEAL_AMT is in
+# million HKD; the tool scales it to 100M HKD (亿) to match the envelope and
+# the HKEX parser.
_DATACENTER_URL = "https://datacenter-web.eastmoney.com/api/data/v1/get"
_REPORT_NAME = "RPT_MUTUAL_DEAL_HISTORY"
_CHANNEL_TYPES = {"shanghai_connect": "002", "shenzhen_connect": "004"}
@@ -121,7 +123,12 @@ def _fetch_eastmoney_channel(mutual_type: str, days: int) -> list[dict[str, Any]
"pageSize": str(days),
},
)
- result = payload.get("result") if isinstance(payload, dict) else None
+ if not isinstance(payload, dict):
+ raise ValueError("Eastmoney datacenter returned a non-object payload")
+ if payload.get("success") is False:
+ message = str(payload.get("message") or "request rejected")
+ raise ValueError(f"Eastmoney datacenter rejected the report: {message}")
+ result = payload.get("result")
rows = result.get("data") if isinstance(result, dict) else None
return rows if isinstance(rows, list) else []
@@ -145,7 +152,7 @@ def _merge_eastmoney(days: int) -> list[dict[str, Any]]:
continue
net = _coerce_float(row.get("NET_DEAL_AMT"))
per_channel.setdefault(date, {})[key] = (
- round(net, 4) if net is not None else None
+ round(net / 100.0, 4) if net is not None else None
)
history: list[dict[str, Any]] = []
for date in sorted(per_channel):
diff --git a/agent/tests/test_southbound_tool.py b/agent/tests/test_southbound_tool.py
index 7bf58cf58..0c2e526ab 100644
--- a/agent/tests/test_southbound_tool.py
+++ b/agent/tests/test_southbound_tool.py
@@ -16,7 +16,7 @@ from src.tools import southbound_tool as sb
def _em_rows(channel_net: dict[str, float]) -> dict:
- """Build one datacenter payload for a channel's NET_DEAL_AMT map."""
+ """Build one datacenter payload for a channel's million-HKD values."""
return {
"result": {
"data": [
@@ -35,8 +35,8 @@ def _em_rows(channel_net: dict[str, float]) -> dict:
def _fake_get_json(url: str, *, params: dict):
assert url == sb._DATACENTER_URL
if '"002"' in params["filter"]:
- return _em_rows({"2026-09-16": 26.6898, "2026-09-17": 25.0239})
- return _em_rows({"2026-09-16": 8.6076, "2026-09-17": 9.9861})
+ return _em_rows({"2026-09-16": 2668.98, "2026-09-17": 2502.39})
+ return _em_rows({"2026-09-16": 860.76, "2026-09-17": 998.61})
class TestEastmoneyEnvelope:
@@ -81,7 +81,7 @@ class TestEastmoneyEnvelope:
def test_one_channel_missing_leaves_none_and_partial_total(self):
def fake(url: str, *, params: dict):
if '"002"' in params["filter"]:
- return _em_rows({"2026-09-17": 25.0239})
+ return _em_rows({"2026-09-17": 2502.39})
return {"result": {"data": []}}
with patch.object(sb.eastmoney_client, "get_json", side_effect=fake):
@@ -159,6 +159,24 @@ class TestHkexFallback:
assert payload["source"] == "hkex"
assert "returned no rows" in payload["warnings"][0]
+ def test_eastmoney_report_rejection_falls_back_with_reason(self):
+ rejected = {
+ "success": False,
+ "message": "RPT_MUTUAL_DEAL_HISTORY report rejected",
+ "result": None,
+ }
+ with (
+ patch.object(sb.eastmoney_client, "get_json", return_value=rejected),
+ patch.object(
+ sb.requests, "get", return_value=self._mock_response(200, self._HKEX_JS)
+ ),
+ ):
+ text = sb.SouthboundFlowTool().execute(lookback_days=5)
+
+ payload = json.loads(text)
+ assert payload["source"] == "hkex"
+ assert "report rejected" in payload["warnings"][0]
+
def test_hkex_walks_back_past_404(self):
responses = [
self._mock_response(404),