"Vibe-Trading: Your Personal Trading Agent"
Python ai-agentalgorithmic-tradingbacktestingfintechllmmcpmulti-agentpythonquantitative-financetrading
fix(longbridge): keep per-currency cash and financing limits in balances
alex38b59f0d2361
diff --git a/agent/src/trading/connectors/longbridge/sdk.py b/agent/src/trading/connectors/longbridge/sdk.pyindex de6025e48..62914cb2c 100644--- a/agent/src/trading/connectors/longbridge/sdk.py+++ b/agent/src/trading/connectors/longbridge/sdk.py@@ -852,6 +852,25 @@ def _balance_to_dict(item: Any) -> dict[str, Any]:"buy_power": _float_or_none(_first(item, ("buy_power",))),"init_margin": _float_or_none(_first(item, ("init_margin",))),"maintenance_margin": _float_or_none(_first(item, ("maintenance_margin",))),+ "max_finance_amount": _float_or_none(_first(item, ("max_finance_amount",))),+ "remaining_finance_amount": _float_or_none(+ _first(item, ("remaining_finance_amount",))+ ),+ # total_cash nets every currency into one figure; a negative leg+ # (borrowing) only shows here.+ "cash_infos": [+ _cash_info_to_dict(info) for info in _as_iter(_first(item, ("cash_infos",)))+ ],+ }+++def _cash_info_to_dict(item: Any) -> dict[str, Any]:+ return {+ "currency": _first(item, ("currency",)),+ "available_cash": _float_or_none(_first(item, ("available_cash",))),+ "withdraw_cash": _float_or_none(_first(item, ("withdraw_cash",))),+ "frozen_cash": _float_or_none(_first(item, ("frozen_cash",))),+ "settling_cash": _float_or_none(_first(item, ("settling_cash",))),}diff --git a/agent/tests/test_sdk_connectors.py b/agent/tests/test_sdk_connectors.pyindex 28ab352ee..83bb93911 100644--- a/agent/tests/test_sdk_connectors.py+++ b/agent/tests/test_sdk_connectors.py@@ -9,6 +9,7 @@ and the service dispatch degrading cleanly when nothing is configured.from __future__ import annotationsimport json+from decimal import Decimalfrom types import SimpleNamespaceimport pytest@@ -250,6 +251,73 @@ def test_longbridge_public_config_redacts_secrets() -> None:assert all(value not in repr(pub) for value in values.values())+def test_longbridge_balance_keeps_financing_and_per_currency_cash() -> None:+ """``total_cash`` nets every currency, so a borrowed HKD leg only shows per currency."""+ balance = SimpleNamespace(+ currency="HKD",+ # USD 1,000 at 7.8 net of the HKD -500 leg.+ total_cash=Decimal("7300.00"),+ max_finance_amount=Decimal("5000.00"),+ remaining_finance_amount=Decimal("4000.00"),+ cash_infos=[+ SimpleNamespace(+ currency="USD",+ available_cash=Decimal("1000.00"),+ withdraw_cash=Decimal("900.00"),+ frozen_cash=Decimal("10.00"),+ settling_cash=Decimal("20.00"),+ ),+ SimpleNamespace(+ currency="HKD",+ available_cash=Decimal("-500.00"),+ withdraw_cash=Decimal("-600.00"),+ frozen_cash=Decimal("500.00"),+ settling_cash=Decimal("-200.00"),+ ),+ ],+ )+ row = lb._balance_to_dict(balance)+ assert row["max_finance_amount"] == 5000.0+ assert row["remaining_finance_amount"] == 4000.0+ assert row["cash_infos"] == [+ {+ "currency": "USD",+ "available_cash": 1000.0,+ "withdraw_cash": 900.0,+ "frozen_cash": 10.0,+ "settling_cash": 20.0,+ },+ {+ "currency": "HKD",+ "available_cash": -500.0,+ "withdraw_cash": -600.0,+ "frozen_cash": 500.0,+ "settling_cash": -200.0,+ },+ ]+++def test_longbridge_balance_sparse_reply_reads_as_none() -> None:+ """Fields the SDK omits read as missing, never as zero cash or headroom."""+ row = lb._balance_to_dict(SimpleNamespace(currency="HKD", total_cash=1))+ assert row["cash_infos"] == []+ assert row["max_finance_amount"] is None+ assert row["remaining_finance_amount"] is None++ row = lb._balance_to_dict(+ SimpleNamespace(currency="HKD", cash_infos=[SimpleNamespace(currency="USD")])+ )+ assert row["cash_infos"] == [+ {+ "currency": "USD",+ "available_cash": None,+ "withdraw_cash": None,+ "frozen_cash": None,+ "settling_cash": None,+ }+ ]++def _set_longbridge_environment(monkeypatch, values) -> None:for field, env_name in {"app_key": "LONGBRIDGE_APP_KEY",