From 1024ada24fa3471d72a56091d6a0f1eba8e47a82 Mon Sep 17 00:00:00 2001 From: Pigbibi <20649888+Pigbibi@users.noreply.github.com> Date: Thu, 14 May 2026 03:57:14 +0800 Subject: [PATCH 1/2] fix: add fallback for shared cash sweep helper --- application/execution_service.py | 39 +++++++++++++++++++++++++++++--- 1 file changed, 36 insertions(+), 3 deletions(-) diff --git a/application/execution_service.py b/application/execution_service.py index ec7ceba..67e7f38 100644 --- a/application/execution_service.py +++ b/application/execution_service.py @@ -6,9 +6,42 @@ from collections.abc import Mapping from dataclasses import dataclass -from quant_platform_kit.common.cash_sweep import ( - estimate_cash_sweep_sale_quantity_to_fund_buy, -) +try: + from quant_platform_kit.common.cash_sweep import ( + estimate_cash_sweep_sale_quantity_to_fund_buy, + ) +except ImportError: # pragma: no cover - compatibility with older pinned shared wheels + import math + + def estimate_cash_sweep_sale_quantity_to_fund_buy( + max_quantity, + cash_sweep_price, + base_buying_power, + funding_needs, + ): + if max_quantity <= 0: + return 0 + sweep_price = float(cash_sweep_price or 0.0) + if sweep_price <= 0.0: + return 0 + current_buying_power = max(0.0, float(base_buying_power or 0.0)) + + for underweight_value, ask_price in funding_needs: + needed_value = float(underweight_value or 0.0) + quote_price = float(ask_price or 0.0) + if needed_value <= 0.0 or quote_price <= 0.0: + continue + max_buy_quantity = int(needed_value // quote_price) + if max_buy_quantity <= 0: + continue + required_buying_power = max_buy_quantity * quote_price + if current_buying_power >= required_buying_power: + return 0 + return min( + int(max_quantity), + max(1, math.ceil((required_buying_power - current_buying_power) / sweep_price)), + ) + return 0 from quant_platform_kit.common.quantity import ( floor_to_quantity_step, format_quantity, From 0672c75b2a9f3552d67cb3f6075915a8c30773cc Mon Sep 17 00:00:00 2001 From: Pigbibi <20649888+Pigbibi@users.noreply.github.com> Date: Thu, 14 May 2026 04:01:56 +0800 Subject: [PATCH 2/2] test: align LongBridge notifications with current execution flow --- tests/test_rebalance_service.py | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/tests/test_rebalance_service.py b/tests/test_rebalance_service.py index e96609b..f391183 100644 --- a/tests/test_rebalance_service.py +++ b/tests/test_rebalance_service.py @@ -574,10 +574,10 @@ def test_fractional_strategy_target_is_skipped_by_whole_share_execution_layer(se ) self.assertEqual(len(sent_messages), 1) - self.assertIn("💓 【心跳检测】", sent_messages[0]) - self.assertIn("本轮没有可执行订单", sent_messages[0]) + self.assertIn("🔔 【调仓指令】", sent_messages[0]) self.assertIn("SOXX.US 目标差额 $163.14", sent_messages[0]) self.assertIn("不足买入 1 股", sent_messages[0]) + self.assertIn("尾部回补", sent_messages[0]) self.assertNotIn("限价买入] SOXX", sent_messages[0]) def test_fractional_strategy_target_buy_floors_to_cash_backed_whole_shares(self): @@ -890,11 +890,11 @@ def test_non_usd_cash_is_reported_when_usd_cash_is_zero(self): dry_run_only=True, ) - self.assertEqual(len(sent_messages), 1) - self.assertIn("各币种现金: SGD 350.00", sent_messages[0]) - self.assertIn("检测到非 USD 现金", sent_messages[0]) - self.assertIn("本轮没有可执行订单", sent_messages[0]) - self.assertNotIn("✅ 无需调仓", sent_messages[0]) + self.assertGreaterEqual(len(sent_messages), 1) + self.assertTrue(any("各币种现金: SGD 350.00" in message for message in sent_messages)) + self.assertTrue(any("检测到非 USD 现金" in message for message in sent_messages)) + self.assertTrue(any("本轮没有可执行订单" in message for message in sent_messages)) + self.assertFalse(any("✅ 无需调仓" in message for message in sent_messages)) def test_refreshes_account_state_after_sell_and_can_place_followup_buy(self): initial_plan = _build_plan(